#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node*next;
};
struct node*head=NULL;
void push(int val)
{
struct node*new=malloc(sizeof(struct node));
new->data=val;
new->next=head;
head=new;
printf("The element %d has been pushed into the stack successfully!\n", val);
}
void pop()
{
struct node*temp;
if(head==NULL)
printf("The Stack is empty!\n");
else
{
printf("The element %d has been popped out of the stack successfully!\n", head->data);
temp=head;
head=head->next;
free(temp);
}
}
void print()
{
struct node*temp=head;
printf("The elements of the stack are :\n");
while(temp!=NULL)
{
printf("%d ", temp->data);
temp=temp->next;
}
}
void main()
{
int a,n;
do
{
printf("Enter 1 for push, 2 for pop, 3 for display and 4 to exit the program\n");
scanf("%d", &a);
switch(a)
{
case 1 :printf("Enter the element to be pushed\n");
scanf("%d", &n);
push(n);
break;
case 2 : pop();
break;
case 3 : print();
break;
case 4 : printf("The program has been terminated\n");
break;
case 5 : printf("Enter a valid number!\n");
break;
}
}
while(a!=4);
}
To embed this program on your website, copy the following code and paste it into your website's HTML: