470 likes | 494 Views
Understand the implementation and applications of stacks and queues using arrays, with detailed explanations and code snippets. Learn about push, pop, enqueue, dequeue operations, and key functions provided by the Stack and Queue classes.
E N D
Stack • A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. • The last entry which is inserted is the first one that will be removed. (Last In First Out : LIFO)
Application of Stack • Page visited history in a Web-browser • Undo sequence in a text editor • Program run-time environment
Pushing and popping a stack • Push box A onto the stack • Push box B onto the stack • Pop a box from the stack • Pop a box from the stack • Push box C onto the stack • Push box D onto the stack • Push box E onto the stack • Push box F onto the stack
Implementation of Stack • Some Basic Operations for Stacks • Create a stack, leaving it empty. • Test whether the stack is Empty. • Retrieve the Top entry from the stack, provided the stack is not empty. • Pop the entry of the top of the stack, provided the stack is not empty. • Push a new entry onto the top off the stack, provided that the stack is not full.
Basic Operations for Stacks • Create a stack, leaving it empty. • Test whether the stack is Empty. • Test whether the stack is Full. • Return the Size (number of entries) of the stack. • Retrieve the Top entry from the stack, provided the stack is not empty. • Pop the entry off the top of the stack, provided the stack is not empty. • Push a new entry onto the top of the stack, provided that the stack is not full. • Print all the entries of the stack.
The Stack Class • Methods: • Stack (constructor) • empty • full • size • top • pop • push • print • Data members
Stack Implementation - Array typedef double Stack_entry; const int max = 100; class Stack { public: Stack(); bool empty(); bool full(); int size(); bool top(Stack_entry &item); bool pop(); bool push(Stack_entry item); void print(); private: int count; Stack_entry entry[max]; };
Create Stack • We use a constructor to initialize a new created stack as empty. Stack::Stack() // O(1) // Initialize Stack to be empty { count = 0; }
Empty bool Stack::empty() // A result of true is returned if the Stack // is empty, otherwise false is returned. { return count == 0; }
Full bool Stack::full() // A result of true is returned if the Stack // is full, otherwise false is returned. { return count == max; }
Size int Stack::size() // return the # of entries in the stack. { return count; }
Top bool Stack::top(Stack_entry &item) // The top of a nonempty Stack is copied to // item. A code of underflow is returned if the // Stack is empty. { if(empty()) return false; item = entry[counter-1]; return true; }
Pop bool Stack::pop() // If the Stack is not empty, the top of the // Stack is removed. If the Stack is empty, an // Error code of underflow is returned and the // Stack is left unchanged. { if(empty()) return false; count--; return true; }
Push bool Stack::push(Stack_entry item) // If the Stack is not full, item is added // to the top of the Stack. If the Stack is full, // an Error code of overflow is returned and the // Stack is left unchanged. { if(full()) return false; entry[count++] = item; return true; }
Print void Stack::print() // Display the entries of the Stack. { if (empty()) cout << "Empty stack"; else{ cout << endl << "The Stack is: "; for (int i=count-1; i>=0; i--) cout << entry[i] << " "; } cout << endl; }
Queue • A data structure modeled after a line of people waiting to be served. • The first entry which is inserted is the first one that will be removed. (First In First Out : FIFO)
The Queue Class • Functions: • Queue (constructor) • clear • empty • full • size • retrieve • dequeue • enqueue • print • Data members
Queue Implementation - Array typedef double Queue_entry; const int max = 100; class Queue { public: Queue(); void clear(); bool empty(); bool full(); int size(); bool retrieve(Queue_entry &item); bool dequeue(); bool enqueue(Queue_entry item); void print(); private: int count; Queue_entry entry[max]; };
Create Queue • We use a constructor to initialize a new created queue as empty. Queue::Queue() // O(1) { count = 0; }
Clear Queue::clear() // O(1) { count = 0; }
Empty bool Queue::empty() { return count == 0; }
Full bool Queue::full() { return count == max; }
Size int Queue::size() { return count; }
Retrieve bool Queue::retrieve(Queue_entry &item) { if(empty()) return false; item = entry[0]; return true; }
Dequeue bool Queue::dequeue() // O(n) { if (empty()) return false; count--; for(int i = 0; i < count; i++) entry[i] = entry[i+1]; return true; }
Enqueue bool Queue::enqueue(Queue_entry item) // O(1) { if(full()) return false; entry[count] = item; count++; return true; }
Print void Queue::print() { for (int i=0; i<count; i++) cout << entry[i] << " "; }
Create Queue Queue::Queue() { front = 0; rear = -1; }
Clear Queue::clear() { rear = -1; }
Empty bool Queue::empty() { return rear == -1; }
Next int Queue::next(int n) { return ((n+1)==max) ? 0 : (n+1); }
Full bool Queue::full() { return (rear!=-1) && (next(rear)==front); }
Size int Queue::size() { int s; if (empty()) s = 0; else{ s = rear-front+1; if (front>rear) s = max + s; } return s; }
Retrieve bool Queue::retrieve(Queue_entry &item) { if (empty()) return false; item = entry[front]; return true; }
Dequeue bool Queue::dequeue() { if (empty()) return false; if (front==rear) // one element rear = -1; else front = next(front); return true; }
Enqueue bool Queue::enqueue(Queue_entry item) { if (full()) return false; if (empty()) rear = front; else rear = next(rear); entry[rear] = item; return true; }
Print void Queue::print() { if (empty()) cout << "The queue is empty" << endl; else{ cout << endl << "The Queue is: "; int i = front; do{ cout << entry[i] << " "; i = next(i); } while (i != next(rear)); } cout << endl; }