Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "queue.h"
- #include "node.h"
- using namespace std;
- //---------------------------------------------------------------------------------------------------
- // initQ
- //---------------------------------------------------------------------------------------------------
- // Given a queue, initializes it to empty
- void initQ(queue & q) {
- q.head = NULL;
- q.tail = NULL;
- } // initQ()
- //---------------------------------------------------------------------------------------------------
- // printQ
- //---------------------------------------------------------------------------------------------------
- // Given a Q, prints the word QUEUE followed by the keys of all nodes in the queue. In addition, (re)print
- // the key of the node pointed to by the tail pointer.
- void printQ(queue & q) {
- cout << "Queue ";
- node * p = q.head;
- while (p != NULL) {
- cout << p->key << " ";
- p = p->next;
- }
- //re-print the key of the node pointed to by the tail pointer.
- if (q.tail == NULL) {
- cout << "Tail = NULL" << endl;
- }
- else {
- cout << "Tail=" << q.tail->key << endl;
- }
- } // printQ()
- //---------------------------------------------------------------------------------------------------
- // enQ
- //---------------------------------------------------------------------------------------------------
- // Given a Q, pops the top, prints the item popped, and prints the stack
- //
- // Given a Q, and a key for a new node: allocate a new node and populate the key member with the
- // argument passed.Insert the new node at the tail of the current Q. Be sure to update the tail pointer… it
- // should point to the newly-allocated node.Be sure the code works for “empty Q”, a Q of 1 node, and a Q
- // of 2 or more nodes.
- void enQ(queue & q, int key) {
- // Case one is when the there is no element, case two if there is only one element,
- // and case three if there is two or more elements
- node *last = NULL, *current = NULL;
- node* n = new node;
- n->next = NULL;
- n->key = key;
- current = q.head;
- if (q.head != NULL) {
- while (current->next != NULL) {
- last = current;
- current = current->next;
- }
- if (current->next == NULL) {
- q.tail = n;
- current->next = q.tail;
- }
- else {
- last->next = n;
- n->next = current;
- }
- }
- else {
- q.head = n;
- n->next = NULL;
- }
- } // enQ()
- //---------------------------------------------------------------------------------------------------
- // deQ
- //---------------------------------------------------------------------------------------------------
- // Given a Q, deQ's the next item and returns it. Leaves no Garbage.
- //
- // Given a Q, remove the head node(save its key) and deallocate it. Return the key. When the Q is
- // empty, return INT_MAX(a constant defined in <iostream>).Be sure this works for an empty Q, a Q of
- // length 1, and a queue of length 2 or more.
- int deQ(queue & q) {
- //Case one is when the there is no element, case two if there is only one element,
- //and case three if there is two or more elements
- node* n = q.head; //current is the first
- int value;
- if (q.head == NULL) { //so if current is empty, do something(return will be needed)
- return INT_MAX;
- }
- else if (q.head->next == NULL) {
- value = n->key;
- delete n;
- q.head = q.tail = NULL;
- }
- else {
- value = n->key;
- q.head = n->next;
- delete n;
- }
- return value;
- } // deQ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement