Page 151 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 151
Fundamentals of Data Structures
Notes void push(node **tos,int item)
{
node *temp;
temp=(node*)malloc(sizeof(node)); /* create a new node dynamically */
if(temp==NULL) /* If sufficient amount of memory is */
{ /* not available, the function malloc will */
printf(“\nError: Insufficient Memory Space”); /* return NULL to temp
*/
getch();
return;
}
else
{
temp->data=item; /* put the item in the data portion of node*/
temp->next=*tos; /*insert this node at the front of the stack */
*tos=temp; /* managed by linked list*/
}
} /*end of function push*/
/* Definition of pop function */
int pop(node **tos)
{
node *temp;
temp=*tos;
int item;
if(*tos==NULL)
return(NULL);
else
{
*tos=(*tos)->next; /* To pop an element from stack*/
item=temp->data; /* remove the front node of the */
free(temp); /* stack managed by L.L*/
return (item);
}
} /*end of function pop*/
/* Definition of display function */
void display(node *tos)
{
node *temp=tos;
if(temp==NULL) /* Check whether the stack is empty*/
{
printf(“\nStack is empty”);
144 LOVELY PROFESSIONAL UNIVERSITY