The following is a program for ins,del,peep, isempty of elements. If you have any doubts please let me know.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *top=NULL,*newn,*ptr=NULL;
void ins();
void del();
void peep();
void display();
void isempty();
main()
{
int ch;
do
{
printf("\nMenu:\n1.Insert\n2.Delete\n3.Peep\n4.Display\n5.Is
empty\n6.Exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
ins();
break;
case 2:
del();
break;
case 3:
peep();
break;
case 4:
display();
break;
case 5:
isempty();
break;
case 6:
break;
default:
printf("Wrong
choice");
break;
}
}while(ch!=6);
}
void ins()
{
int
num;
printf("Enter the element to be inserted:");
scanf("%d",&num);
newn=(struct node *)malloc(sizeof(struct node));
newn->data=num;
if(top==NULL)
{
newn->next=NULL;
top=newn;
}
else
{
newn->next=top;
top=newn;
}
}
void del()
{
if(top==NULL)
printf("\nUndeflow\n");
else
{
ptr=top;
top=top->next;
free(ptr);
}
}
void peep()
{
if(top==NULL)
{
printf("\nUnderflow\n");
}
else
{
printf("top: %d ",top->data);
}
}
void display()
{
ptr=top;
if(ptr==NULL)
{
printf("\nUnderflow\n");
}
else
{
while(ptr->next!=NULL)
{
printf("=>%d ",ptr->data);
ptr=ptr->next;
}
printf(" =>%d ",ptr->data);
}
}
void isempty()
{
if(top==NULL)
{
printf("\nStack is empty\n");
}
else
{
printf("\nStack is not empty\n");
}
}
No comments:
Post a Comment