C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Stack using linked list

C Program: Stack using linked list

C

#include <stdio.h>

#include <stdlib.h>

 

// Structure for a stack node

struct Node {

    int data;

    struct Node *next;

};

 

struct Node *top = NULL;  // Global pointer to the top of the stack

 

// Function to push an element onto the stack

void push(int value) {

    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

    if (newNode == NULL) {

        printf("\nStack Overflow! Memory not allocated.\n");

        return;

    }

    newNode->data = value;

    newNode->next = top;  // Link the new node to the previous top

    top = newNode;        // Update top pointer

    printf("\n%d pushed to stack.\n", value);

}

 

// Function to pop an element from the stack

void pop() {

    if (top == NULL) {

        printf("\nStack Underflow! No elements to pop.\n");

        return;

    }

    struct Node *temp = top;

    printf("\n%d popped from stack.\n", top->data);

    top = top->next;  // Move top to next node

    free(temp);       // Free memory

}

 

// Function to display stack elements

void display() {

    if (top == NULL) {

        printf("\nStack is empty.\n");

        return;

    }

    struct Node *temp = top;

    printf("\nStack elements are:\n");

    while (temp != NULL) {

        printf("%d\n", temp->data);

        temp = temp->next;

    }

}

 

// Function to peek at the top element

void peek() {

    if (top == NULL)

        printf("\nStack is empty.\n");

    else

        printf("\nTop element is: %d\n", top->data);

}

 

// Main function

int main() {

    int choice, value;

 

    printf("=== STACK IMPLEMENTATION USING LINKED LIST ===\n");

 

    while (1) {

        printf("\nMenu:\n");

        printf("1. Push\n");

        printf("2. Pop\n");

        printf("3. Peek\n");

        printf("4. Display\n");

        printf("5. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        switch (choice) {

            case 1:

                printf("Enter value to push: ");

                scanf("%d", &value);

                push(value);

                break;

            case 2:

                pop();

                break;

            case 3:

                peek();

                break;

            case 4:

                display();

                break;

            case 5:

                printf("\nExiting program.\n");

                exit(0);

            default:

                printf("\nInvalid choice! Try again.\n");

        }

    }

 

    return 0;

}

Output

 
OUTPUT :
=== STACK IMPLEMENTATION USING LINKED LIST ===

Menu:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter value to push: 10
10 pushed to stack.

Enter your choice: 1
Enter value to push: 20
20 pushed to stack.

Enter your choice: 4
Stack elements are:
20
10

Enter your choice: 2
20 popped from stack.

Enter your choice: 3
Top element is: 10

Explanation

Operation

Description

Push

Creates a new node and places it on top of the stack.

Pop

Removes the top node and frees memory.

Peek

Displays the value at the top of the stack.

Display

Traverses and prints all nodes from top to bottom.