Stack (Linked List Implementation), Code ↔ Language

Creation of Stack (Linked List Implementation)

class Stack {
private:
struct Node {
int data;
Node* next;
Node(int val) {
data = val;
next = nullptr;
}
};

Node* top;

pu…


This content originally appeared on DEV Community and was authored by Harsh Mishra

Creation of Stack (Linked List Implementation)

class Stack {
private:
    struct Node {
        int data;
        Node* next;
        Node(int val) {
            data = val;
            next = nullptr;
        }
    };

    Node* top;

public:
    // Constructor to initialize stack
    Stack() {
        top = nullptr;
    }
};
  1. First, we will create a class named Stack.

  2. It will contain a struct Node with the following properties:

    • int data: Stores the value of the node.
    • Node* next: Points to the next node in the stack.
    • Node(int val): Constructor to initialize data with val and next with nullptr.
  3. It will contain a property top:

    • Node* top: Points to the top node of the stack.
  4. We will declare a constructor Stack():

    • It will initialize the top pointer to nullptr, indicating an empty stack.

Push Operation

void push(int item) {
    Node* newNode = new Node(item);
    newNode->next = top;
    top = newNode;
}

We will define a method push(int item):

  • It will take an integer item as input, which is the value to be pushed onto the stack.
  • We will create a new Node with item as its value.
  • We will set the next pointer of the new node to the current top node.
  • We will update top to point to the new node.

Pop Operation

int pop() {
    if (isEmpty()) {
        cout << "Underflow: Stack is empty.\n";
        return -1;
    }
    Node* temp = top;
    int poppedValue = temp->data;
    top = top->next;
    delete temp;
    return poppedValue;
}

We will define a method pop():

  • It will return an integer, which is the value removed from the top of the stack.
  • We will check if the stack is empty (using isEmpty()); if so, it will print "Underflow: Stack is empty." and return -1.
  • Otherwise, we will:
    • Store the top node in a temporary pointer temp.
    • Retrieve the value from temp and store it in poppedValue.
    • Update top to point to the next node in the stack.
    • Delete the temp node to free up memory.
    • Return the poppedValue.

Peek Operation

int peek() {
    if (isEmpty()) {
        cout << "Stack is empty.\n";
        return -1;
    }
    return top->data;
}

We will define a method peek():

  • It will return an integer, which is the value at the top of the stack.
  • We will check if the stack is empty (using isEmpty()); if so, it will print "Stack is empty." and return -1.
  • Otherwise, it will return the value of the top node.

isEmpty Operation

bool isEmpty() {
    return top == nullptr;
}

We will define a method isEmpty():

  • It will return a boolean value indicating whether the stack is empty.
  • It will return true if top is nullptr, indicating that the stack is empty.
  • Otherwise, it will return false.

Full Code Implementation

#include <iostream>
using namespace std;

class Stack {
private:
    struct Node {
        int data;
        Node* next;
        Node(int val) {
            data = val;
            next = nullptr;
        }
    };

    Node* top;

public:
    // Constructor to initialize stack
    Stack() {
        top = nullptr;
    }

    // Destructor to free allocated memory
    ~Stack() {
        while (!isEmpty()) {
            pop();
        }
    }

    // Push operation
    void push(int item) {
        Node* newNode = new Node(item);
        newNode->next = top;
        top = newNode;
    }

    // Pop operation
    int pop() {
        if (isEmpty()) {
            cout << "Underflow: Stack is empty.\n";
            return -1;
        }
        Node* temp = top;
        int poppedValue = temp->data;
        top = top->next;
        delete temp;
        return poppedValue;
    }

    // Peek operation
    int peek() {
        if (isEmpty()) {
            cout << "Stack is empty.\n";
            return -1;
        }
        return top->data;
    }

    // Check if stack is empty
    bool isEmpty() {
        return top == nullptr;
    }
};


This content originally appeared on DEV Community and was authored by Harsh Mishra


Print Share Comment Cite Upload Translate Updates
APA

Harsh Mishra | Sciencx (2024-08-04T14:37:31+00:00) Stack (Linked List Implementation), Code ↔ Language. Retrieved from https://www.scien.cx/2024/08/04/stack-linked-list-implementation-code-%e2%86%94-language/

MLA
" » Stack (Linked List Implementation), Code ↔ Language." Harsh Mishra | Sciencx - Sunday August 4, 2024, https://www.scien.cx/2024/08/04/stack-linked-list-implementation-code-%e2%86%94-language/
HARVARD
Harsh Mishra | Sciencx Sunday August 4, 2024 » Stack (Linked List Implementation), Code ↔ Language., viewed ,<https://www.scien.cx/2024/08/04/stack-linked-list-implementation-code-%e2%86%94-language/>
VANCOUVER
Harsh Mishra | Sciencx - » Stack (Linked List Implementation), Code ↔ Language. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/04/stack-linked-list-implementation-code-%e2%86%94-language/
CHICAGO
" » Stack (Linked List Implementation), Code ↔ Language." Harsh Mishra | Sciencx - Accessed . https://www.scien.cx/2024/08/04/stack-linked-list-implementation-code-%e2%86%94-language/
IEEE
" » Stack (Linked List Implementation), Code ↔ Language." Harsh Mishra | Sciencx [Online]. Available: https://www.scien.cx/2024/08/04/stack-linked-list-implementation-code-%e2%86%94-language/. [Accessed: ]
rf:citation
» Stack (Linked List Implementation), Code ↔ Language | Harsh Mishra | Sciencx | https://www.scien.cx/2024/08/04/stack-linked-list-implementation-code-%e2%86%94-language/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.