C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Dynamic Memory Allocation in C

Stack using dynamic memory

C Program: Stack Implementation Using Dynamic Memory (malloc)

C

#include <stdio.h>

#include <stdlib.h>

 

// Structure for Stack

struct Stack {

    int *arr;

    int top;

    int capacity;

};

 

// Function prototypes

void initialize(struct Stack *stack, int capacity);

int isFull(struct Stack *stack);

int isEmpty(struct Stack *stack);

void push(struct Stack *stack, int value);

int pop(struct Stack *stack);

int peek(struct Stack *stack);

void display(struct Stack *stack);

 

int main() {

    struct Stack stack;

    int capacity, choice, value;

 

    printf("Enter the maximum size of stack: ");

    scanf("%d", &capacity);

 

    initialize(&stack, capacity);

 

    do {

        printf("\n--- Stack Operations Menu ---\n");

        printf("1. Push\n");

        printf("2. Pop\n");

        printf("3. Peek (Top Element)\n");

        printf("4. Display Stack\n");

        printf("0. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        switch (choice) {

            case 1:

                printf("Enter value to push: ");

                scanf("%d", &value);

                push(&stack, value);

                break;

            case 2:

                value = pop(&stack);

                if (value != -1)

                    printf("Popped value: %d\n", value);

                break;

            case 3:

                value = peek(&stack);

                if (value != -1)

                    printf("Top element: %d\n", value);

                break;

            case 4:

                display(&stack);

                break;

            case 0:

                printf("Exiting program.\n");

                break;

            default:

                printf("Invalid choice!\n");

        }

    } while (choice != 0);

 

    // Free dynamically allocated memory

    free(stack.arr);

 

    return 0;

}

 

// Function to initialize stack

void initialize(struct Stack *stack, int capacity) {

    stack->capacity = capacity;

    stack->top = -1;

    stack->arr = (int *) malloc(stack->capacity * sizeof(int));

 

    if (stack->arr == NULL) {

        printf("Memory allocation failed!\n");

        exit(1);

    }

}

 

// Check if stack is full

int isFull(struct Stack *stack) {

    return stack->top == stack->capacity - 1;

}

 

// Check if stack is empty

int isEmpty(struct Stack *stack) {

    return stack->top == -1;

}

 

// Push operation

void push(struct Stack *stack, int value) {

    if (isFull(stack)) {

        printf("Stack Overflow! Cannot push %d\n", value);

        return;

    }

    stack->arr[++stack->top] = value;

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

}

 

// Pop operation

int pop(struct Stack *stack) {

    if (isEmpty(stack)) {

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

        return -1;

    }

    return stack->arr[stack->top--];

}

 

// Peek operation

int peek(struct Stack *stack) {

    if (isEmpty(stack)) {

        printf("Stack is empty!\n");

        return -1;

    }

    return stack->arr[stack->top];

}

 

// Display stack elements

void display(struct Stack *stack) {

    if (isEmpty(stack)) {

        printf("Stack is empty!\n");

        return;

    }

 

    printf("\nStack elements (Top to Bottom):\n");

    for (int i = stack->top; i >= 0; i--) {

        printf("%d\n", stack->arr[i]);

    }

}

Output

 
OUTPUT :
Enter the maximum size of stack: 3

--- Stack Operations Menu ---
1. Push
2. Pop
3. Peek (Top Element)
4. Display Stack
0. 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 (Top to Bottom):
20
10

Enter your choice: 2
Popped value: 20

Enter your choice: 3
Top element: 10


Explanation

Function

Description

initialize()

Allocates memory for stack using malloc().

push()

Adds an element to the top if not full.

pop()

Removes and returns the top element.

peek()

Displays the top element without removing it.

display()

Prints all stack elements from top to bottom.

free()

Releases dynamically allocated memory.