PROGRAM TO IMPLEMENT INVERSE OF STACK USING LINKED LIST

/*PROGRAM TO IMPLEMENT INVERSE OF STACK USING LINKED LIST*/
#include<conio.h>
#include<iostream.h>
struct node
{
int data;
struct node *link;
};
void main()
{
struct node *top=NULL,*ptr,*temp,*start=NULL,*temp1;
int num;
char ch;
clrscr();
do
{
temp=new node;
cout<<“Enter the element to be pushed: “;
cin>>num;
temp->data=num;
temp->link=NULL;
if(top==NULL)
{
top=temp;
}
else
{
temp->link=top;
top=temp;
}
cout<<“Do you want to push more elements?? “;
cin>>ch;
}while(ch==’y’||ch==’Y’);
cout<<“Elements in the stack are: “<<endl;
for(ptr=top;ptr!=NULL;ptr=ptr->link)
{
cout<<ptr->data<<“\t”;
}
//INVERSE OF STACK
cout<<endl<<“Inverse of stack is:”<<endl;
for(ptr=top;ptr!=NULL;ptr=ptr->link)
{
temp1=new node;
temp1->data=ptr->data;
if(start==NULL)
{
start=temp1;
start->link=NULL;

}
else
{
temp1->link=start;
start=temp1;
}
}
for(ptr=start;ptr!=NULL;ptr=ptr->link)
{
cout<<ptr->data<<“\t”;
}
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *