| Re: Linked list ..help Program to illustrate stack using Linked list concept: /*Program to illustrate stack using linked list concept*/ #include /*Link section*/ struct node /*Structure used to represent elements as nodes*/ { int val; /*Element*/ struct node *next; /*Next pointer*/ }*top = NULL,*temp = NULL; void push(int); /*Function declaration section*/ int pop(); void disp(); /*Main function starts here*/ void main() { int opt,item,popped; /*Loop which processes infinitely and ends when forced by the user*/ do { clrscr(); printf("Enter the required option\n1.Push\n2.Pop\n3.View the stack\n4.exit:"); scanf("%d",&opt); /*Option is received here*/ switch(opt) { case 1: printf("Enter the element to push:"); scanf("%d",&item); push(item); /*Element pushed into the stack*/ break; case 2: popped = pop(); /*Element popped out of the stack*/ if(popped == 0) { disp(); continue; } printf("Popped element = %d",popped); getch(); break; case 3: disp(); /*Used to diplay the stack*/ break; } }while(opt != 4); } /*Function to push an element into the stack*/ void push(int item) { if(top == NULL) /*Condition which checks whether first element*/ { /*Reserving memory space using malloc*/ top = (struct node *)malloc(sizeof(struct node)); /*Assigning the first element as the entered value*/ top->val = item; /*Assigning the next pointer to NULL*/ top->next = NULL; } else { /*Reserving memory space using malloc*/ temp = (struct node *)malloc(sizeof(struct node)); /*Asssigning the entered value into a temporary space*/ temp->val = item; /*Mapping the next pointer to the previous element*/ temp->next = top; /*Mapping the top of stack with the current element*/ top = temp; } } int pop() { int res; if(top == NULL) /*Checking for the stack to be empty*/ return(0); else { int pop; pop = top->val; /*Storing the popped value into a variable*/ temp = top; /*Manipulating the pointers to remove the element*/ top = top->next; free(temp); /*Making the temporary space free*/ return pop; /*Returning the value stored to pop out*/ } } void disp() { if(top == NULL) /*Condition checking for the stack to be empty*/ { printf("The stack is empty"); getch(); return; } temp = top; printf("The stack elements are:\n"); while(temp != NULL) /*Printing the stack elements*/ { printf("\n%d",temp->val); /*Moving the temporary pointer to the next element*/ temp = temp->next; } getch(); } Output: Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:3 The stack is empty Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:1 Enter the element to push:156 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:1 Enter the element to push:185 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:1 Enter the element to push:894 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:1 Enter the element to push:474 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit3 The stack elements are: 474 894 185 156 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:2 Popped element = 474 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:2 Popped element = 894 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:3 The stack elements are: 185 156 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:2 Popped element = 185 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:2 Popped element = 156 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:2 The stack is empty Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:4 |