Page 82 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 82
Unit 3: Stacks
push ( &top, 16 ) ; Notes
push ( &top, 17 ) ;
clrscr( ) ;
stack_display ( top ) ;
printf ( “No. of items in stack = %d” , count ( top ) ) ;
printf ( “\nItems extracted from stack : “ ) ;
item = pop ( &top ) ;
printf ( “%d “, item ) ;
item = pop ( &top ) ;
printf ( “%d “, item ) ;
item = pop ( &top ) ;
printf ( “%d “, item ) ;
stack_display ( top ) ;
printf ( “No. of items in stack = %d” , count ( top ) ) ;
}
/* adds a new element on the top of stack */
push ( struct node **s, int item )
{
struct node *q ;
q = malloc ( sizeof ( struct node ) ) ;
q -> data = item ;
q -> link = *s ;
*s = q ;
}
/* removes an element from top of stack */
pop ( struct node **s )
{
int item ;
struct node *q ;
/* if stack is empty */
if ( *s == NULL )
printf ( “ stack is empty” ) ;
else
{
q = *s ;
item = q -> data ;
*s = q -> link ;
free ( q ) ;
return ( item ) ;
}
}
/* displays whole of the stack */
stack_display ( struct node *q )
LOVELY PROFESSIONAL UNIVERSITY 77