Stack Implementation with Linked List using C++ program

Stack implementation using Linked List C++: In this tutorial, we will learn to implement a stack using linked lists with the help of a C++ program. By IncludeHelp Last updated : July 30, 2023

A STACK is a simple Data Structure, it can be implemented as an array or as Linked List, Stack has only One TOP End, item can be pushed (add) and popped (remove) by only this End (TOP Pointer). Array follows LIFO (Last In First Out) property, it means the Item that is inserted Last will be popped first.

Stack implementation using linked list in C++

For the stack implementation using a singly linked list, you have to write all operations of the singly linked list based on the stack operations so that you can achieve the basic concept of the stack which is LIFO (Last-In-First-Out). You have to follow all the basic rules of the stack to implement various operations such as PUSH, POP, PEEK, and DISPLAY.

C++ program to implement stack using linked list

In this example we will implement STACK with Linked List using C++ program, here we will implement PUSH, POP, and TRAVERSE (Display) Operations for STACK using Singly Linked List.

#include <iostream>
#include <string.h>

using namespace std;

struct Node {
    int stu_no;
    char stu_name[50];
    int p;
    Node* next;
};
Node* top;

class stack {

public:
    void push(int n, char name[], int perc);
    void pop();
    void display();
};

void stack::push(int n, char name[], int perc)
{
    struct Node* newNode = new Node;
    //fill data part
    newNode->stu_no = n;
    newNode->p = perc;
    strcpy(newNode->stu_name, name);
    //link part
    newNode->next = top;
    //make newnode as top/head
    top = newNode;
}
void stack::pop()
{
    if (top == NULL) {
        cout << "List is empty!" << endl;
        return;
    }
    cout << top->stu_name << " is removed." << endl;
    top = top->next;
}
void stack::display()
{
    if (top == NULL) {
        cout << "List is empty!" << endl;
        return;
    }
    struct Node* temp = top;
    while (temp != NULL) {
        cout << temp->stu_no << " ";
        cout << temp->stu_name << " ";
        cout << temp->p << " ";
        cout << endl;
        temp = temp->next;
    }
    cout << endl;
}

// Main code
int main()
{
    stack s;
    char ch;
    
    do {
        int n;

        cout << "ENTER CHOICE\n"
             << "1.Push\n"
             << "2.Pop\n"
             << "3.Display\n";
        cout << "Make a choice: ";
        cin >> n;

        switch (n) {
        case 1:
            Node n;
            cout << "Enter details of the element to be pushed : \n";
            cout << "Roll Number : ";
            cin >> n.stu_no;
            cout << "Enter Name: ";
            std::cin.ignore(1);
            cin.getline(n.stu_name, 50);
            cout << "Enter Percentage: ";
            cin >> n.p;

            //push data into the stack
            s.push(n.stu_no, n.stu_name, n.p);
            break;

        case 2:
            //pop data from stack
            s.pop();
            break;

        case 3:
            //display data
            s.display();
            break;

        default:
            cout << "Invalid Choice\n";
        }

        cout << "Do you want to continue ? : ";
        cin >> ch;

    } while (ch == 'Y' || ch == 'y');

    return 0;
}

Output

ENTER CHOICE
1.Push
2.Pop 
3.Display 
Make a choice: 1
Enter details of the element to be pushed : 
Roll Number : 101 
Enter Name: PRIYA 
Enter Percentage: 99
Do you want to continue ? : y 
ENTER CHOICE
1.Push
2.Pop 
3.Display 
Make a choice: 1
Enter details of the element to be pushed : 
Roll Number : 102 
Enter Name: Mahak 
Enter Percentage: 95
Do you want to continue ? : y 
ENTER CHOICE
1.Push
2.Pop 
3.Display 
Make a choice: 3
102 Mahak 95
101 PRIYA 99

Do you want to continue ? : y 
ENTER CHOICE
1.Push
2.Pop 
3.Display 
Make a choice: 2
Mahak is removed. 
Do you want to continue ? : y 
ENTER CHOICE
1.Push
2.Pop 
3.Display 
Make a choice: 3
101 PRIYA 99

Do you want to continue ? : n 


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.