top of page

Stack Implementation using class/LinkedList.


Stack:

A stack is an abstract data structure that contains a collection of elements.

He order may be LIFO(Last In First Out) or FILO(First In Last Out).

Mainly the following three basic operations are performed in the stack:

  • Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.

  • Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.

  • Peek or Top: Returns top element of stack.

  • isEmpty: Returns true if stack is empty, else false.

Using Class:

#include<iostream>
using namespace std;

class node {
    public:
        int data;
        node* next;
};

class stack {
    private:
        node* top;
    public:
        stack(){
            top = NULL;
        }
        void push(int data)
        {
            node* newnode = new node;
            newnode->data = data;
            newnode->next = top;
            top = newnode;
        }
        int pop()
        {
            if(!(top == NULL))
            {
                int value = top->data;
                node* temp = top;
                top = temp->next;
                return value;
            }
            else {
                cout<<"Stack is Empty"<<endl;
                exit(1);
            }
        }
};

int main()
{
    stack *obj = new stack();
    int choice;
    while(1)
    {
        cout<<"1. Push operation"<<endl;
        cout<<"2. Pop Operation"<<endl;
        cout<<"3. Exit from program"<<endl;
        cout<<"Enter the choice"<<endl;
        cin>>choice;
        switch(choice)
        {
            int element;
            case 1 :
                cout<<"Enter the data"<<endl;
                cin>>element;
                obj->push(element);
                break;
            case 2 :
                element = obj->pop();
                cout<<element<<endl;
                break;
            case 3:
                exit(1);
            default:
                cout<<"Please choose correct option"<<endl;
                
        }
    }
    delete obj;
    return 0;
}

Using LinkedList:

Using LinkedList:
// Example program
#include<iostream>
using namespace std;

typedef struct nodetype {
    public:
        int data;
        nodetype* next;
}node;

node* top = NULL;

void push(int data)
{
    node* newnode = (node*)malloc(sizeof(node));
    newnode->data = data;
    newnode->next = top;
    top = newnode;
}

int pop()
{
    if(top != NULL)
    {
        node* temp;
        int value = top->data;
        temp = top->next;
        top = temp;
        return value;
    }
    else {
        cout<<"Stack is Empty"<<endl;
    }
}

int main()
{
    int choice;
    while(1)
    {
        cout<<"1. Push operation"<<endl;
        cout<<"2. Pop Operation"<<endl;
        cout<<"3. Exit from program"<<endl;
        cout<<"Enter the choice"<<endl;
        cin>>choice;
        switch(choice)
        {
            int element;
            case 1 :
                cout<<"Enter the data"<<endl;
                cin>>element;
                push(element);
                break;
            case 2 :
                element = pop();
                cout<<element<<endl;
                break;
            case 3:
                exit(1);
            default:
                cout<<"Please choose correct option"<<endl;
                
        }
    }
    return 0;
}

Comments


bottom of page