file: 03.2.theQueueADT.txt author: Bob Muller date: January 30, 2014 CS102 Computer Science 102 Spring 2014 Lecture Notes for Meeting 2 of Week 3 Topics: 0. Queues -- first-in-first-out data structures ----------------------------------------------------------------- Notes: 0. A useful quadrangle taxonomy of data structures. This doesn't cover everything but it informs many. Representation Sequential Linked +-------------------+--------------------+ M | | | u | ResizingArray | Linked | t Mutable | Stack/Queue | Stack/Queue | a | | | b | | | i +-------------------+--------------------+ l | | | i | | | t Immutable | String | Lists/Sequences | y | | | | | | +-------------------+--------------------+ ----------------------------------------------------------------- 0. Queues -- first-in-first-out data structures Queues are very simple and flexible data structures that obey a first-in-first-out (FIFO) discipline. op queue - enq(A) A // A is the back and front of queue enq(B) A B // A is in front, B is in back enq(C) A B C deq() A <== B C // B is now in front. For a mutable linked implementation it will be useful to retain queue elements in Nodes: +---+---+ +---+---+ +---+---+ | A | o-+---->| B | o-+---->| C | o-+--+ +---+---+ +---+---+ +---+---+ = and to maintain a header-node with 3 fields: +-------------+ | +---+ | | front | o-+-+--> to Node holding A | +---+ | | back | o-+-+--> to Node holding C | +---+ | | N | 3 | | +-------+---+-+ See 03.2.Queue.zip for the code. We also discussed a sequential implementation using resizing arrays. We found it convenient to organize the array as a circular structure. See: http://algs4.cs.princeton.edu/13stacks/ResizingArrayQueue.java.html