stack using array in c code



Before going through stack using array in c code, let us know what is stack? Stack is a linear data structure  implements as a last in first out data operation.

stack using array in c code

#define MAX 4 //you can take any number to limit your stack size
#include<stdio.h>
#include<conio.h>

int stack[MAX];
int top;

void push(int token)
{
    char a;
    if(top==MAX-1)
    {
        printf("Stack full");
        return;
    }
    do
    {
        printf("\nEnter the token to be inserted:");
        scanf("%d",&token);
        top=top+1;
        stack[top]=token;
        printf("do you want to continue insertion Y/N");
        a=getch();
    }
    while(a=='y');
}

int pop()
{
     int t;
     if(top==-1) 
     {
          printf("Stack empty");
          return -1;
     }
     t=stack[top];
     top=top-1;
     return t;
}

void show()
{
     int i;
     printf("\nThe Stack elements are:");
     for(i=0;i<=top;i++)
     {
          printf("%d",stack[i]);
     }
}

int main()
{
     char ch , a='y';
     int choice, token;
     top=-1;
     printf("1.Insert");
     printf("\n2.Delete");
     printf("\n3.show or display");
     do
     {
          printf("\nEnter your choice for the operation: ");
          scanf("%d",&choice);
          switch(choice)
          {
               case 1:
               {
               push(token);
               show();
               break;
               }              
               case 2: 
               {
               token=pop();
               printf("\nThe token deleted is %d",token);
               show();
               break;
               }             
               case 3:
               {
               show();
               break;
               }
               default:printf("Wrong choice");
               break;
         }
         printf("\nDo you want to continue(y/n):");
         ch=getch();
     }
     while(ch=='y'||ch=='Y');
     getch();
}

Output of stack using array in c :

stack using array in c



"Please Do Like Facebook Page and follow us on Twitter so that you can actively participate and develop skills in programming. If you find above post interesting do share the webpage."
This entry was posted in c program and tagged , , . Bookmark the permalink.

2 Responses to stack using array in c code

  1. vishal Annadate says:

    this program doesn’t work properly reason:
    1. i’ll continue to work even after we’ve crossed max range
    solution: in push() use instead of previously written code:
    do
    {
    if(top==MAX-1)
    {
    printf(“Stack full”);
    return;
    }

    printf(“\nEnter the token to be inserted:”);
    scanf(“%d”,&token);
    top=top+1;
    stack[top]=token;
    printf(“do you want to continue insertion Y/N”);
    a=getch();
    }
    while(a==’y');

  2. balamurugan says:

    how could we implement 3 stacks using array in c program. such that stack overflow will not occur

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>