Write a program to implement stack using linked list in C++.
In this we are going to see a program to  implement stack using linked list in C++ Programming Language.



The Code given below can be used in TURBO C++ Compilers: -


#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *head;
void push()
{
    struct node *ptr;
    int item;
    ptr = (struct node *)malloc(sizeof(struct node *));
    cout << "Enter a value: ";
    cin >> item;
    ptr->data = item;
    ptr->next = head;
    head = ptr;
    cout << "Value pushed" << endl;
}
void pop()
{
    struct node *ptr;
    int item;
    if (head == NULL)
        cout << "Underflow" << endl;
    else
    {
        item = head->data;
        ptr = head;
        cout << item << endl;
        head = head->next;
        free(ptr);
        cout << "Value popped" << endl;
    }
}
void display()
{
    struct node *ptr;
    ptr = head;
    cout << "Values are: ";
    while (ptr != NULL)
    {
        cout << ptr->data << " ";
        ptr = ptr->next;
    }
}
void main()
{
    clrscr();
    int choice;
    cout << "MENU\n1. Push Element\n2. Pop Element\n3. Display\n4. Exit\n";
    do
    {
        cout << "\nEnter your choice: ";
        cin >> choice;
        switch (choice)
        {
        case 1:
            push();
            break;
        case 2:
            pop();
            break;
        case 3:
            display();
            break;
        case 4:
            exit(1);
            break;
        default:
            cout << "Invalid choice" << endl;
        }
    } while (choice != 4);
    getch();
}

The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
#include <stdlib.h>
using namespace std;

struct node
{
    int data;
    struct node *next;
};
struct node *head;
void push()
{
    struct node *ptr;
    int item;
    ptr = (struct node *)malloc(sizeof(struct node *));
    cout << "Enter a value: ";
    cin >> item;
    ptr->data = item;
    ptr->next = head;
    head = ptr;
    cout << "Value pushed" << endl;
}
void pop()
{
    struct node *ptr;
    int item;
    if (head == NULL)
        cout << "Underflow" << endl;
    else
    {
        item = head->data;
        ptr = head;
        cout << item << endl;
        head = head->next;
        free(ptr);
        cout << "Value popped" << endl;
    }
}
void display()
{
    struct node *ptr;
    ptr = head;
    cout << "Values are: ";
    while (ptr != NULL)
    {
        cout << ptr->data << " ";
        ptr = ptr->next;
    }
}
int main()
{
    int choice;
    cout << "MENU\n1. Push Element\n2. Pop Element\n3. Display\n4. Exit\n";
    do
    {
        cout << "\nEnter your choice: ";
        cin >> choice;
        switch (choice)
        {
        case 1:
            push();
            break;
        case 2:
            pop();
            break;
        case 3:
            display();
            break;
        case 4:
            exit(0);
            break;
        default:
            cout << "Invalid choice" << endl;
        }
    } while (choice != 4);
    return 0;
}


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post