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

Pointers

Pointers: A pointer is a variable whose value is a memory address. A pointer contains the memory address of a variable that, in turn, contains a specific value. In this sense, a variable name directly references a value, and a pointer indirectly references a value. Syntax: type * variable ; Interpretation: The value of the pointer variable ptr is a memory address. A data item whose address is stored in this variable must be of the specified type. Dynamic Memory Management: C/C++ enables programmers to control the allocation and deallocation of memory in a program for any built in or user defined type. The ability for a program to obtain more memory space at execution time to hold new nodes, and to release space no longer needed is known as dynamic memory management. Syntax: int *num = (int *)malloc(sizeof (int)*numCount); or int *ptr = (int *)calloc(numCount, sizeof (int)); /* returns a pointer to a section of memory just large enough to hold the integers, whose q...

Single LinkList in Java

Linked List: Linked List contains a sequence nodes which are linked together. Each node contains a connection to another link and data. Linked list is the second most-used data structure after array. Following are the important terms to understand the concept of Linked List. Link − Each link of a linked list can store a data called an element. Next − Each link of a linked list contains a link to the next link called Next. LinkedList − A Linked List contains the connection link to the first link called First. Types of Linked List: Following are the various types of linked list. Simple Linked List − Item navigation is forward only. Doubly Linked List − Items can be navigated forward and backward. Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous. Basic Operations: Insert:  Inserts at tail,  specific index. Delete: Deletes from the tail.  specific index. ...

Object Oriented Programming (OOP)

Object Oriented Programming: Object Oriented Programming (OOP) is a programming concept which used in the modern programming world. Languages like Java, C++,  and Python support Object Oriented Programming (OOP). It works on the principle that objects are the most important part of a program. In OOP we think in terms of objects and every object has its attributes (properties) and a state (behavior/functions). Object Oriented Programming (OOP) is a technique of system modeling and its main purpose is to understand the product before developing it and manipulating these objects to achieve a specific task.   Pillars of Object Oriented Language (OOP): There are four basic principles of Object Oriented Language (OOP). Inheritance Polymorphism Data Encapsulation Abstraction WHY is Object Oriented Language (OOP) NEEDED? Problems with Procedural Languages: Functions have unrestricted access to global data Unrelated Functions and data. Before Object Oriente...