Let us understand how to prepare queue using two stacks When we say we have to prepare queue using stack that means all the operations of stack should be used to get the queue functionality. Queue has FIFO property i.e. First-In First-Out i.e. element which is inserted first , same element should be removed or pop first. Consider that we have two stacks s1 and s2. Initially both the stacks are empty. Here we have used Stack Using Linked List. When we push any data in to stack, we have…
Category: Stack
Stack Using Linked List
Let us see in the below program how stack can be implemented using Linked List. #include <iostream>using namespace std;typedef struct node{ int data; struct node* next; }Node;class stack{private: Node* head;public: stack() { head = NULL; } ~stack() { while (NULL != head) { Node* temp = head; head = head ->next; delete temp; } } void push(int data) { Node* temp = new Node; temp->data = data; if (NULL == head) { head = temp; head->next = NULL; return; } temp->next = head; head = temp; } int pop() {…
Stack using fixed size array
Stack is one of the data structure that uses LIFO (Last In First Out) or FILO (First In Last Out) property i.e. element which is stored in last, will be out (read) first.Real life example of stack is stack of plates. Last plate put over the plates is the first plate that is removed or used. #include <iostream>using namespace std;const int MAX_INDEX = 50; // Maximum index of array.const int MIN_INDEX = -1; // Minimum index of array.class stack{private: unsigned int arr[MAX_INDEX]; int top;public: stack() { top = MIN_INDEX; }…