Skip to main content

stack using link list

/* stack using link list   */
#include<iostream>
using namespace std;
class node
{

private:
int data;
node *next;
public:
void push();
void pop();
void show();

};
node *head=NULL;
node *tail=NULL;
node *current=NULL;
node *temp=NULL;
int num;
int size=0;


int main()
{
node n;
string x;
while(1)
{
cout<<"\n\n1 to Push"<<endl;
cout<<"\n2 to Pop"<<endl;
cout<<"\n3 to Display"<<endl;
cout<<"\n4 to exit: ";
cin>>x;

if(x=="1")
{
n.push();
}
else if(x=="2")
{
n.pop();
}
else if(x=="3")
{
n.show();
}
else if(x=="4")
{
return 0;
}
else
{
cout<<"\nInvalid Command\n"<<endl;
}
}


}

//push Function
void node::push()
{
cout<<"\nEnter value: ";
cin>>num;
temp=new node;
temp->data=num;
temp->next=NULL;

if(head==NULL)
{
head=temp;
tail=temp;
current=temp;
}
else
{

tail->next=temp;
tail=temp;
current=temp;
}
size++;
cout<<"\n"<<num<<": Pushed Successfully\n"<<endl;

}

//push Function


//POP function
void node::pop()
{
if(size==0)
{
cout<<"\n^^^^^^^^^^^^^^^^^^^^^^"<<endl;
cout<<"\nList is Empty\n"<<endl;
cout<<"\n^^^^^^^^^^^^^^^^^^^^^^"<<endl;
}
else
{
current=head;
for(int i=1;i<size;i++)
{
temp=current;
current=current->next;
}
num=current->data;
if(size==1)
{

delete current;
head=NULL;
current=NULL;
tail=NULL;
temp=NULL;
}
if(size>1)
{

delete current;
temp->next=NULL;
tail=temp;
current=temp;
}
size--;
cout<<"\n"<<num<<": POPED Successfully\n"<<endl;

}



}


//POP function



//Display function
void node::show()
{
if(size==0)
{
cout<<"\n^^^^^^^^^^^^^^^^^^^^^^"<<endl;
cout<<"\nList is Empty\n"<<endl;
cout<<"\n^^^^^^^^^^^^^^^^^^^^^^"<<endl;
}
else
{
current=head;
cout<<"\n**********************\n"<<endl;
cout<<"Total Nodes: "<<size<<"\n"<<endl;
while(current!=NULL)
{
cout<<current->data<<"  ";
current=current->next;
}
cout<<"\n\n**********************\n"<<endl;
}
}

//Display function


Comments

Popular posts from this blog

C++ Projects (MAKING DMC )

c++ project of dmc #include<iostream> #include<fstream> #include<iomanip> #include<conio.h> #include<windows.h> using namespace std; class student { protected : char  name[50]; int roll; char board[50]; }; class dmc:public student { protected : float per; int phy; int maths; int cs; int english; public : void getdata() { cout<<"ENTER NAME : "; cin>>name; cout<<"ENTER ROLL : "; cin>>roll; cout<<"PHYSIC MARK : "<<endl; cin>>phy; cout<<"MATHS MARKS :"<<endl; cin>>maths; cout<<"ENTER CS MARKS :"<<endl; cin>>cs; cout<<"ENTER ENGLISH MARKS : "<<endl; cin>>english;    } float calculate() { per=(((phy+maths+cs+english)*(100))/400); return per; } double add() { double total...

C++ project of school mangment system

            C++ SIMPLE PROJECT FOR STORYING RECORDS OF STUDENTS    POST COMMENT FOR ANY ERROR OR IF YOU HAVE ANY PROBLEM WITH THE                                                           CODE  #include<iostream>  #include<windows.h> using namespace std; int main() { string name[3]; int id[3] ; int count=0; int z; int y; int ch; int k; int roll; int c; int dd; char yy; cout<<"*************************WELLCOME TO DATABASE         SYSTM*********************"<<endl; while(1) { cout<<"\t\tpress 1 for entry "<<endl; cout<<"\t\tpress 2 for full data display "<<endl;          cout<<"\t\tpress 3 changing :  "<<endl;           c...