/* stack */
# include
# include
struct node
{
int code;
char name[20];
struct node *next;
}*start=NULL,*ptr;
main()
{
int ch;
while(1)
{
clrscr();
printf("\n\t\tMain Menu");
printf("\n\t\t1. Push");
printf("\n\t\t2. Pop");
printf("\n\t\t3. Display Stack");
printf("\n\t\t4. Exit program");
printf("\n\t\Enter your choice(1-4)");
scanf("%d",&ch);
switch(ch)
{ case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return;
default:
printf("\nWrong choice");
}
getch();
}
}
push()
{ ptr=(struct node *) malloc(sizeof(struct node));
printf("ptr=%u",ptr);
printf("\nEnter code & name");
scanf("%d%s",&ptr->code,ptr->name);
printf("start=%u",start);
ptr->next=start;
printf("ptr next=%d",ptr->next);
start=ptr;
printf("start=%u",start);
}
pop()
{
ptr=start;
printf("ptr =%u",ptr);
if(ptr==NULL)
printf("\nStack Empty");
else
{
printf("\n%d popped",ptr->code);
printf("ptr next=%d",ptr->next);
start=ptr->next;
printf("start=%u",start);
free(ptr);
}
}
display()
{ ptr=start;
if(ptr==NULL)
printf("\nThe stack is empty");
else
while(ptr!=NULL)
{ printf("\nCode=%d \tName=%s",ptr->code,ptr->name);
ptr=ptr->next;
}
}
# include
# include
struct node
{
int code;
char name[20];
struct node *next;
}*start=NULL,*ptr;
main()
{
int ch;
while(1)
{
clrscr();
printf("\n\t\tMain Menu");
printf("\n\t\t1. Push");
printf("\n\t\t2. Pop");
printf("\n\t\t3. Display Stack");
printf("\n\t\t4. Exit program");
printf("\n\t\Enter your choice(1-4)");
scanf("%d",&ch);
switch(ch)
{ case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return;
default:
printf("\nWrong choice");
}
getch();
}
}
push()
{ ptr=(struct node *) malloc(sizeof(struct node));
printf("ptr=%u",ptr);
printf("\nEnter code & name");
scanf("%d%s",&ptr->code,ptr->name);
printf("start=%u",start);
ptr->next=start;
printf("ptr next=%d",ptr->next);
start=ptr;
printf("start=%u",start);
}
pop()
{
ptr=start;
printf("ptr =%u",ptr);
if(ptr==NULL)
printf("\nStack Empty");
else
{
printf("\n%d popped",ptr->code);
printf("ptr next=%d",ptr->next);
start=ptr->next;
printf("start=%u",start);
free(ptr);
}
}
display()
{ ptr=start;
if(ptr==NULL)
printf("\nThe stack is empty");
else
while(ptr!=NULL)
{ printf("\nCode=%d \tName=%s",ptr->code,ptr->name);
ptr=ptr->next;
}
}
No comments:
Post a Comment