10. stack implementation using linked list
10. stack implementation using linked list
}
void pop()
{
if(head==NULL)
cout<<"stack underflow"<<endl;
New Section 1 Page 4
cout<<"stack underflow"<<endl;
else
{
if(head->next==NULL)
{
cout<<"popped element is"<<top->data<<endl;
delete top;
head=NULL;
}
else
{
temp=head;
while(temp->next!=top)
{
temp=temp->next;
}
cout<<"popped element is"<<top->data<<endl;
delete top;
temp->next=NULL;
top=temp;
}
}
}
void display()
{
if(head==NULL)
{
cout<<"stack underflow"<<endl;
}
else
{
cout<<"stack elements are and top element
is"<<top->data<<endl;
for(temp=head;temp!=top->next;temp=temp->
next)
{
cout<<temp->data<<endl;
}
}
}
int main()
{
push(5);
#include <cstdlib>
#include <iostream>
# include<stdlib.h>
using namespace std;
struct stack
{
}
void pop()
{
if(top==NULL)
cout<<"stack underflow"<<endl;
else
{
if(top->next==NULL)
{
cout<<"popped element is"<<top->data<<endl;
delete top;
top=NULL;
}
else
{
cout<<"popped element is"<<top->data<<endl;
temp=top;
top=top->next;
delete temp;
}
}
}
void display()
{
if(top==NULL)
for(temp=top;temp!=NULL;temp=temp->next)
{
cout<<temp->data<<endl;
}
}
}
int main()
{
push(5);
display();
push(10);
display();
push(15);
display();
pop();
display();
pop();
display();
pop();
display();
pop();