Linear Data Structure 485
printf ("
By choice terminated : ");
ex i t (0);
}
P++;
if (p>4)
{
printf ("Stack is full
");
break;
}
else
printf ("Stack is emptyn");
}
printf ("
Elements of stack are : ");
for (j=0;j<5;j++)
printf (" %d n,stack[j]);
}
OUTPUT:
Enter Elements put zero to exit:
4
Stack is empty
2
Stack is empty
6
Stack is empty
8
Stack is empty
3
Stack is full
Elements of stack are: 4 2 6 8 3
Explanation In the above program an array stack [5] is declared. Using while loop, elements are
read through the keyboard and placed in an array. The value of variable p is initially zero. The
variable p is increasing in every iteration. The variable p is acting as a TOP of stack. The 'if' statement
checks the value of p. If the value of p is greater than four, it means that the stack is full and no more
elements can be added. When value of p is less than four it means more elements can be inserted in
the stack. The 'for' loop and cout statement display the array elements. In a stack, random insertion
or deletion of elements is not possible. (Though in an array it is possible, but here keep in mind that
we are treating an array as a stack. Hence, we have to follow the restrictions that exist in stack
implementation). If we want to delete any particular element, we have to delete
14.9 Write a program to store elements in the stack. Delete the specified element.
# include <stdio.h>
# include <conio.h>