Stack Using Linked List 7
Stack Using Linked List 7
Name: Implementation of Stack using Linked List - push() and display() operations Date:
Fil l the missing code in the functions push(int x) and display() in the below code.
The function push() inserts an element into the stack and it gives "stack overflow" error if the maximum capacity of the
stack is reached.
Source Code:
StackListMain1.c
#include "StackListPushDisplay.c"
int main() {
int op, x;
while(1) {
printf("1.Push 2.Display 3.Exit\n");
scanf("%d",&op);
switch(op) {
case 1:
scanf("%d",&x);
push(x);
break;
case 2:
display();
break;
case 3:
exit(0);
}
}
Sasi Institute of Technology and Engineering (Autonomous)
StackListPushDisplay.c
#include <stdio.h>
#include <stdlib.h>
struct stack {
int data;
};
stk push(int x)
{
if(temp==NULL)
printf("Stack is empty.\n");
else
if(top==NULL)
temp->data=x;
temp->next=NULL;
top=temp;
else
temp->data=x;
temp->next=top;
top=temp;
printf("Successfully pushed.\n");
void display();
void display()
if(top==NULL)
printf("Stack is empty.\n");
else
while(temp!=NULL)
printf("%d ",temp->data);
temp=temp->next;
printf("\n");
Test Case - 1
User Output
1.Push 2.Display 3.Exit 1
Test Case - 1
Enter your option : 1
Test Case - 2
Test Case - 2
Test Case - 3
User Output
1.Push 2.Display 3.Exit 1
Test Case - 3
Enter your option : 1