/*ALGORITHM:bth insertion nd deletion is at one end only
insertion:-frst check if there is memory present nd if yes thn enter
deletion:-frst check if stack has somethng to delte if yes copy the 2nd node from top to top ths wud leave the current top free in memory
*/
#include<iostream>
#include<conio.h>
using namespace std;
struct player
{
char name[20];
player *next; //to store address of next piece of info(node) i.e which is at top of it
};
class stack
{
player *top; //stores the info topmost node(similar to top in static stack)
public:
stack()
{
top=NULL; //means stack is empty no node present(similar to static stack)
}
void ins();
void del();
void display();
};
void stack::ins()
{
player *temp=new player; //use it to store recent node for temporary tym nd assigned memory dynamically i.e at run time
if(temp==NULL)
{
cout<<"\nOVERFLOW ERROR..."<<endl;
return;
}
else
{
cout<<"ENTER PLAYER NAME:";
cin.ignore();
cin.getline(temp->name,20);
temp->next=NULL; //we store a null value in ths becozz currently it is an indiviual node nt part of stack so nt storing any address of next node
if(top==NULL) //ths mns ths node stack is empty so temp will now be frst node
{
top=temp; //stores all data of temp to top i.e bth name and next
}
else //ths tym the upcoming node is nt frst node
{
temp->next=top; //so frst store the adddress of current top node in its next portion so dat whn we cpy ths in top we dnt loose the top node
top=temp; //now the upcoming node will become topmost node
}
}
}
void stack::del()
{
player *temp;
if(top==NULL)
{
cout<<"\nUNDERFLOW ERROR..."<<endl;
return;
}
else
{
temp=top;
cout<<temp->name<<" DELETED.."<<endl;
top=top->next; //makes the 2nd node frm top the topmost node(and address of 2nd node is stored in current topmost nodes next part)
delete temp; //to delete the variable temp from memory
}
}
void stack::display()
{
for(player *temp=top;temp!=NULL;temp=temp->next)
cout<<"PLAYER NAME"<<temp->name<<endl;
}
int main()
{
int choice;
char ans;
stack s;
do
{
cout<<"MENU:"<<endl
<<"1.INSERT"<<endl
<<"2.DELETE"<<endl
<<"3.DISPLAY"<<endl
<<"4.EXIT"<<endl<<endl;
cout<<"ENTER YOUR CHOICE:";
cin>>choice;
switch(choice)
{
case 1:
s.ins();
break;
case 2:
s.del();
break;
case 3:
s.display();
break;
case 4:
exit(0);
}
cout<<"\nDO YOU WANT TO GO BACK TO MAIN MENU:";
cin>>ans;
}while(ans!='n');
getch();
return 0;
}