You are on page 1of 12

STACK DATA STRUCTURE

TWO STACKS IN ONE ARRAY

ANKIT CHAUHAN 21SCSE1040134


ANKUR KUMAR 21SCSE1040080
AMIT KUMAR 21SCSE1040072
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-

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.
Memory Representation of a STACK as an Linked-list

2
3
Programe in C to Implement

Code
#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);

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);
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);
}
}
/* Function to add an element to the stack */
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;
}

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;
}
/* Function to delete an element from the stack */
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);
}

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);
}
/* Function to display the status of the stack */
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");
}
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");
}
Output:
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.so when boy put or remove his old or new
comics ,it use the LIFO.

old
new

You might also like