You are on page 1of 16

STACK

DATA STRUCTURE
Ankit Chauhan
(21SCSE1040134)

Amit Kumar
(21SCSE1040072)

Ankur Kumar
(21SCSE1040080)
STACK DATA STRUCTURE
Definition

A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle.


Stack has one end, It contains only one pointer top pointer pointing to the topmost
element of the stack. Whenever an element is added in the stack, it is added on the top
of the stack, and the element can be deleted only from the stack. In other words,
“a stack can be defined as a container in which insertion and deletion can be done from the one end known as
the top of the stack.”

ref. on-

2
STACK DATA STRUCTURE
Operations on stack

The following are some common operations implemented on the stack:


● push(): When we insert an element in a stack then the operation is known as a push. If the stack is full then the overflow condition
occurs.
● pop(): When we delete an element from the stack, the operation is known as a pop. If the stack is empty means that no element exists
in the stack, this state is known as an underflow state.
● isEmpty(): It determines whether the stack is empty or not.
● isFull(): It determines whether the stack is full or not.'
● peek(): It returns the element at the given position.
● count(): It returns the total number of elements available in a stack.
● change(): It changes the element at the given position.
● display(): It prints all the elements available in the stack.
3
Memory Representation of a STACK as an ARRAY

(0) (1) (2) (3) (4)

85 63 5 15 10
85
TOP
ARRAY
63
5
15 We are given a stack of elements: 85,63,5,15,10.
10
STACK

4
* According to a Survey
Memory Representation of a STACK as an Linked-
list

5
Memory Representation of a STACK as an Linked-
list

6
Memory Representation of a STACK as an Linked-
list

7
CODE TO IMPLEMENT TWO STACK IN ONE
ARRAY
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10

struct stack
{
int stk[MAXSIZE];
int top;
int top2;
};
typedef struct stack STACK;
STACK s;

void push(void);
void push2(void);
int pop(void);
int pop2(void);
void display(void);
void display2(void);

8
CODE TO IMPLEMENT TWO STACK IN ONE
ARRAY
void main ()
{
int choice;
int option = 1;
s.top = -1;
s.top2 = MAXSIZE;

printf ("STACK OPERATION\n");


while (option)
{
printf ("******************************************\n");
printf ("------------------------------------------\n");
printf (" press 1 for PUSH 1 STACK \n");
printf (" press 2 for PUSH 2 STACK \n");
printf (" press 3 for POP 1 STACK \n");
printf (" press 4 for POP 2 STACK \n");
printf (" press 5 for PEEK 1 STACK \n");
printf (" press 6 for PEEK 2 STACK \n");
printf (" 7 --> EXIT \n");
printf ("------------------------------------------\n");
printf ("******************************************\n");

printf ("Enter your choice\n");


scanf ("%d", &choice); 9
CODE TO IMPLEMENT TWO STACK IN ONE
ARRAY
switch (choice)
{
case 1:
push();
break;
case 2:
push2();
break;
case 3:
pop();
break;
case 4:
pop2();
break;
case 5:
display();
break;
case 6:
display2();
break;
case 7:
exit(0);
break;
default:
printf("sorry incorrect credentials !!");
}
fflush (stdin);
printf ("Do you want to continue(Type 0 or 1)?\n");
scanf ("%d", &option);
}
}
10
CODE TO IMPLEMENT TWO STACK IN ONE
ARRAY
/* Function to add an element to the stack 1*/
void push ()
{
int num;
if (s.top == (s.top2 - 1))
{
printf ("Stack1 is Full\n");
return;
}
else
{
printf ("Enter the element to be pushed\n");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}
/* function to add element in stack 2*/
void push2 ()
{
int num;
if (s.top == (s.top2 - 1))
{
printf ("Stack2 is Full\n");
return;
}
else
{
printf ("Enter the element to be pushed\n");
scanf ("%d", &num);
s.top2 = s.top2 - 1;
s.stk[s.top2] = num;
}
return;
} 11
CODE TO IMPLEMENT TWO STACK IN ONE
ARRAY
/* Function to delete an element from the stack 1*/
int pop ()
{
int num;
if (s.top == - 1)
{
printf ("Stack1 is UNDERFLOW\n");
return (s.top);
}
else
{
num = s.stk[s.top];
printf ("poped element is = %dn", s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}
/* Function to delete an element from the stack 2*/
int pop2 ()
{
int num;
if (s.top2 < MAXSIZE)
{
printf ("Stack2 is UNDEFRFLOW\n");
return (s.top2);
}
else
{
num = s.stk[s.top2];
printf ("poped element is = %dn", s.stk[s.top2]);
s.top2 = s.top2 - 1;
}
return(num);
}
12
CODE TO IMPLEMENT TWO STACK IN ONE
ARRAY
/* Function to display the status of the stack 1*/
void display ()
{
int i;
if (s.top == -1)
{
printf ("Stack is empty\n");
return;
}
else
{
printf ("\n The status of the stack is \n");
for (i = s.top; i >= 0; i--)
{
printf ("%d\n", s.stk[i]);
}
}
printf ("\n");
}
/* Function to display the status of the stack 2*/
void display2 ()
{
int i;
if (s.top2 == MAXSIZE)
{
printf ("Stack is empty\n");
return;
}
else
{
printf ("\n The status of the stack is \n");
for (i = s.top2; i < MAXSIZE; ++i)
{
printf ("%d\n", s.stk[i]);
}
}
printf ("\n");
} 13
OUTPUT

14
REAL LIFE EXAMPLE OF TWO STACK IN ONE
ARRAY
Consider a scenario where a little boy has a box, and boy divide the box into two half part to
store his new comic in it and another used to store his old comic books in it. And we know that
if we put something in the box to use when we remove the same goes out one by one.
The same thing also perform by stack, which is called as LIFO(Last in First Out) philosophy.

D
OL W
NE

IMAGE REF
BY-
15
THANK
YOU

You might also like