Welcome To Cbitcse 2k12



[+] Post Title :

Data Structures Stack Implementation of Linked list Using C++


[+] Date : Sunday, 20 October 2013
[+] Author : Prudhvi raj
[+] Type :
Algorithm:-
 
1. Declare and initialize necessary variables such as struct node *top, *p, top = NULL
2. For push operation,
    - check for memory full
       if((p=(nodetype*) malloc (sizeof(nodetype))) == NULL)
         print "Memory Exhausted"
       Else
        -take data to be inserted say x
        -Create an empty node p and assign data x to its info field
         i.e. p->info = x;
               p->next = NULL
               if(top!=NULL)
                 p->next = top
3. For next push operation, goto step 2.
4. For pop operation, 
    if top = NULL
      print "stack is empty"
    else
      -temp = top
      -top = top->next
      -display popped item as top->info
      -delete temp
5. For next pop operation goto step 4
 
 
 
Program :- 
 
 
 
#include<iostream>

#include<cstdlib>

#include<malloc.h>

#include<conio.h>

using namespace std;

struct node{

    int info;

    struct node *next;

};

class stack{

    struct node *top;

    public:

        stack();

        void push();

        void pop();

        void display();

};

stack::stack(){

    top = NULL;

}

void stack::push(){

    int data;

    struct node *p;

    if((p=(node*)malloc(sizeof(node)))==NULL){

        cout<<"Memory Exhausted";

        exit(0);

    }

    cout<<"Enter a Number to insert:";

    cin>>data;

    p = new node;

    p->info = data;

    p->next = NULL;

    if(top!=NULL){

        p->next = top;

    }

    top = p;

    cout<<"\nNew item inserted"<<endl;

}

void stack::pop(){

    struct node *temp;

    if(top==NULL){

        cout<<"\nThe stack is Empty"<<endl;

    }else{

        temp = top;

        top = top->next;

        cout<<"\nThe value popped is "<<temp->info<<endl;

        delete temp;

    }

}

void stack::display(){

    struct node *p = top;

    if(top==NULL){

        cout<<"\nNothing to Display\n";

    }else{

        cout<<"\nThe contents of Stack\n";

        while(p!=NULL){

            cout<<p->info<<endl;

            p = p->next;

        }

    }

}

int main(){

    stack s;

    int choice;

    do{

        cout<<"\nEnter your choice:";

        cout<<"\n1. PUSH\n2. POP\n3. DISPLAY\n4. EXIT\n";

        cin>>choice;

        switch(choice){

            case 1:

                s.push();

                break;

            case 2:

                s.pop();

                break;

            case 3:

                s.display();

                break;

            case 4:

                exit(0);

                break;

            default:

                cout<<"Invalid Choice";

                break;

        }

    }while(choice);

    getch();

    return 0;

}
 
users online