Stack using Queue in c++

 

Stack and queue are two important data structures that are widely used in computer science and software development. Both of these data structures have their own unique features and are used in various applications. In this article, we will take a closer look at how to use stack and queue in C++.

A stack is a data structure that follows the Last In First Out (LIFO) principle. This means that the last element added to the stack is the first one to be removed. Stacks are often used in applications such as undo/redo functionality, backtracking, and parsing expressions. In C++, we can implement a stack using an array or a linked list.

To implement a stack using an array, we first need to declare an array of a certain size and two variables, top and maxSize. The top variable keeps track of the top element of the stack and maxSize is the maximum size of the stack. We also need to define the push, pop, and peek functions. The push function is used to add an element to the stack, the pop function is used to remove an element from the stack, and the peek function is used to view the top element of the stack without removing it.

Algorithm for Stack using Array in C++:

  1. Declare an array, top and maxSize variables.
  2. Initialize the top variable to -1 and the maxSize variable to the maximum size of the stack.
  3. Define the push function, which takes in an element as a parameter.
  4. Check if the top variable is equal to maxSize - 1. If it is, print "Stack overflow" and return.
  5. Otherwise, add the element to the stack by incrementing the top variable and assigning the element to the top index of the array.
  6. Define the pop function, which does not take in any parameters.
  7. Check if the top variable is equal to -1. If it is, print "Stack underflow" and return -1.
  8. Otherwise, remove the top element of the stack by decrementing the top variable and returning the element at the top index of the array.
  9. Define the peek function, which also does not take in any parameters.
  10. Check if the top variable is equal to -1. If it is, print "Stack is empty" and return -1.
  11. Otherwise, return the element at the top index of the array without removing it.

Algorithm for Queue using Array in C++:

  1. Declare an array, front and rear variables.
  2. Initialize the front variable to 0 and the rear variable to -1.
  3. Define the enqueue function, which takes in an element as a parameter.
  4. Check if the rear variable is equal to maxSize - 1. If it is, print "Queue overflow" and return.
  5. Otherwise, add the element to the queue by incrementing the rear variable and assigning the element to the rear index of the array.
  6. Define the dequeue function, which does not take in any parameters.
  7. Check if the front variable is greater than the rear variable. If it is, print "Queue underflow" and return -1.
  8. Otherwise, remove the front element of the queue by incrementing the front variable and returning the element at the front index of the array.
  9. Define the peek function, which also does not take in any parameters.
  10. Check if the front variable is greater than the rear variable. If it is, print "Queue is empty" and return -1.
  11. Otherwise, return the element at the front index of the array without removing it.

 

Here is an example of a stack implementation using an array in C++:

#include <iostream> 
 using namespace std;
 const int MAX_SIZE = 100;
 class Stack
 private:
 int top;
 int maxSize; 
 int arr[MAX_SIZE];
 public:
 Stack() { 
 top = -1
 maxSize = MAX_SIZE;
 } 
 void push(int data) {
 if (top == maxSize - 1) { 
 cout << "Stack overflow" << endl;
 return;
 } 
 arr[++top] = data; 
}
 int pop() {
 if (top == -1) {
 cout << "Stack underflow" << endl;
 return -1;
 } 
 return arr[top--];
 } 
 int peek() {
 if (top == -1) {
 cout << "Stack is empty" << endl; 
 return -1;
 }
 return arr[top];
 } };

On the other hand, a queue is a data structure that follows the First In First Out (FIFO) principle. This means that the first element added to the queue is the first one to be removed. Queues are often used in applications such as scheduling, task management, and message passing. In C++, we can implement a queue using an array or a linked list.

To implement a queue using an array, we first need to declare an array of a certain size and two variables, front and rear. The front variable keeps track of the front element of the queue and rear is the last element of the queue. We also need to define the enqueue, dequeue, and peek functions. The enqueue function is used to add an element to the queue, the dequeue function is used to remove an element from the queue, and the peek function is used to view the front element of the queue without removing it.

Stack using Queue in c++