Write a C Program to Implement a Stack using Arrays

Aim:

Write a C Program to Implement a Stack Using Arrays.

Theory:

Stack:

  • A stack is a list in which all insertions and deletions are made at one end, called the top. It is a collection of contiguous cells, stacked on top of each other. The last element to be inserted into the stack will be the first to be removed. Thus stacks are sometimes referred to as Last in First out (LIFO) lists.
  • The operations that can be performed on a stack are push, pop and top. Push is to insert an element at the top of the stack. Pop is deleting an element that is at the topmost position in the stack. Top simple examines and returns the topmost value in the stack without deleting it. Push on an already filled stack and pop on empty stack results in serious errors. Before any insertion, the value of the variable top is initialized to -1.

Push:

  • The push operation adds to the top of the list, hiding any items already on the stack, or initializing the stack if it is empty.

Pop:

  • The pop operation removes an item from the top of the list and returns this value to the caller. A pop either reveals previously concealed items or results in an empty list.

Stack Applications:

  1. Expression evaluation.
  2. Backtracking (game playing, finding paths, exhaustive searching).
  3. Memory management, run-time environment for nested language features.

Algorithm:

  1. Declare an array ahead of time called Array.
  2. Declare a structure called Stack that contains the TopOfStack and the Capacity fields.
  3. The variable called TopOfStack for the stack is initialized to -1.
  4. To push an element into the stack, increment TopOfStack and then set Array [TopOfStack] =X.
  5. To pop an element from the array, set the return value to Array [TopOfStack] and then decrement TopOfStack.
    • If stack status is overflow we can’t push the element into the stack.
    • Otherwise, we can add the data to the stack.
    • Move top to next position.

Program:

#include<stdio.h>
#define MAX 6
void push();
void pop();
void display();
int stack[MAX], top=-1, item;
int main()
{
int ch;
do
{
printf("\n\n\n\n1.\tPush\n2.\tPop\n3.\tDisplay\n4.\tExit\n");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
default:
printf("\n\nInvalid entry. Please try again...\n");
}
}
while(ch!=4);
}
void push(void)
{
if(top == MAX-1)
printf("\n\nStack is full");
else
{
printf("\n\nEnter ITEM:");
scanf("%d", &item);
top++;
stack[top] = item;
printf("\n\nITEM inserted = %d", item);
}
}
void pop(void)
{
if(top == -1)
printf("\n\nStack is empty.");
else
{
item = stack[top];
top--;
printf("\n\nITEM deleted = %d", item);
}
}
void display(void)
{
int i;
if(top == -1)
printf("\n\nStack is empty.");
else
{
for(i=top; i>=0; i--)
printf("\n%d", stack[i]);
}
}

Execution:

Input:

1 11 1 22 1 33 3 2 3 2 3 4

Output:

1.	Push
2.	Pop
3.	Display
4.	Exit

Enter your choice:

Enter ITEM:

ITEM inserted = 11



1.	Push
2.	Pop
3.	Display
4.	Exit

Enter your choice:

Enter ITEM:

ITEM inserted = 22



1.	Push
2.	Pop
3.	Display
4.	Exit

Enter your choice:

Enter ITEM:

ITEM inserted = 33



1.	Push
2.	Pop
3.	Display
4.	Exit

Enter your choice:
33
22
11



1.	Push
2.	Pop
3.	Display
4.	Exit

Enter your choice:

ITEM deleted = 33



1.	Push
2.	Pop
3.	Display
4.	Exit

Enter your choice:
22
11



1.	Push
2.	Pop
3.	Display
4.	Exit

Enter your choice:

ITEM deleted = 22



1.	Push
2.	Pop
3.	Display
4.	Exit

Enter your choice:
11



1.	Push
2.	Pop
3.	Display
4.	Exit

Enter your choice:

Invalid entry. Please try again...

Result:

Thus, Implement stack using array was executed successfully.

Leave a Comment