Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication7
- {
- class GenericQueueNode<T>
- {
- public GenericQueueNode(T content)
- {
- Content = content;
- next = null;
- }
- public T Content { get; set; }
- public GenericQueueNode<T> next { get; set; }
- }
- class GenericQueue<T>
- {
- private GenericQueueNode<T> _head;
- private int count = 0;
- public bool IsEmpty
- {
- get { return _head == null; }
- }
- public int Count { get { return count; } set { count = value; } }
- private GenericQueueNode<T> getLastNode()
- {
- GenericQueueNode<T> node = _head;
- while (node.next != null)
- node = node.next;
- return node;
- }
- public void Put(T x)
- {
- GenericQueueNode<T> newNode = new GenericQueueNode<T>(x);
- if(IsEmpty)
- {
- GenericQueueNode<T> node = new GenericQueueNode<T>(x);
- _head = newNode;
- }
- else
- {
- GenericQueueNode<T> node = new GenericQueueNode<T>(x);
- GenericQueueNode<T> lastNode = getLastNode();
- lastNode.next = node;
- lastNode = node;
- }
- count++;
- }
- public T Get()
- {
- if (IsEmpty)
- throw new InvalidOperationException("D kju iz empti");
- T x = _head.Content;
- _head = _head.next;
- count--;
- return x;
- }
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
- GenericQueueNode<T> node = _head;
- while (node != null)
- {
- sb.AppendFormat("{0} ", node.Content);
- node = node.next;
- }
- return sb.ToString();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- GenericQueue<int> queue = new GenericQueue<int>();
- queue.Put(1);
- queue.Put(2);
- queue.Put(3);
- Console.WriteLine(queue.ToString());
- Console.WriteLine("Skidam {0} ", queue.Get());
- Console.WriteLine(queue.ToString());
- queue.Put(4);
- queue.Put(5);
- Console.WriteLine(queue.ToString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement