Posts

Showing posts with the label Program implements linked list as a stack.

Program implements linked list as a stack.

#include #include #include /* structure containing data part and linkpart */ struct node { int data ; struct node *link ; } ; void push ( struct node **, int ) ; int pop ( struct node ** ) ; void delstack ( struct node ** ) ; void main( ) { struct node *s = NULL ; int i ; clrscr( ) ; push ( &s, 14 ) ; push ( &s, -3 ) ; push ( &s, 18 ) ; push ( &s, 29 ) ; push ( &s, 31 ) ; push ( &s, 16 ) ; i = pop ( &s ) ; printf ( "\nItem popped: %d", i ) ; i = pop ( &s ) ; printf ( "\nItem popped: %d", i ) ; i = pop ( &s ) ; printf ( "\nItem popped: %d", i ) ; delstack ( &s ) ; getch( ) ; } /* adds a new node to the stack as linked list */ void push ( struct node **top, int item ) { struct node *temp ; temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ; if ( temp == NULL ) printf ( "\nStack is full." ) ; temp -> data = item ; temp -> link = *top ; *top = temp ; } /* pops an eleme...