0% found this document useful (0 votes)
35 views

Ex - No.6 Implementation of Stacks As Linked Lists 22.1.'10 Aim

The document describes a C++ program that implements stacks using linked lists. It includes the following: 1) A node structure is defined containing an integer data field and a link to the next node. 2) Push and pop functions are created to add and remove elements from the top of the stack. 3) The main function demonstrates pushing multiple values onto the stack and popping the top value off the stack.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Ex - No.6 Implementation of Stacks As Linked Lists 22.1.'10 Aim

The document describes a C++ program that implements stacks using linked lists. It includes the following: 1) A node structure is defined containing an integer data field and a link to the next node. 2) Push and pop functions are created to add and remove elements from the top of the stack. 3) The main function demonstrates pushing multiple values onto the stack and popping the top value off the stack.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

EX.NO.

6 IMPLEMENTATION OF STACKS AS LINKED LISTS

22.1.’10

AIM:
To write a C++ program to implement stacks using linked lists and to perform the basic
operations .

ALGORITHM:
STEP 1: Start.

STEP 2: A structure is created to implement the nodes in the linked list which contains a ‘data’
part and a link to the next node.

STEP 3: The PUSH function adds an element to the top of the stack.

STEP 4: The POP function deletes an element from the top of the stack.

STEP 5: Stop.
CODING:
#include<iostream.h>

#include<conio.h>

class stack

private:

struct node

int d;

node *link;

}*top;

public:

stack();

void push(int i);

int pop();

};

stack::stack()

top=NULL;

void stack::push(int i)
{

node *temp;

temp=new node;

if(temp==NULL)cout<<"Stack is full";

temp->data=i;

temp->link=top;

top=temp;

int stack::pop()

if(top==NULL)

cout<<"Stack is empty";

return NULL;

node *temp;

int i;

temp=top;

i=temp->d;

top=top->link;

delete temp;

return i;
}

void main()

clrscr();

int a,ch,c=3;

stack s;

s.stack();

while(c==3)

cout<<"\n 1.PUSH \n 2.POP \n ";

cout<<"\n Enter the choice:";

cin>>ch;

switch(ch)

case 1:

cout<<"\n Enter the element: ";

cin>>a;

s.push(a);

break;

case 2:

int v=s.pop();
cout<<"\n The popped element is "<<v;

break;

cout<<"\n Enter 3 to continue";

cin>>c;

getch();

}
OUTPUT:
1.PUSH

2.POP

Enter your choice 1

Enter the element: 5

Enter 3 to continue

1.PUSH

2.POP

Enter your choice 1

Enter the element: 2

Enter 3 to continue

1.PUSH

2.POP

Enter your choice 1

Enter the element: 15

Enter 3 to continue

1.PUSH

2.POP

Enter your choice 2

The popped element is 15

Enter 3 to continue

4
RESULT:
Thus a C++ program was executed successfully to implement stacks using linked lists.

You might also like