STACK

Iot Lab KIIT
6 min readSep 12, 2022

A stack is a linear data structure in which operations are carried out in a specific order. The sequence could be LIFO (Last In First Out) or FILO (First In Last Out) (First In Last Out).

A stack is considered to be a restricted data structure as only a limited number of operations are allowed. A stack may be fixed in size or may have a dynamic implementation where the size is allowed to change.

Mainly the following basic operations are performed in the stack:

  • Push: Adds an item to the stack. If the stack is full, it is said to be an Overflow condition.
  • Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
  • Peek or Top: Returns the top element of the stack.
  • isEmpty: Returns true if the stack is empty, else false.
  • is full: Returns true if the stack is full, else false.

What is the best way to understand a stack in practice?

There are numerous examples of stacks in the real world. Consider the basic example of plates in a canteen placed on top of one another. The plate at the top is the first to be removed, but the plate at the bottom is the one that stays in the stack the longest. As a result, it can simply be considered as following the LIFO/FILO sequence.

Time Complexities of Stack Operations:

All of the functions push(), pop(), isEmpty(), isFull() and peek() have O(1) time. In none of these operations do we use a loop?

STACK IMPLEMENTATION USING ARRAY

#include<stdio.h>

#include<string.h>

#define MAX 10

int st[MAX];

int top=-1;

void push(int st[],int val);

int pop(int st[]);

int peek(int st[]);

void display(int st[]);

void main()

{

int val,option;

do

{

printf(“\n***MAIN MENU***”);

printf(“\n 1. PUSH”);

printf(“\n 2. POP”);

printf(“\n 3. CHECK IF EMPTY”);

printf(“\n 4. DISPLAY”);

printf(“\n 5. EXIT”);

printf(“\n Enter your option”);

scanf(“%d”,&option);

switch(option)

{

case 1:

printf(“\n Enter the number to be pushed in stack:”);

scanf(“%d”,&val);

push(st,val);

break;

case 2:

val=pop(st);

if(val!=-1)

printf(“\n The deleted element from stack is : %d”,val);

break;

case 3:

empty(st);

break;

case 4:

display(st);

break;

}

}while(option !=5);

}

void push(int st[],int val)

{

if(top==(MAX-1))

printf(“\nStack overflow”);

else

{

top++;

st[top]=val;

}

}

int pop(int st[])

{

if(top==-1)

{

printf(“\nStack underflow”);

return -1;

}

else

{

top — ;

return(st[top+1]);

}

}

void empty(int st[])

{

if(top==-1)

{

printf(“\nStack is empty”);

}

else

printf(“\nStack is not empty”);

}

void display(int st[])

{

int i;

if(top==-1)

{

printf(“\nStack is empty”);

}

else

{

for(i=top;i>=0;i — )

printf(“\n%d”,st[i]);

}

}

OUTPUT

Diagrammatic Explanation:

STACK IMPLEMENTATION USING LINKED LIST

#include <stdio.h>

#include <malloc.h>

struct stack

{

int data;

struct stack * next;

};

struct stack *top = NULL;

struct stack* push(struct stack *, int);

struct stack* display(struct stack *);

struct stack* pop(struct stack *);

int peek(struct stack *);

void empty(struct stack *);

int main()

{

int val, option;

do {

printf(“\n **MAIN MENU**”);

printf(“\n 1. PUSH”);

printf(“\n 2. POP”);

printf(“\n 3. PEEK”);

printf(“\n 4. DISPLAY”);

printf(“\n 5. EXIT”);

printf(“\n Enter your option: “);

scanf(“%d”, &option);

switch (option)

{

case 1:

printf(“\n Enter the number to be pushed on stack: “);

scanf(“%d”, &val);

top = push(top, val);

break;

case 2:

top = pop(top);

break;

case 3:

val = peek(top);

if (val != -1)

printf(“\n The value at the top of the stack is: %d”, val);

else

printf(“\n STACK IS EMPTY”);

break;

case 4:

top = display(top);

break;

}

} while (option != 5);

return 0;

}

struct stack* push(struct stack *top, int val)

{

struct stack * ptr;

ptr = (struct stack *) malloc(sizeof(struct stack));

ptr->data = val;

if (top == NULL)

{

ptr->next = NULL;

top = ptr;

}

else

{

ptr->next = top;

top = ptr;

}

return top;

}

struct stack* display(struct stack *top)

{

struct stack * ptr;

ptr = top;

if (top == NULL)

printf(“\n STACK IS EMPTY”);

else

{

while (ptr != NULL)

{

printf(“\n %d”, ptr->data);

ptr = ptr->next;

}

}

return top;

}

struct stack* pop(struct stack *top)

{

struct stack * ptr;

ptr = top;

if (top == NULL)

printf(“\n STACK UNDERFLOW”);

else

{

top = top->next;

printf(“\n The value being deleted is: %d”, ptr->data);

free(ptr);

}

return top;

}

int peek(struct stack *top)

{

if (top == NULL)

return -1;

else

return top->data;

}

void empty(struct stack *top)

{

if(top==NULL)

printf(“Stack is empty\n”);

else

printf(“Stack is not empty\n”);

}

OUTPUT

Diagrammatic Explanation:

LEETCODE EXAMPLE

https://leetcode.com/problems/baseball-game/submissions/

You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds’ scores.

At the beginning of the game, you start with an empty record. You are given a list of strings ops, where ops[i] is the ith operation you must apply to the record and is one of the following:

  1. An integer x — Record a new score of x.
  2. “+” — Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
  3. “D” — Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
  4. “C” — Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.

Return the sum of all the scores on the record.

Example 1:

Input: ops = [“5”,”2",”C”,”D”,”+”]

Output: 30

Explanation:

“5” — Add 5 to the record, the record is now [5].

“2” — Add 2 to the record, the record is now [5, 2].

“C” — Invalidate and remove the previous score, the record is now [5].

“D” — Add 2 * 5 = 10 to the record, record is now [5, 10].

“+” — Add 5 + 10 = 15 to the record, record is now [5, 10, 15].

The total sum is 5 + 10 + 15 = 30.

class Solution {

public:

int calPoints(vector<string>& ops) {

stack<int> st;

int a, b, sum = 0;

for(auto i = ops.begin(); i != ops.end(); i++) {

if(*i == “C”){

st.pop();

}

else if(*i == “D”) {

a = st.top();

a *= 2;

st.push(a);

}

else if(*i == “+”) {

a = st.top();

st.pop();

b = st.top();

st.pop();

st.push(b);

st.push(a);

a += b;

st.push(a);

}

else {

stringstream x(*i);

x >> a;

st.push(a);

}

}

while(!st.empty()) {

sum += st.top();

st.pop();

}

return sum;

}

};

--

--