Skip to main content

Stack Data Structure in Cpp (c++)

 What is Stack?

Stack refers to an abstract data types (ADTs) that is a collection of elements, with two principal operations that are addition and removal of elements from the collection. It works on the principle of LIFO (last In first out). It is also considered as a linear data structure.


 

Example: A set of books. It easy to take an item off the top rather from the middle.



Basic operations of Stack:

  • Push: Add item to the list.
  • Pop: Removes item from the list.
 

Advantages:

Stack is faster than heap memory allocation (also known as Dynamic Memory Allocation). Stack memory is automatically reclaimed when the function exits, which can be convenient for the programmer if the data is no longer required. Therefore, it is suitable for temporary data, where memory is not needed after the execution of a function.

Implementation Of Stack in C++:


#include<iostream>

using namespace std;

class StackMemory
{
 private:
  int arr[10];
     int top;
  
 public:
         
  StackMemory()
  {
   top=0;
  }
 
  void push ()
  {
      int num;
      if (top==(10 - 1))
      {
          cout<<"Memory is Full"<<endl;
 
      }
      else
      {
          cout<<"Enter The Element To Be Pushed: "<<endl;
          cin>>num;
          arr[top]=num;
          top++;
      }

  }

  void pop ()
  {
      int num;
      if (top <0)
      {
          cout<<"Stack is Empty:"<<endl;
         
      }
      else
      {
          cout<<"POP Element: = "<<arr[0]<<endl;
          for(int x=0;x<top;x++)
          arr[x]=arr[x+1];
          
          top--;
          
      }
     
  }

  void display ()
  {
      if (top== -1)
      {
                 cout<<"Stack is empty"<<endl;
        
      }
      else
      { 
          cout<<"The status is: "<<endl;
          for (int x=0; x<top;x++)
          {
              cout<<arr[x]<<endl;
          }
      }
     }

};
 
int main()
{
    int ch;
    int op = 1;
 StackMemory one;
  
    cout<<"OPERATION:"<<endl;
    while (op!=0)
    {
        cout<<"Enter 1 To PUSH "<<endl;
        cout<<"Enter 2 To POP "<<endl;
        cout<<"Enter 3 To DISPLAY "<<endl;
        cout<<"Enter 4 To EXIT: ";
        cin>>ch;
        switch (ch)
        {
         case 1:
             one.push();
             break;
         case 2:
             one.pop();
             break;
         case 3:
             one.display();
             break;
         case 4:
             return 0;
        }
        cout<<"Do you want to continue(0 or 1 ): ";
        cin>>op;
        cout<<endl;
    }
}

Comments

Popular posts from this blog

Data Types

DATA TYPES in C/C++: Since C and C++ are strongly type languages, therefore every thing must be defined. Variables are classified according to their data type, which determines the kind of information that may be stored in them. Data Types Description C-Keyword Format Specifier integer Integers are whole numbers that can have both positive and negative values but no decimal values.   int   %d or %i Float Floating type variables can hold real numbers precision of 6 digits. float %f Double can hold real numbers with the precision of 14 digits. double %f Character data type allows a variable to store only one character. char %c Implementation in C: #include<stdio.h> int main() { printf(“%d n”,18); //this will print integer printf(“%fn”,32.56); //this will print decimal number printf(“%d %d %dn”,32,56,89); // %d is a place holder for integer and it provide place for one integer printf(“%ld...

Tic-Tac-Toe Implementation in Cpp (C++) Step by Step

Conditional Statement

Conditional Statement: A conditional statement is an if-then statement which means that if some statement is true then it follows the if-block of code and if the condition is false then it follows the else-block of code. In the programming world, there are two types of conditional statement namely if-else and switch. if-else Statement: Syntax: if (condition 1) { } else if (condition 2) { } else { }   Implementation of If-else statement in C: Q: Write a program to find the greatest of three numbers. #include<stdio.h> int main() { int a,b,c; printf(“Please Enter First Number “); scanf(“%d”,&a); printf(“Please Enter Second Number “); scanf(“%d”,&b); printf(“Please Enter Third Number “); scanf(“%d”,&c); if (a>b && a>c) { printf(“The Greatest Number is %d”,a); } else if (b>c) { printf(“The Greatest Number is %d”,b); } else { pr...