0% found this document useful (0 votes)
11 views9 pages

10. stack implementation using linked list

Uploaded by

asodariadhruv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

10. stack implementation using linked list

Uploaded by

asodariadhruv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

25 August 2024 19:43

New Section 1 Page 1


New Section 1 Page 2
New Section 1 Page 3
#include <cstdlib>
#include <iostream>
# include<stdlib.h>
using namespace std;
struct stack
{
int data;
struct stack* next;
}*top,*head,*newnode,*temp;

void push(int input)


{
cout<<"push function called"<<endl;
newnode=(struct stack*)malloc(sizeof(struct stack));
newnode->data=input;
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
top=head; // top=newnode
}
else
{
top->next=newnode;
top=top->next; //top=newnode;
}

}
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);

New Section 1 Page 5


push(5);
display();
push(10);
display();
push(15);
display();
pop();
display();
pop();
display();
pop();
display();
pop();

#include <cstdlib>
#include <iostream>
# include<stdlib.h>
using namespace std;
struct stack
{

New Section 1 Page 6


{
int data;
struct stack* next;
}*top,*newnode,*temp;

void push(int input)


{
cout<<"push function called"<<endl;
newnode=(struct stack*)malloc(sizeof(struct stack));
newnode->data=input;
newnode->next=NULL;
if(top==NULL)
{
top=newnode;
}
else
{
newnode->next=top;
top=newnode;
}

}
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)

New Section 1 Page 7


if(top==NULL)
{
cout<<"stack underflow"<<endl;
}
else
{
cout<<"stack elements are and top element
is"<<top->data<<endl;

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();

New Section 1 Page 8


New Section 1 Page 9

You might also like