Stack Using Linked List

Implementation of a stack using linked list and its functions.

Stack Node Structure :

struct Node {
    int data;
    struct Node *next;
};

struct Node *top = NULL;

Function to Check if Stack is Empty :

int isEmpty() {
    return top == NULL;
} 

Stack Push Function :

void push(int data) {

    // Creating a new stack node
    struct Node *newNode;
    newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode -> data = data;
    
    // Set the next to point to current top
    newNode -> next = top;
    
    // Update top
    top = newNode;

}

Stack Pop Function :

Stack Peek Function :

Stack Display Function :

Contributed by Nitin Ranganath

Last updated

Was this helpful?