5 StackQandAns
5 StackQandAns
5. The Stack
Definition and Examples
A stack is an ordered collection of items into which new items may be inserted and from which items may
be deleted at one end, called the top of the stack
Example of stack
We know that in a cafeteria the cups are placed one above the other and every new cup is added at top.
When a cup is required, it is taken off from the top and it is used. We call this process as stacking of cups.
#define STACKSIZE 50
struct stack
{
int items[STACKSIZE];
int top;
};
Once this has been done, an actual stack s may be declared as,
struct stack s;
In the above declaration stack is a structure template and members of stack can be accessed via the
structure variable s.
You can now access stack elements using dot (.) operator. For instance, s. items[9] refers to the
10th element of a stack, since array index starts from zero in C.
The integer variable top is used to denote the topmost element of the stack s. for example, if s.top
is the integer value 5, and then there are 6 items on the stack.
If s.top is -1, then the stack is empty (stack underflow).
If s. top exceeds 49, then stack overflow occurs. That is, stack is full (stack overflow).
3. Write an algorithm to perform push and pop operations in stack
Insert/push operation:
Inserting an element into the stack is called push operation. Only one item is inserted at a time and
item has to be inserted on the top of the stack.
step 1: start
else
Goto step3
top = top + 1 ;
stack [ top ] = item ;
step 4: Stop
Delete/pop operation:
Deleting an element from the stack is called pop operation. Only one item can be deleted at a time
and item has to be deleted only from top of the stack.
step1: Start
if (top = = -1 ) then
else
item=s[top]
top=top-1;
Step 4: Stop
#include <stdio.h>
#include <conio.h>
#define MAXSIZE 5
void main ()
{
int choice;
int option = 1;
clrscr ();
s.top = -1;
printf ("STACK OPERATION\n");
while (option)
{
printf ("------------------------------------------\n");
printf (" 1 --> PUSH \n");
printf (" 2 --> POP \n");
printf (" 3 --> DISPLAY \n");
printf (" 4 --> EXIT \n");
printf ("------------------------------------------\n");
printf ("Enter your choice\n");
scanf ("%d", &choice);
switch (choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: return;
}
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 == (MAXSIZE - 1))
{
printf ("Stack is Full\n");
return;
}
else
{
printf ("Enter the element to be pushed\n");
scanf ("%d", &num);
s.top = s.top + 1;
s.items[s.top] = num;
}
}
/*Function to delete an element from the stack*/
int pop ()
{
int num;
if (s.top == - 1)
{
printf ("Stack is Empty\n");
return (s.top);
}
else
{
num = s.items[s.top];
printf ("poped element is = %d\n", s.items[s.top]);
s.top = s.top - 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 ("\nThe status of the stack is\n");
for (i = s.top; i >= 0; i--)
{
printf ("%d\n", s.items[i]);
}
}
printf ("\n");
}
/*---------------------------------------------------------------------------
Output
STACK OPERATION
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
23
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
45
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
78
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
3
The status of the stack is
78
45
23
Do you want to continue (Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
2
poped element is = 78
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
3
The status of the stack is
45
23
Do you want to continue(Type 0 or 1)?
0