Data Structures and Database Applications Queues in C#
Queue Overview System.Collections.Generic.Queue Automatic resizing and homogeneous list Represents a queue of elements
The Queue ADT A queue is another type of list collection. With a queue, insertion is done at one end (the back end), while deletion is performed at the other end (the front end). Accessing the elements of queues follows a First In, First Out (FIFO) order. Queues are analogous to customers standing in a check-out line in a store, the first customer in the line is the first customer served.
The Queue ADT Like Stacks, Queues are less flexible than normal lists. But, once again, they are more efficient and easy to implement Primary operations: Enqueue: insert an element at the rear of the list Dequeue: delete the element at the front of the list Peek: returns the first element without removing it First-in First-out (FIFO) list
Creating and Traversing a Queue Example: Queue<string> numbers = new Queue<string>(); numbers.Enqueue("one"); numbers.Enqueue("two"); numbers.Enqueue("three"); numbers.Enqueue("four"); // A queue can be enumerated without disturbing its contents. Console.Write("There are {0} queue elements", numbers.Count); Console.Write(" in the following order:\n\t"); while (numbers.Peek() != null) { Console.Write(numbers.Dequeue()); Console.Write((numbers.Peek() != null) ? ", " : "\n")); } This prints the following two lines: There are 4 queue elements in the following order: one, two, three, four
Common C# Queue Methods Enqueue() Dequeue() Peek() Clear()
Queue Database Implementation To create an efficient Database implementation of a Queue we will continue using the Tables we used for the Stack and LinkedList operations. We shall also continue to use those same methods we developed to get entries, and to get and set the front and rear of the list, and to clear the list, in order to develop the Queue methods that we need. And, as you probably already guessed, the Dequeue() and Peek() method will have the same code as the Stack’s Pop() and Peek() methods.
Queue Database Implementation The following implements the Enqueue operation using the Entry Data Model and some of the get and set operations for the Linked List.
Queue Database Implementation The following implements the Dequeue operation using the Entry Data Model and some of the get and set operations for the Linked List.
Queue Database Implementation And the following implements the simple Peek operation.