Program to implement Stack using Arrays in C C++

In this post we will write a program to implement Stack using Arrays. Let’s first understand what is Stack:

Stack:

A stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed. The order in which elements come off a stack gives rise to its alternative name, LIFO (for last in, first out).

Below image will make it clear:


implement stack using arrays

There are two ways to implement Stacks:

  1. Using Array
  2. Using Linked List

Here I’ve discussed how to implement Stacks, below the code in C for the same.

Program to implement stack using Arrays:

#include<stdio.h>
#include<process.h>

#define MAXSIZE 10

void push();
int pop();
void traverse();
int stack[MAXSIZE];
int Top=-1;

int main()
{
    int choice;
    char ch;
    do
    {
        printf("\n1. PUSH ");
        printf("\n2. POP ");
        printf("\n3. TRAVERSE ");
        printf("\nEnter your choice ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1: push();
                break;
            case 2: printf("\nThe deleted element is %d ",pop());
                break;
            case 3: traverse();
                break;
            default: printf("\nYou Entered Wrong Choice");
        }
        printf("\nDo You Wish To Continue (Y/N)");
        fflush(stdin);
        scanf("%c",&ch);
    }
    while(ch=='Y' || ch=='y');
    return 0;
}

void push()
{
    int item;
    if(Top == MAXSIZE - 1)
    {
        printf("\nThe Stack Is Full");
        exit(0);
    }
    else
    {
        printf("Enter the element to be inserted ");
        scanf("%d",&item);
        Top= Top+1;
        stack[Top] = item;
    }
}

int pop()
{
    int item;
    if(Top == -1)
    {
        printf("The stack is Empty");
        exit(0);
    }
    else
    {
        item = stack[Top];
        Top = Top-1;
    }
    return(item);
}

void traverse()
{
    int i;
    if(Top == -1)
    {
        printf("The Stack is Empty");
        exit(0);
    }
    else
    {
        for(i=Top;i>=0;i--)
        {
            printf("Traverse the element ");
            printf("%dn",stack[i]);
        }
    }
}

 

OUTPUT:

1. PUSH
2. POP
3. TRAVERSE
Enter your choice 1
Enter the element to be inserted 4

Do You Wish To Continue (Y/N)y

1. PUSH
2. POP
3. TRAVERSE
Enter your choice 1
Enter the element to be inserted 6

Do You Wish To Continue (Y/N)y

1. PUSH
2. POP
3. TRAVERSE
Enter your choice 3
Traverse the element 6
Traverse the element 4

Do You Wish To Continue (Y/N)y

1. PUSH
2. POP
3. TRAVERSE
Enter your choice 1
Enter the element to be inserted 8

Do You Wish To Continue (Y/N)y

1. PUSH
2. POP
3. TRAVERSE
Enter your choice 2

The deleted element is 8
Do You Wish To Continue (Y/N)y

1. PUSH
2. POP
3. TRAVERSE
Enter your choice 3
Traverse the element 6
Traverse the element 4

Do You Wish To Continue (Y/N)n

--------------------------------
Process exited after 43.2 seconds with return value 0
Press any key to continue . . .

 

Comment if you face any problem.

1 thought on “Program to implement Stack using Arrays in C C++”

Leave a Comment