Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "queue.h"
- using namespace std;
- //---------------------------------------------------------------------------------------------------
- // initQ
- //---------------------------------------------------------------------------------------------------
- // Given a queue, initializes it to empty
- void initQ(queue & q) {
- q.head = NULL;
- q.tail = NULL;
- } // initQ()
- //---------------------------------------------------------------------------------------------------
- // printQ
- //---------------------------------------------------------------------------------------------------
- void printQ(queue & q) {
- node * p = q.head;
- node * z = q.tail;
- cout << "QUEUE: ";
- if (p == NULL) {
- cout << "Empty" << endl;
- }
- else {
- while (z != NULL) {
- cout << z->key << " ";
- z = z->next;
- }
- }
- if (p != NULL) {
- cout << "TAIL= " << p->key << " ";
- }
- else {
- cout << "TAIL= EMPTY";
- }
- } // printQ()
- //---------------------------------------------------------------------------------------------------
- // enQ
- //---------------------------------------------------------------------------------------------------
- // Given a Q, pops the top, prints the item popped, and prints the stack
- void enQ(queue & q, int key) {
- node * n = new node;
- node * d = new node;
- n->key = key;
- n->next = NULL;
- if (q.head == NULL) {
- q.head = n;
- q.tail = n;
- }
- else {
- q.head->next = n;
- q.head = n;
- }
- }
- // enQ()
- //---------------------------------------------------------------------------------------------------
- // deQ
- //---------------------------------------------------------------------------------------------------
- // Given a Q, deQ's the next item and returns it. Leaves no Garbage.
- int deQ(queue & q) {
- if (q.head != NULL) {
- node * c = new node;
- c = q.tail;
- q.tail = q.tail->next;
- return c->key;
- }
- else {
- return 0;
- }
- } // deQ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement