Stack Linked List Example Program Using Functions in C++


Definition

A stack is a basic computer science data structure and can be defined in an abstract, implementation-free manner, or it can be generally defined as a linear list of items in which all additions and deletion are restricted to one end that is Top.

Stack Linked List Operations

  • Push data in linked list Stack top position
  • Pop data in linked list Stack based top position
  • Display Nodes in Linked List Stack
  • Count Nodes in Linked List Stack

Stack Linked List Example C++ Program

 

/* Stack Program C++, Singly Linked List Example - Example Program Using Functions in C++*/
/* Data Structure C++ Programs,Stack Programs,C++ Linked List Examples */
/* Stack Singly Linked List Example C++ - Push,Pop,Display and Count C++ Operations*/

#include <iostream>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>

using namespace std;

#define MAX_SIZE 5

struct node {
    int value;
    struct node *next;
};

void push();
void display();
void pop();
int count();

typedef struct node DATA_NODE;

DATA_NODE *head_node, *first_node, *temp_node = 0, *prev_node, next_node;
int data;

int main() {
    int option = 0;

    cout << "Stack Linked List C++ Example - All Operations\n";

    while (option < 5) {

        cout << "\nOptions\n";
        cout << "1 : Push into Linked List Stack\n";
        cout << "2 : Pop from Linked List Stack\n";
        cout << "3 : Display Linked List Stack\n";
        cout << "4 : Count Linked List Stack\n";
        cout << "Others : Exit()\n";
        cout << "Enter your option:";
        cin>>option;

        switch (option) {
            case 1:
                push();
                break;
            case 2:
                pop();
                break;
            case 3:
                display();
                break;
            case 4:
                count();
                break;
            default:
                break;
        }
    }

    return 0;
}

void push() {
    int pos;
    pos = count();
    if (pos == MAX_SIZE) {
        cout << "\nStack Linked List Is Full \n";
    } else {
        cout << "\nEnter Element for Push Linked List : \n";
        cin>>data;

        temp_node = (DATA_NODE *) malloc(sizeof (DATA_NODE));

        temp_node->value = data;

        if (first_node == 0) {
            first_node = temp_node;
        } else {
            head_node->next = temp_node;
        }
        temp_node->next = 0;
        head_node = temp_node;
        fflush(stdin);
    }
}

void pop() {
    int pos, i = 0;
    pos = count();
    temp_node = first_node;

    if (pos > 0) {
        if (pos == 1) {
            temp_node = temp_node -> next;
            first_node = temp_node;
            cout << "\n Pop from Linked List Successfully \n\n";
        } else {
            while (temp_node != 0) {
                if (i == (pos - 1)) {
                    prev_node->next = temp_node->next;
                    head_node = prev_node;
                    cout << "\n Pop from Linked List Successfully \n\n";
                    break;
                } else {
                    i++;
                    prev_node = temp_node;
                    temp_node = temp_node -> next;
                }
            }
        }
    } else
        cout << "\Empty Linked List Stack\n\n";
}

void display() {
    int count = 0;
    temp_node = first_node;
    cout << "\nDisplay Stack : Linked List : \n";
    while (temp_node != 0) {
        cout << "# " << temp_node->value;
        count++;
        temp_node = temp_node -> next;
    }
    cout << "\nNo Of Items In Linked List : %d" << count;
}

int count() {
    int count = 0;
    temp_node = first_node;
    while (temp_node != 0) {
        count++;
        temp_node = temp_node -> next;
    }
    cout << "\nNo Of Items In Linked List : %d" << count;
    return count;
}

Sample Output

Stack Linked List Example - All Operations

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:1

No Of Items In Linked List Stack : 0

Enter Element for Push Linked List :
11

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:1

No Of Items In Linked List Stack : 1

Enter Element for Push Linked List :
22

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:1

No Of Items In Linked List Stack : 2

Enter Element for Push Linked List :
33

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:1

No Of Items In Linked List Stack : 3

Enter Element for Push Linked List :
44

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:3

Display Stack : Linked List :
# 11 # # 22 # # 33 # # 44 #
No Of Items In Linked List Stack : 4

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:4

No Of Items In Linked List Stack : 4

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:2

No Of Items In Linked List Stack : 4

 Pop from Linked List Successfully


Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:3

Display Stack : Linked List :
# 11 # # 22 # # 33 #
No Of Items In Linked List Stack : 3

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:2

No Of Items In Linked List Stack : 3

 Pop from Linked List Successfully


Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:1

No Of Items In Linked List Stack : 2

Enter Element for Push Linked List :
899

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:3

Display Stack : Linked List :
# 11 # # 22 # # 899 #
No Of Items In Linked List Stack : 3

Options
1 : Push into Linked List Stack
2 : Pop from Linked List Stack
3 : Display Linked List Stack
4 : Count Linked List Stack
Others : Exit()
Enter your option:5