What is Stack in Data Structure?

Stack
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out), for example – a deck of cards or a pile of plates, etc. A real-world stack allows operations at one end only.

Stack example

This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element which is placed (inserted or added) last, is accessed first. In stack terminology, insertion operation is called PUSH operation and removal operation is called POP operation.

Stack Representation

The following diagram depicts a stack and its operations −

Stack Representation

A stack can be implemented by means of Array, Structure, Pointer, and Linked List. Stack can either be a fixed size one or it may have a sense of dynamic resizing. Here, we are going to implement stack using arrays, which makes it a fixed size stack implementation.

The time complexity of stack is O(1).

In this tutorial, I’ll show you how to implement Stack using C++.Well, Stack has basically two main function one is Push() another one is Pop(). Using push function we insert in data in our stack and using pop function we remove or return data from stack.

Push(int val)

void push( int n)
{
    if(top>=n)
    {
        failed();
    }
    else
    {
    int val;
    printf("\nenter value for push : ");
    scanf("%d",&val);
    stack[top] = val;
    top ++;
    success();
    }
}

Pop()

int pop()
{
    if(top==0)
    {
        failed();
    }
    else{
        int temp = stack[top-1];
        top--;
        return temp;
    }
}

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  

About Anisur Rahman Shahin

Hello. My name is Shahin. I'm a tech enthusiast guy. Personally, I’m Optimistic and always in hurry kinda person. I'm a freelance web developer. I am working at Bangla Kings Software as Junior Laravel Developer. My Portfolio website: https://devshahin.com/

View all posts by Anisur Rahman Shahin →

Leave a Reply

Your email address will not be published. Required fields are marked *