Skip to main content

10. Stack - Menu driven program

 

#include <stdio.h>

#include <stdlib.h>

struct MyStack
{
    int TotalSize;
    int Top;
    int *BaseAddress;
} stack;
struct Node
{
    int data;
    struct Node *next;
};
struct Node *top = NULL;
void star()
{
    int j = 0;
    printf("\n\n\t\t");
    while (j <= 5)
    {
        printf("================");
        j++;
    }
    printf("\n\n");
}
void createStack(struct MyStack *a, int tsize)
{
    (*a).TotalSize = tsize;
    (*a).Top = -1;
    (*a).BaseAddress = (int *)malloc(sizeof(int) * tsize);
}
void pushArray(struct MyStack *a)
{
    if ((*a).Top == (*a).TotalSize - 1)
    {
        printf("\n\t\t\tStack is FULL, you can't PUSH any element.");
    }
    else
    {
        (*a).Top++;
        printf("\t\t\tEnter value of element you want to push in stack: ");
        scanf("%d", &(*a).BaseAddress[(*a).Top]);
        printf("\t\t\tInsertion Successful");
    }
}
void pushLinkedList()
{
    struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
    if (temp == NULL)
    {
        printf("\t\t\tInsertion not possible...\n");
    }
    else
    {
        printf("\t\t\tEnter the value: ");
        scanf("%d", &(*temp).data);
        if (top == NULL)
            (*temp).next = NULL;
        else
            (*temp).next = (top);
        top = temp;
        printf("\t\t\tInsertion Successful");
    }
}
void popArray(struct MyStack *a)
{
    if ((*a).Top == -1)
    {
        printf("\n\t\t\tStack is EMPTY, you can't POP any element.");
    }
    else
    {
        printf("\t\t\tDeleted element: %d\n", (*a).BaseAddress[(*a).Top]);
        (*a).Top--;
    }
}
void popLinkedList()
{
    struct Node *temp;
    temp = top;
    if (top == NULL)
        printf("\t\t\tStack is EMPTY, you can't POP any element.\n");
    else
    {
        temp = top;
        top = (*top).next;
        printf("\t\t\tDeleted value: %d\n", (*temp).data);
        free(temp);
    }
}
void displayArray(struct MyStack *a)
{
    if ((*a).Top == -1)
    {
        printf("\n\t\t\tStack is EMPTY, so there is no element in STACK.");
    }
    else
    {
        printf("\t\t\tPrinting the stack...\n\t\t\t");
        for (int i = (*a).Top; i >= 0; i--)
        {
            printf("%d\n\t\t\t", (*a).BaseAddress[i]);
        }
    }
}
void displayLinkedList()
{
    struct Node *temp = top;
    if (temp == NULL)
    {
        printf("\t\t\tStack is empty\n");
    }
    else
    {
        printf("\t\t\tPrinting Stack elements \n\t\t\t");
        while (temp != NULL)
        {
            printf("%d\n\t\t\t", (*temp).data);
            (temp) = (*temp).next;
        }
    }
}
void peek(struct MyStack *a)
{
    int position;
    if ((*a).Top == -1)
    {
        printf("\n\t\t\tStack is EMPTY, so there is no element in STACK.");
    }
    else
    {
        printf("\n\t\t\tEnter the position of element you want from top: ");
        scanf("%d", &position);
        if (position > (*a).Top + 1)
            printf("\t\t\tThe stack contains only %d elements.", (*a).Top + 1);
        else if (position < 0)
            printf("\t\t\tInvalid position...");
        else
            printf("\t\t\tThe element with position %d from top in stack = %d",
            position, (*a).BaseAddress[(*a).Top - position + 1]);
    }
}
void main()
{
    int choice;
    createStack(&stack, 10);
    while (1)
    {
        star();
        printf("\t\t\t\t\tSTACK IMPLEMENTATION\n\n\t\t\tPress '1' to PUSH an element in ARRAY\n\t\t\t
        Press '2' to POP an element in ARRAY\n\t\t\tPress '3' to SHOW the elements in ARRAY\n\t\t\t
        Press '4' to show an element from top in ARRAY\n\n\t\t\tPress '5' to PUSH an element in LINKED LIST\n\t\t\t
        Press '6' to POP an element in LINKED LIST\n\t\t\tPress '7' to SHOW the elements in LINKED LIST\n\n\t\t\t
        Press 'any other key' to EXIT\n\t\t\tEnter your choice: ");
        scanf("%d", &choice);
        star();
        if (choice == 1)
        {
            pushArray(&stack);
            star();
        }
        else if (choice == 2)
        {
            popArray(&stack);
            star();
        }
        else if (choice == 3)
        {
            displayArray(&stack);
            star();
        }
        else if (choice == 4)
        {
            peek(&stack);
            star();
        }
        else if (choice == 5)
        {
            pushLinkedList();
            star();
        }
        else if (choice == 6)
        {
            popLinkedList();
            star();
        }
        else if (choice == 7)
        {
            displayLinkedList();
            star();
        }
        else
        {
            printf("\t\t\tExiting...");
            star();
            exit(0);
        }
    }
}


Output :


================================================================================================ STACK IMPLEMENTATION Press '1' to PUSH an element in ARRAY Press '2' to POP an element in ARRAY Press '3' to SHOW the elements in ARRAY Press '4' to show an element from top in ARRAY Press '5' to PUSH an element in LINKED LIST Press '6' to POP an element in LINKED LIST Press '7' to SHOW the elements in LINKED LIST Press 'any other key' to EXIT Enter your choice: 1 ================================================================================================ Enter value of element you want to push in stack: 1 Insertion Successful ================================================================================================ ================================================================================================ STACK IMPLEMENTATION Press '1' to PUSH an element in ARRAY Press '2' to POP an element in ARRAY Press '3' to SHOW the elements in ARRAY Press '4' to show an element from top in ARRAY Press '5' to PUSH an element in LINKED LIST Press '6' to POP an element in LINKED LIST Press '7' to SHOW the elements in LINKED LIST Press 'any other key' to EXIT Enter your choice: 1 ================================================================================================ Enter value of element you want to push in stack: 2 Insertion Successful ================================================================================================ ================================================================================================ STACK IMPLEMENTATION Press '1' to PUSH an element in ARRAY Press '2' to POP an element in ARRAY Press '3' to SHOW the elements in ARRAY Press '4' to show an element from top in ARRAY Press '5' to PUSH an element in LINKED LIST Press '6' to POP an element in LINKED LIST Press '7' to SHOW the elements in LINKED LIST Press 'any other key' to EXIT Enter your choice: 1 ================================================================================================ Enter value of element you want to push in stack: 3 Insertion Successful ================================================================================================ ================================================================================================ STACK IMPLEMENTATION Press '1' to PUSH an element in ARRAY Press '2' to POP an element in ARRAY Press '3' to SHOW the elements in ARRAY Press '4' to show an element from top in ARRAY Press '5' to PUSH an element in LINKED LIST Press '6' to POP an element in LINKED LIST Press '7' to SHOW the elements in LINKED LIST Press 'any other key' to EXIT Enter your choice: 1 ================================================================================================ Enter value of element you want to push in stack: 4 Insertion Successful ================================================================================================ ================================================================================================ STACK IMPLEMENTATION Press '1' to PUSH an element in ARRAY Press '2' to POP an element in ARRAY Press '3' to SHOW the elements in ARRAY Press '4' to show an element from top in ARRAY Press '5' to PUSH an element in LINKED LIST Press '6' to POP an element in LINKED LIST Press '7' to SHOW the elements in LINKED LIST Press 'any other key' to EXIT Enter your choice: 1 ================================================================================================ Enter value of element you want to push in stack: 5 Insertion Successful ================================================================================================ ================================================================================================ STACK IMPLEMENTATION Press '1' to PUSH an element in ARRAY Press '2' to POP an element in ARRAY Press '3' to SHOW the elements in ARRAY Press '4' to show an element from top in ARRAY Press '5' to PUSH an element in LINKED LIST Press '6' to POP an element in LINKED LIST Press '7' to SHOW the elements in LINKED LIST Press 'any other key' to EXIT Enter your choice: 3 ================================================================================================ Printing the stack... 5 4 3 2 1 ================================================================================================ ================================================================================================ STACK IMPLEMENTATION Press '1' to PUSH an element in ARRAY Press '2' to POP an element in ARRAY Press '3' to SHOW the elements in ARRAY Press '4' to show an element from top in ARRAY Press '5' to PUSH an element in LINKED LIST Press '6' to POP an element in LINKED LIST Press '7' to SHOW the elements in LINKED LIST Press 'any other key' to EXIT Enter your choice: 2 ================================================================================================ Deleted element: 5 ================================================================================================ ================================================================================================ STACK IMPLEMENTATION Press '1' to PUSH an element in ARRAY Press '2' to POP an element in ARRAY Press '3' to SHOW the elements in ARRAY Press '4' to show an element from top in ARRAY Press '5' to PUSH an element in LINKED LIST Press '6' to POP an element in LINKED LIST Press '7' to SHOW the elements in LINKED LIST Press 'any other key' to EXIT Enter your choice: 4 ================================================================================================ Enter the position of element you want from top: 3 The element with position 3 from top in stack = 2 ================================================================================================ ================================================================================================ STACK IMPLEMENTATION Press '1' to PUSH an element in ARRAY Press '2' to POP an element in ARRAY Press '3' to SHOW the elements in ARRAY Press '4' to show an element from top in ARRAY Press '5' to PUSH an element in LINKED LIST Press '6' to POP an element in LINKED LIST Press '7' to SHOW the elements in LINKED LIST Press 'any other key' to EXIT Enter your choice: 5 ================================================================================================ Enter the value: 1 Insertion Successful ================================================================================================ ================================================================================================ STACK IMPLEMENTATION Press '1' to PUSH an element in ARRAY Press '2' to POP an element in ARRAY Press '3' to SHOW the elements in ARRAY Press '4' to show an element from top in ARRAY Press '5' to PUSH an element in LINKED LIST Press '6' to POP an element in LINKED LIST Press '7' to SHOW the elements in LINKED LIST Press 'any other key' to EXIT Enter your choice: 5 ================================================================================================ Enter the value: 2 Insertion Successful ================================================================================================ ================================================================================================ STACK IMPLEMENTATION Press '1' to PUSH an element in ARRAY Press '2' to POP an element in ARRAY Press '3' to SHOW the elements in ARRAY Press '4' to show an element from top in ARRAY Press '5' to PUSH an element in LINKED LIST Press '6' to POP an element in LINKED LIST Press '7' to SHOW the elements in LINKED LIST Press 'any other key' to EXIT Enter your choice: 5 ================================================================================================ Enter the value: 3 Insertion Successful ================================================================================================ ================================================================================================ STACK IMPLEMENTATION Press '1' to PUSH an element in ARRAY Press '2' to POP an element in ARRAY Press '3' to SHOW the elements in ARRAY Press '4' to show an element from top in ARRAY Press '5' to PUSH an element in LINKED LIST Press '6' to POP an element in LINKED LIST Press '7' to SHOW the elements in LINKED LIST Press 'any other key' to EXIT Enter your choice: 5 ================================================================================================ Enter the value: 4 Insertion Successful ================================================================================================ ================================================================================================ STACK IMPLEMENTATION Press '1' to PUSH an element in ARRAY Press '2' to POP an element in ARRAY Press '3' to SHOW the elements in ARRAY Press '4' to show an element from top in ARRAY Press '5' to PUSH an element in LINKED LIST Press '6' to POP an element in LINKED LIST Press '7' to SHOW the elements in LINKED LIST Press 'any other key' to EXIT Enter your choice: 5 ================================================================================================ Enter the value: 5 Insertion Successful ================================================================================================ ================================================================================================ STACK IMPLEMENTATION Press '1' to PUSH an element in ARRAY Press '2' to POP an element in ARRAY Press '3' to SHOW the elements in ARRAY Press '4' to show an element from top in ARRAY Press '5' to PUSH an element in LINKED LIST Press '6' to POP an element in LINKED LIST Press '7' to SHOW the elements in LINKED LIST Press 'any other key' to EXIT Enter your choice: 7 ================================================================================================ Printing Stack elements 5 4 3 2 1 ================================================================================================ ================================================================================================ STACK IMPLEMENTATION Press '1' to PUSH an element in ARRAY Press '2' to POP an element in ARRAY Press '3' to SHOW the elements in ARRAY Press '4' to show an element from top in ARRAY Press '5' to PUSH an element in LINKED LIST Press '6' to POP an element in LINKED LIST Press '7' to SHOW the elements in LINKED LIST Press 'any other key' to EXIT Enter your choice: 6 ================================================================================================ Deleted value: 5 ================================================================================================ ================================================================================================ STACK IMPLEMENTATION Press '1' to PUSH an element in ARRAY Press '2' to POP an element in ARRAY Press '3' to SHOW the elements in ARRAY Press '4' to show an element from top in ARRAY Press '5' to PUSH an element in LINKED LIST Press '6' to POP an element in LINKED LIST Press '7' to SHOW the elements in LINKED LIST Press 'any other key' to EXIT Enter your choice: 8 ================================================================================================ Exiting... ================================================================================================

Comments