Friday, 24 November 2017

C++ Program to Implement Stack


Code:

#include    iostream
#include    cstdlib
using namespace std;

/*
 * Node Declaration
 */
struct node
{
    int info;
    struct node *link;    
}*top;

/*
 * Class Declaration
 */
class stack_list
{
    public:
        node *push(node *, int);
        node *pop(node *);
        void traverse(node *);
        stack_list()
        {
            top = NULL;
        }               
};

/*
 * Main: Contains Menu
 */
int main()
{
    int choice, item;
    stack_list sl;
    while (1)
    {
        cout<<"\n-------------"<
        cout<<"Operations on Stack"<
        cout<<"\n-------------"<
        cout<<"1.Push Element into the Stack"<
        cout<<"2.Pop Element from the Stack"<
        cout<<"3.Traverse the Stack"<
        cout<<"4.Quit"<
        cout<<"Enter your Choice: ";
        cin>>choice;
        switch(choice)
        {
        case 1:
            cout<<"Enter value to be pushed into the stack: ";
            cin>>item;
            top = sl.push(top, item);
            break;
        case 2:
            top = sl.pop(top);
            break;
        case 3:
            sl.traverse(top);
            break;
        case 4:
            exit(1);
            break;
        default:
            cout<<"Wrong Choice"<
        }
    }
    return 0;
}

/*
 * Push Element into the Stack
 */
node *stack_list::push(node *top, int item)
{
    node *tmp;
    tmp = new (struct node);
    tmp->info = item;
    tmp->link = top;
    top = tmp;
    return top;
}

/*
 * Pop Element from the Stack
 */
node *stack_list::pop(node *top)
{
    node *tmp;
    if (top == NULL)
        cout<<"Stack is Empty"<
    else
    {       
        tmp = top;
        cout<<"Element Popped: "<info<
        top = top->link;
        delete(tmp);
    }
    return top;
}

/*
 * Traversing the Stack
 */
void stack_list::traverse(node *top)
{       
    node *ptr;
    ptr = top;
    if (top == NULL)
        cout<<"Stack is empty"<
    else
    {
        cout<<"Stack elements :"<
        while (ptr != NULL)
        {
            cout<info<
            ptr = ptr->link;
        }
    }
}


Output:

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 3
Stack is empty

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 2
Stack is Empty

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 1
Enter value to be pushed into the stack: 100

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 3
Stack elements :
100

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 1
Enter value to be pushed into the stack: 200

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 3
Stack elements :
200
100

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 1
Enter value to be pushed into the stack: 150

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 3
Stack elements :
150
200
100

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 1
Enter value to be pushed into the stack: 50

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 3
Stack elements :
50
150
200
100

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 2
Element Popped: 50

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 3
Stack elements :
150
200
100

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 2
Element Popped: 150

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 3
Stack elements :
200
100

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 1
Enter value to be pushed into the stack: 1010

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 3
Stack elements :
1010
200
100

-------------
Operations on Stack

-------------
1.Push Element into the Stack
2.Pop Element from the Stack
3.Traverse the Stack
4.Quit
Enter your Choice: 4


------------------
(program exited with code: 1)
Press return to continue



More C++ Programs:

















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...