484 Programming and Data Structures
Empty
stack
TOP
8 TOP !
5
2
push (8)
Fig. 14.11 (a) Push operation with stack
Stack initially pop (8) pop (2) Empty stack
(b) PoP operation with stack
14.14 IMPLEMENTATION OF A STACK
The stack implementation can be done in the following two ways.
1) Static implementation: Static implementation can be achieved using arrays. Though, it is a very
simple method, but it has a few limitations. Once the size of an array is declared, its size cannot be
altered during program execution. While in array declaration, memory is allocated equal to array
size. The vacant space of stack (array) also occupies memory space. Thus, it is inefficient in terms
of memory utilization. In both the cases if we store less argument than declared, memory is wasted
and if we want to store more elements than declared, array could not expand. It is suitable only
when we exactly know the number of elements to be stored.
14.8 Write a program to explain the working of stack using array implementation.
# include <atdio.h>
# include cconio«h>
# include <proces0 .h>
main ()
{
int j,s ta c k [5] ■{<>}»
int p»0»
c lrsc r O j
printf ("In ter Klaaeats put aero to exit t n")»
while (1)
{
scanf ( "%d", ftatack[p]);
i f (atac k [p]»»0 )
{
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>
486 Programming and Data Structures
main ()
{
int j,sta c k [5];
int p=0;
clrscr () ;
prin tf ("Enter Elements put zero to exit : ") ;
while (1)
{
scanf ( %d, &stack[p]);
i f (stack[p]*b0)
break;
P++;
i f (p>4)
{
prin tf ("Stack is fu ll n "); break;
}
}
prin tf (" Stack elements are t );
for (j*0;j< 5;j++)
prin tf (" %d ", s ta c k [j]);
printf ( n Enter element number to delete : );
scanf ("%d",&p);
P --;
for (j*4 ;j> s p ;j--)
Stack[j]®NULL;
printf ("Stack elements are : " );
for (j»0;j<5 ;j++ )
printf (" %d ",s t a c k [j]);
getchO ;
}
OUTPUT:
Enter Elements put zero to exit:
8
i
3
Stack is full
Stack elements are: 8 9 4 5 3
Linear Data Structure 487
Enter element number to delete: 3
Stack elements are: 8 9 0 0 0
Explanation In the above program, as usual, elements are entered and stored in the stack. The
element number that is to be deleted from the stack will be entered from the keyboard. Recall that in
a stack, before deleting any element, it is compulsory to delete all elements before that element. In
this program, the elements before a target element are replaced with value NULL (0). The elements
after the target element are retained. Recall that when an element is inserted in the stack the operation
is called 'push'. When an element is deleted from the stack then the operation is pop. The following
program explains the push and pop operations.
14.10 Write a simple program to demonstrate push () and pop () operations.
# include <stdio.h>
# include <conio.h>
static int stack[10], top=-l;
void main ()
{
void push(int);
void pop(void)i
void show(void);
c lr s c r ();
print£ (" push operation : );
push( 5 );
show () ;
push ( 8) ;
show() ;
pu sh(9 );
show () ;
printf ( nn pop operation : );
show();
pop();
show () ;
pop();
show () ;
}
void push (int j)
{
top++;
stac k [top ]»j;
void pop ()
{
stack[top]=0;
to p --;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.255.250