DATA STRUCTURES

  • Home
  • DATA STRUCTURES USING C++


Admission Enquiry Form



Program of Stack Using Linked List.

//Stack using linked list
#include<iostream>
using namespace std;
struct node
{
int data;
node *link;
};//end of structure

node *start=NULL;
node *temp=NULL;
void createNode(int value)
{
node *ptr;
ptr=new node;
ptr->data=value;
ptr->link=NULL;
if(start==NULL)
{
start=ptr;
temp=ptr;
}
else
{
temp->link=ptr;
temp=ptr;
}
}//end of function

void display()
{
node *ptr;
ptr=start;
cout<<endl<<"Data of List is :"<<endl;
while(ptr!=NULL)
{
cout<<" "<<ptr->data;
ptr=ptr->link;
}
}//end of function
void pop()
{
node *ptr;
ptr=start;
while(ptr!=NULL)
{
if(ptr->link->link==NULL)
{
temp=ptr;
ptr->link=NULL;
cout<<endl<<"Node deleted...";
break;
}
ptr=ptr->link;
}//end of while
}//end of function

int main()
{
int choice,value;
do
{
cout<<endl<<"1-----------------> Add Node";
cout<<endl<<"2-----------------> Display ";
cout<<endl<<"3-----------------> Exit ";
cout<<endl<<"4----------------->pop ";
cout<<endl<<endl<<"Enter your Choice :";
cin>>choice;
if(choice==1)
{
cout<<endl<<"Enter value :";
cin>>value;
createNode(value);
}
else if(choice==2)
{
display();
}
else if(choice==3)
{
cout<<endl<<"Program terminated...";
break;
}
else if(choice==4)
{
pop();
}
}while(1);//end of loop
return 0;
}