Sunday, September 18, 2016

Queue ADT- Linked List Implementation

using System;

namespace Queue_Linked_List_Implementation
{
     class Queue
     {
        public class Node
        {
            public object item;
            public Node next;

            public Node() {
            }

            public Node(object item) {
               this.item = item;
            }
        }

        private int length;
        private Node rear, front;

        public Queue()
        {
             front = rear = null;
             length = 0;
        }

        public int Length
        {
            get { return length; }
        }

        public bool isEmpty()
        {
             return front == null;
        }

        public void enqueue(Object item)
        {
             length++;
             Node newptr;
             newptr = new Node()
             {
                  next = null,
                  item = item
             };

             if (isEmpty()) {
                  front = rear = newptr;
             }
             else
             {
                  rear.next = newptr;
                  rear = newptr;
             }
        }

        public Object dequeue()
        {
             Object item = "";
             if (!isEmpty())
             {
                  item = front.item;
                  front = front.next;
                  length--;
             }
             else
             {
                  Console.WriteLine("Error! Queue Underflow");
             }
             return item;
        }

        public Object Front()
        {
             Object item = "";
             if (!isEmpty())
             {
                  item = front.item;
             }
             else
             {
                  Console.WriteLine("Error! Queue Underflow");
             }
             return item;
        }
     }
}

------------------------------------------------------------------------------------------------------------------------------

Driver Program Using Queue Class

using System;

namespace Queue_Linked_List_Implementation
{
     class Program
     {
          static void Main(string[] args)
          {
               Queue queue = new Queue();
               Object fruit;

               queue.enqueue("Apple");  // add apple at the rear of the queue
               queue.enqueue("Banana"); // add banana at the rear of the queue
               queue.enqueue("Grape");  // add grape at the rear of the queue

               Console.WriteLine(queue.Front()); // return the item at the front of the queue

               queue.enqueue("Guava");  // add gauva at the rear of the queue
               queue.enqueue("Orange"); // add orange at the rear of the queue
              
               fruit = queue.dequeue(); // return and remove the item at the front of the queue
               Console.WriteLine(fruit.ToString());
               fruit = queue.dequeue(); // return and remove the item at the front of the queue
               Console.WriteLine(fruit.ToString());
               fruit = queue.dequeue(); // return and remove the item at the front of the queue
               Console.WriteLine(fruit.ToString());


               Console.WriteLine("\nPress any key to  continue...");
               Console.ReadKey();
          }
     }
}

No comments:

Post a Comment