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);
}

I am sorry can you explain a bit more ....
What is the exact problem that you are facing ? This is the O/P that I got after running your code

$ ./test


[1] Stack
[2] Queue
[3] Exit

Enter your choice: 1


[1] Push an item
[2] Pop an item
[3] Display the items
[4] Exit

Enter your choice: 1

Enter a number to be saved in the Stack List: 10

is pushed in the Stack List.

[1] Push an item
[2] Pop an item
[3] Display the items
[4] Exit

Enter your choice: 1

Enter a number to be saved in the Stack List: 20

is pushed in the Stack List.

[1] Push an item
[2] Pop an item
[3] Display the items
[4] Exit

Enter your choice: 3

Stack List contains: Top->20->10->NULL

[1] Push an item
[2] Pop an item
[3] Display the items
[4] Exit

Enter your choice: 2

20 has been popped out.

[1] Push an item
[2] Pop an item
[3] Display the items
[4] Exit

Enter your choice: 2

10 has been popped out.

[1] Push an item
[2] Pop an item
[3] Display the items
[4] Exit

Enter your choice: 2

The stack list is empty.

$

Hello abhimanipal,

Thanks for replying Sir my problem Sir. Here's a quick video of my problem.

Using Turbo C --> Video

Using Borland C++ --> Video

Notice after I added the elements, the display and pop functions are not working. The problem with Queue is just the same with Stack. The Push and Queue function as well are not working. Even if I use Borland C++, nothings happen.

Edited 6 Years Ago by SHENGTON: n/a

I think your problem is that your screen is getting cleared . Try removing the clrscr() commands for options 2, 3.

This article has been dead for over six months. Start a new discussion instead.