1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
/****************************************************************************************** ******************************************************************************************* Chapter 3 Stack and Queue Implement a MyQueue class which implements a queue using two stacks. We use two stacks, s1 and s2 for enqueue and dequeue. By: Hamed Kiani (Sep. 7, 2015) ****************************************************************************************** ******************************************************************************************/ #include "stdafx.h" #include <iostream> using namespace std; struct Node{ int data; Node *next; }; class MyQueue{ private: Node *_s1_head; // enQueue is done using s1 Node *_s2_head; // s2 is used for dequeue int _s1_size; // the size of s1 stack int _s2_size; // the size of s2 stack public: MyQueue(); ~MyQueue(); void enQueue(int data); // enqueue data value using s1 Node* deQueue(); // dequeue using s1 and s2 void print_s1(); // print elements of s1 stack void print_s2(); // print elements of s2 stack bool isEmpty(Node *); // check if s1 or s2 are empty int get_s1_size(){return _s1_size;} // return size of s1 int get_s2_size(){return _s2_size;} // return size of s2 }; //////////////////////////////////////////////////////////////////// MyQueue::MyQueue() { _s1_head = NULL; _s2_head = NULL; _s1_size = 0; _s2_size = 0; cout << "the stack and queue are initialized! " << endl; } //////////////////////////////////////////////////////////////////// MyQueue::~MyQueue() { Node *temp; while(_s1_head != NULL) { temp = _s1_head; _s1_head = _s1_head->next; delete temp; } while(_s2_head != NULL) { temp = _s2_head; _s2_head = _s2_head->next; delete temp; } cout << "the stack and queue are deleted! " << endl; } //////////////////////////////////////////////////////////////////// // enQueue data at begin of s1 (push s1 stack) void MyQueue::enQueue(int data) { Node *temp = new Node; temp->data = data; temp->next = _s1_head; _s1_head = temp; _s1_size++; cout << " the enQueue value is: " << data << endl; } //////////////////////////////////////////////////////////////////// // For deQueue, // we copy all from s1 to ss except the last node of s1 O(n) // re-copy s2 to s1 in reverse order O(n) // update the s1_size and s2_size Node* MyQueue::deQueue() { // the queue is empty and dequeue is not possible if ((_s1_size == 0) ) { cout<< "dequeue is not possible !" << endl; return NULL; } // there is just one element in queue if ((_s1_size == 1) ) { Node * temp = _s1_head; _s1_head = NULL; _s1_size--; cout << " the dequeue value is: " << temp->data << endl; return temp; } // there are more than one element in the queue _s2_head = _s1_head; _s1_head = NULL; Node * temp; temp = _s2_head; _s1_size--; while(temp->next->next != NULL) { temp = temp->next; } Node * temp2; temp2 = temp->next; temp->next = NULL; _s1_head = _s2_head; _s2_head = NULL; cout << " the dequeue value is: " << temp2->data << endl; return temp2; } //////////////////////////////////////////////////////////////////// void MyQueue::print_s1() { if (_s1_head == NULL) { cout << " stack s1 is empty" << endl; return; } Node* temp; temp = _s1_head; while(temp) { cout << temp->data << " " ; temp = temp->next; } cout << endl; } ////////////////////////////////////////////////////////// void MyQueue::print_s2() { if (_s2_head == NULL) { cout << " stack s2 is empty" << endl; return; } Node* temp; temp = _s2_head; while(temp) { cout << temp->data << " " ; temp = temp->next; } cout << endl; } //////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////// int _tmain(int argc, _TCHAR* argv[]) { MyQueue queue; queue.enQueue(1); cout << queue.get_s1_size() << endl; cout << queue.get_s2_size() << endl; queue.print_s1(); queue.print_s2(); Node * temp = queue.deQueue(); temp = queue.deQueue(); temp = queue.deQueue(); temp = queue.deQueue(); temp = queue.deQueue(); queue.print_s1(); queue.print_s2(); queue.enQueue(3); queue.enQueue(4); queue.print_s1(); queue.print_s2(); return 0; } |
0 Comments
Leave a Reply. |
A place to practice the coding interview.
AuthorHamed Kiani Categories
All
Archives |