C Program to to implement Stack

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

What is a stack?

In this c program, we have to implement a stack. The stack is a linear data structure that uses lifo last in first out to perform push(insert operation), pop(delete operation), and display stack. For that, we have to know more about the stack and its functions. The stack is simply a linear data structure in which the operations are performed in a particular order. In this program, we use 3 operations. They are:

  1. Use three Functions for operations like Push, Pop, and Display.
  2. Uses a Switch case statement for accessing the functions
  3. Exit

Which are the operations performed on the stack?

There are mainly three operations are performed in a stack. They are:

  • Push - Adds an item into stock. If the stack is full by doing this, then it is said to be an overflow condition
  • Pop - It is just the opposite of the push function that is it removes an item from the stack. Here the items appear in the reverse order in which they are pushed. Here an underflow condition occurs when the stack is empty.
  • IsEmpty - It returns true if the stack is empty otherwise false

How to implement a stack in C?

We can implement stack mainly in two ways. They are

  • Using array
  • Using a linked list

How push, pop, and the display is done in the stack?

In this c program, after importing the header files into the program, we declare the variables and structure for the stack and push, pop, and display functions. Now we display the menu for the user to select the operation to do on the stack. Use a switch case statement to select the operation on the stack. Select the case depends on the user's choice. Now we call the respective function to do the operation like push pop or display.

In a push operation, we declare a variable, and we give the user a window to enter the element. Now insert the element into the stack. Similarly, we give the user a window to delete. Check the stack is not empty before deleting anything. If the stack is not empty, delete the top and make the top and top -1. Finally, display the stack elements using a for loop.

ALGORITHM

STEP 1: Include the header files to use the built-in header files into the C program.

STEP 2: Declare the structured stack with members stk[], top.

STEP 3: Declare the variable s of type STACK.

STEP 4: Declare the function void push(void),void pop(void),void display(void).

STEP 5: Declare the variable choice, option as an integer.

STEP 6: Set s.top=-1 and display STACK OPERATION

STEP 7: Using a while loop with the options display the menu as 1 for push,2 for pop,3 for display 4 for the exit.

STEP 8: Read the choice into the variable choice.

STEP 9: If the choice is 1 then call the function push().

STEP 10: If the choice is 2 then call the function pop().

STEP 11: If the choice is 3 then call the function display().

STEP 12: If the choice is 4, then call the return.

STEP 13: Display You want to continue and read the option into the option. And repeat step 8.

Function void push()

STEP 1: Declare the integer variable num.

STEP 2: Check if s.top=MAXSIZE-1 then display stack is full and return else do step 3.

STEP 3: Read the element to be pushed into a variable num,increment s.top=s.top+1 and place s.stk[s.top]=num and return.

Function void pop()

STEP 1: Declare the integer variable num.

STEP 2: Check if s.top=-1 then display stack is empty and return s.top else do step 3.

STEP 3: Set num=s.stk[s.top].

STEP 4: Display popped element as s.stk[s.top]

STEP 5: Decrement s.top=s.top-1.

STEP 5: Return num.

Function void display()

STEP 1: Declare the integer variable i.

STEP 2: Check if s.top=-1 then display stack is empty and return s.top else do step 3.

STEP 3: Display the status of the stack is.

STEP 4: By using a for loop with condition i>=0 display s.stk[i].

STEP 5: Decrement 'i' by one. And repeat step 4.

C Source Code

                                          #include <stdio.h>


#define MAXSIZE 5
struct stack /* Structure definition for stack */ {
  int stk[MAXSIZE];
  int top;
};
typedef struct stack STACK;
STACK s;
void push(void); /* Function declaration/Prototype*/
int pop(void);
void display(void);
void main() {
  int choice;
  int option;

  s.top = -1;
  printf("STACK OPERATION\n");
  while (option) {
    printf("------------------------------------------\n");
    printf("   1 --> PUSH            \n");
    printf("   2 --> POP            \n");
    printf("   3 --> DISPLAY            \n");
    printf("   4 --> EXIT     \n");
    printf("------------------------------------------\n");
    printf("Enter your choice\n");
    scanf("%d", & choice);
    switch (choice) {
    case 1:
      push();
      break;
    case 2:
      pop();
      break;
    case 3:
      display();
      break;
    case 4:
      return;
    }
    fflush(stdin);
    printf("Do you want to continue(Type 0 or 1)?\n");
    scanf("%d", & option);
  }
}
void push() /*Function to add an element to the stack*/ {
  int num;
  if (s.top == (MAXSIZE - 1)) {
    printf("Stack is Full\n");
    return;
  } else {
    printf("Enter the element to be pushed\n");
    scanf("%d", & num);
    s.top = s.top + 1;
    s.stk[s.top] = num;
  }
  return;
}
int pop() /*Function to delete an element from the stack*/ {
  int num;
  if (s.top == -1) {
    printf("Stack is Empty\n");
    return (s.top);
  } else {
    num = s.stk[s.top];
    printf("poped element is = %d\n", s.stk[s.top]);
    s.top = s.top - 1;
  }
  return (num);
}
void display() /*Function to display the status of the stack*/ {
  int i;
  if (s.top == -1) {
    printf("Stack is empty\n");
    return;
  } else {
    printf("\nThe status of the stack is\n");
    for (i = s.top; i >= 0; i--) {
      printf("%d\n", s.stk[i]);
    }
  }
  printf("\n");
}
                                      

OUTPUT

STACK OPERATION
------------------------------------------
 1       -->     PUSH

        2       -->     POP

 3       -->     DISPLAY

        4       -->     EXIT
------------------------------------------
Enter your choice
1

Enter the element to be pushed
23

Do you want to continue(Type 0 or 1)?
1
------------------------------------------
 1       -->     PUSH

        2       -->     POP

 3       -->     DISPLAY

        4       -->     EXIT
------------------------------------------
Enter your choice
1

Enter the element to be pushed
45

Do you want to continue(Type 0 or 1)?
1
------------------------------------------
    1       -->     PUSH

        2       -->     POP

    3       -->     DISPLAY

        4       -->     EXIT
------------------------------------------
Enter your choice
1

Enter the element to be pushed
78

Do you want to continue(Type 0 or 1)?
1
------------------------------------------
        1       -->     PUSH

        2       -->     POP

 3       -->     DISPLAY

        4       -->     EXIT
------------------------------------------
Enter your choice
3

The status of the stack is
78
45
23

Do you want to continue(Type 0 or 1)?
1
------------------------------------------
        1       -->     PUSH

 2       -->     POP

        3       -->     DISPLAY

        4       -->     EXIT
------------------------------------------
Enter your choice
2

poped element is = 78
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
        1       -->     PUSH

        2       -->     POP

        3       -->     DISPLAY

        4       -->     EXIT
------------------------------------------
Enter your choice
3

The status of the stack is
45
23
Do you want to continue(Type 0 or 1)?
0