I need to have my code modified, but am unsure how to write the code to do it. After the ID and name are poped from the stack, I need the program to automatically display the list created i.e. I don't want to use display as an option.
I think I need to have main function loop back after pop and display are done. How can I do this?
Here is the code:
#include<conio.h>
#include<stdio.h>
#include <iostream.h>
class link_stack
{
struct node
{
int id;
char name[10];
node *next;
};
node *top,*x,*ptr;
public:
link_stack()
{
top=x=ptr=NULL;
}
void push()
{
x=new node;
cout << "Enter an ID number and name: ";
cin >> x->id >> x->name;
x->next=top;
top=x;
}
void pop()
{
if(top==NULL)
cout<<"\nStack is Empty";
else
{
x=top;
top=top->next;
delete x;
}
}
void display()
{
ptr=top;
while(ptr!=NULL)
{
cout << "\nName: "<< ptr->name;
cout<<"\nID number: "<<ptr->id;
ptr=ptr->next;
}
}
};
void main()
{
link_stack obj;
int choice;
do
{
cout << "\n ----------MENU---------- \n";
cout << "1.Push\n"
<< "2.Pop\n"
<< "3.Display\n"
<< "4.Exit";
cout << "\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1: obj.push();
break;
case 2: obj.pop();
break;
case 3: obj.display();
break;
case 4: cout << endl;
}
}
while (choice!=4);
getch();
}
Code tags added. -Narue