Tuesday, 23 September 2014

Stack Using Linked List or Pointer

$$$         STACK IMPLEMENTATION USING LINKED LIST OR POINTER          $$$

PROGRAM:

#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct stack
{
int data;
                struct stack *next;
}node;
node *newn,*top=NULL,*temp;

class stackp
{
public:
                void push()
                {
                                newn=new node;
                                cout<<"Enter the element to be pushed:\t";
                                cin>>newn->data;
                                if(top==NULL)
                                {
                                                newn->next=NULL;
                                                top=newn;
                                }
                                else
                                {
                                                newn->next=top;
                                                top=newn;
                                }
                }
                void pop()
                {
                                if(top==NULL)
                                {
                                                cout<<"Stack is empty or underflow\n";
                                }
                                else
                                {
                                                temp=top;
                                                top=top->next;
                                                cout<<"The deleted element is:"<<temp->data<<endl;
                                                delete(temp);
                                }
}
                void display()
                {
                                if(top==NULL)
                                {
                                                cout<<"Stack is empty or underflow\n";
                                }
                                else
                                {
                                                temp=top;
                                                cout<<"Content of the stack is:\n";
                                                while(temp!=NULL)
                                                {
                                                                cout<<temp->data<<endl;
                                                                temp=temp->next;
                                                }
                                }
}
 };

 void main()
 {
                stackp s;
                int ch;
                cout<<"\n\n\n$$$ STACK IMPLEMENTATION USING LINKED LIST OR POINTER $$$\n\n\n";
                while(1)
                {
                                cout<<"STACK OPERATIONS ARE\n";
                                cout<<"1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n";
                                cout<<"Enter your choice:\t";
                                cin>>ch;
                                switch(ch)
                                {
                                                case 1:s.push();break;
                                                case 2:s.pop();break;
                                                case 3:s.display();break;
                                                case 4:exit(0);
                                                default:cout<<"Enter the correct option\n";
                                                break;
                                }
                }
 }


OUTPUT:

$$$ STACK IMPLEMENTATION USING LINKED LIST OR POINTER $$$

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be pushed: 11

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be pushed: 22

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be pushed: 33

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      3
Content of the stack is:
33
22
11

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      2
The deleted element is: 33

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:           3
Content of the stack is:
22
11

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT

Enter your choice:           4

No comments:

Post a Comment