Hello, good morning guys! I got a problem here with my submenus of Stack and Queue link list. When I try to add an item, it won't display and pop the items when I try to select the display and pop functions. Same as well with Queue. Can anyone help me with this?
Here's my code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node
{
int val;
struct node *next;
}*head;
void main()
{
int cho,choi,choice,x;
struct node *curr,*tempe;
clrscr();
head=NULL;
do
{
clrscr();
printf("\n\n [1] Stack\n");
printf(" [2] Queue\n");
printf(" [3] Exit\n\n");
printf(" Enter your choice: ");
scanf("%d",&cho);
if(cho == 1)
{
do
{
clrscr();
printf("\n\n [1] Push an item\n");
printf(" [2] Pop an item\n");
printf(" [3] Display the items\n");
printf(" [4] Exit\n\n");
printf(" Enter your choice: ");
scanf("%d",&choi);
switch(choi)
{
case 1:
clrscr();
printf("\n Enter a number to be saved in the Stack List: ");
scanf("%d",&x);
curr = (struct node*)malloc(sizeof(struct node));
curr->val = x;
curr->next = head;
head = curr;
printf("\n %d is pushed in the Stack List.");
break;
case 2:
clrscr();
if(head == NULL)
{
printf("\n The stack list is empty.");
return;
}
printf("\n %d has been popped out.",head->val);
curr = head;
head = head->next;
free(curr);
break;
case 3:
clrscr();
if(head == NULL)
{
printf("\n The stack list is empty.");
return;
}
printf("\n Stack List contains: Top->");
curr = head;
while(curr)
{
printf("%d->",curr->val);
curr = curr->next;
}
printf("NULL");
break;
case 4:
printf("\n\n T H A N K Y O U F O R U S I NG ! ! !");
getch();
exit(0);
break;
default:
clrscr();
printf("\n Enter the correct choice");
break;
}
}while(choi != 4);
}
else if(cho ==2)
{
do
{
clrscr();
printf("\n\n [1] Queue an item\n");
printf(" [2] Deque an item\n");
printf(" [3] Display the items\n");
printf(" [4] Exit\n\n");
printf(" Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
clrscr();
printf("\n Enter a number to be saved in the Queue List: ");
scanf("%d",&x);
curr = (struct node*)malloc(sizeof(struct node));
curr->val = x;
tempe = head;
if(head == NULL)
{
head = curr;
head->next = NULL;
}
else
{
while(tempe->next != NULL)
{
tempe = tempe->next;
}
curr->next = NULL;
tempe->next = curr;
}
printf("\n %d is pushed in the Queue List.");
break;
case 2:
clrscr();
if(head == NULL)
{
printf("\n The queue list is empty.");
return;
}
printf("\n %d has been deque out.",head->val);
curr = head;
head = head->next;
free(curr);
break;
case 3:
clrscr();
if(head == NULL)
{
printf("\n The queue list is empty.");
return;
}
printf("\n Queue List contains: Top->");
curr = head;
while(curr)
{
printf("%d->",curr->val);
curr = curr->next;
}
printf("NULL");
break;
case 4:
printf("\n\n T H A N K Y O U F O R U S I NG ! ! !");
getch();
exit(0);
break;
default:
clrscr();
printf("\n Enter the correct choice");
break;
}
}while(choice != 4);
}
}while(cho !=3);
}