Linked List Implementation of Queue // Class declaration file: Queue.h // Linked List implementation of queue ADT. #ifndef QUEUE_H #include "LinkedList.h" class queue: protected LinkedList { public: // Class constructors queue(); queue(const queue &q); // Member functions bool isEmpty(); void enqueue(const elementType &item); elementType dequeue(); protected: // Data members nodePtr tail; }; #define QUEUE_H #endif CS 240 // Class implementation file: Queue.cpp // Linked List implementation of the queue ADT. #include "Queue.h" #include // Default constructor: Makes an empty queue // queue:: queue(): LinkedList() { tail = NULL; } The queue class “inherits” from the LinkedList class, so all LinkedList members are accessible to any queue. With its “protected” access specifier, the public and protected members of LinkedList are considered protected in the queue class. Let’s assume that the getNode and head members in LinkedList were declared protected, not private! Let’s also assume that the elementType typedef occurred in the LinkedList definition! 6
Linked List Implementation of Queue // Class declaration file: Queue.h // Linked List implementation of queue ADT. #ifndef QUEUE_H #include "LinkedList.h" #include using namespace std; class queue: protected LinkedList { public: // Class constructors queue(); queue(const queue &q); // Class implementation file: Queue.cpp // Linked List implementation of the queue ADT. #include "queue.h" #include #include using namespace std; // Default constructor: Makes an empty queue // queue:: queue(): LinkedList() { tail = NULL; } The Thequeue queueclass class“inherits” “inherits” from the LinkedList from the LinkedListclass, class,so so all LinkedList members are // Member functions all LinkedList members are accessible bool isEmpty(); accessibleto toany anyqueue. queue. void enqueue(const elementType &item); With Withits its“protected” “protected”access access elementType dequeue(); specifier, the public specifier, the publicand and protected members protected membersof of protected: LinkedList are considered // Data members LinkedList are considered protected nodePtr tail; protectedin inthe thequeue queue }; class. class. Let’sassume assumethat thatthe thegetNode getNodeand andhead head Let’s #define QUEUE_H membersin inLinkedList LinkedListwere weredeclared declaredprotected, protected, #endif members notprivate! private! not Let’salso alsoassume assumethat thatthe theelementType elementTypetypedef typedef Let’s occurredin inthe theLinkedList LinkedListdefinition! definition! occurred CS 240 Chapter 7 - Queues Page 5
CROSS JOIN Student ID Name 123 124 125 126 John Mary Mark Jane Enrolment ID Code 123 124 124 126 DBS PRG DBS PRG SELECT * FROM Student CROSS JOIN Enrolment ID Name ID Code 123 124 125 126 123 124 125 126 123 124 John Mary Mark Jane John Mary Mark Jane John Mary 123 123 123 123 124 124 124 124 124 124 DBS DBS DBS DBS PRG PRG PRG PRG DBS DBS