int pop();
void push();
int isempty();
void display();
struct node
{
int data;
struct node *next;
};
struct node *temp;
struct node *top=NULL;
void push()
{
int value;
printf("\nEnter the value : ");
scanf("%d",&value);
temp=(struct node *)malloc(sizeof(struct node));
if (temp==NULL)
printf("\nMemory is not available!");
else
{
temp->data=value;
if (top==NULL)
temp->next=NULL;
else
temp->next=top;
top=temp;
}
}
int pop()
{
struct node *temp;
int value;
if (isempty())
printf("\nStack is empty!");
else
{
temp=top;
value=temp->data;
top=top->next;
free(temp);
return value;
}
}
int isempty()
{
if (top==NULL)
return 1;
else
return 0;
}
void display()
{
struct node *temp;
if (isempty())
printf("\nStack is empty!");
else
{
printf("The elements in the stack are...\n");
for(temp=top;temp!=NULL;temp=temp->next)
printf("%d\n",temp->data);
}
}
main()
{
int ch;
printf("\n1.Push 2.Pop 3.Display 4.Exit\n");
do
{
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: push(); break;
case 2: printf("\nDeleted element is %d",pop()); break;
case 3: display(); break;
}
}while(ch!=4);
}
No comments:
Post a Comment