Tuesday 24 April 2012

3 rd Experiment STACK OPERATION USING LINKED LIST

 ALGORITHM: STACK USING LINKED LIST:
---------------------------------------------------------------------------------------------------------------------------------------

Step- 1: Start.
Step- 2: do the following
a)      print the options
b)     read the choice
c)      i) if case is ‘push’, use the object of the class to call the member function push(). The members of the function are accessed through the pointers of the structure ‘node’. Thus stack is implemented using linked lists.
ii) If case is ‘pop’, use the object to call the member function pop(). The manipulation is done through pointers.
iii) If case is ‘display’, use the object to call the member function display().
vi) If case is ‘quit’, display’bye’.
Step-4: while the case is not ‘quit’, repeat the step-3.
Step-5: Stop.

---------------------------------------------------------------------------------------------------------------------------------------
                                                                            FLOW CHARTs
-----------------------------------------------------------------------------------------------------------------------------




-------------------------------------------------------------------------------------------------------------------------------------
                                                                 C++ IMPLEMENTATION
------------------------------------------------------------------------------------------------------------------------------------

//program for implementing Linked list on Stack
#include<iostream.h>
#include<conio.h>
#define NULL 0
class lstack
{
private:
struct node
{
int no;
node * next;
};
node * head;
public:
lstack();
~lstack();
void push();
void pop();
void display();
};
//******** Function Definition*******************************
lstack::lstack()
{
head=NULL;
}
lstack::~lstack()
{
//--------------
}
void lstack::push()
{
node * temp;
temp=new node;
cout<<"\n Enter the element:\n";
cin>>temp->no;
if (head==NULL)
{
head=temp;
temp->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
cout<<"\n Element "<<temp->no<<" is pushed into the stack\n";
}
void lstack::pop()
{
node*p=head;
if(head==NULL)
{
cout<<"\n Stack is empty\n";
}
else
{
head=head->next;
cout<<"\n Element "<<p->no<<"is popped from stack\n";
delete p;
}
}
void lstack::display()
{
node *p;
if(head==NULL)
{
cout<<"\n Stack is Empty\n:";
}
else
{
p=head;
cout<<"\nTHE ELEMENTS IN THE STACK:\n";
while(p!=NULL)
{
cout<<p->no<<endl;
p=p->next;
}
}
}
//*********************MAIN PROGRAM***********************
void main()
{
clrscr();
int ch;
lstack ls;
do
{
cout<<"\n1.PUSH\n2.POP\n3.QUIT\n";
cout<<"\n ENTER YOUR CHOICE:\n";
cin>>ch;
switch(ch)
{
case 1:
ls.push();
ls.display();
getch();
break;
case 2:
ls.pop();
ls.display();
getch();
break;

case 3:
cout<<"\n BYE...";
getch();
}
}
while(ch!=3);
}

OUTPUT:
1.Push
2.Pop
3.Exit
Enter your choice:
1
Enter the element:
1
The element 1 is pushed into the stack
The elements in stack
1

1.Push
2.Pop
3.Exit
Enter your choice:
1
Enter the element:
2
The element 2 is pushed into the stack
The elements in stack
2
1

1.Push
2.Pop
3.Exit
Enter your choice:
2
Element 2 is popped from stack
The elements in stack
1

1.Push
2.Pop
3.Exit
Enter your choice:
3
Bye..

No comments:

Post a Comment