implementation of stack using array

C Program to implement stack using array.

 

#include<stdio.h>
void push(int);
int pop(void);
void display(void);

int main()
{

int choice=0,val=0;
do
{

printf("\n\t1.Push 2.Pop 3.Display 4.Exit\n\tSelect Your Choice : ");
scanf("%d",&choice);
switch(choice)
{

case 1:

printf("\tElement to be Pushed : ");
scanf("%d",&val);
push(val);
break;

case 2:

val=pop();
if(val!=-1)
printf("\tPopped Element : %d\n",val);
break;

case 3:

display();
break;

case 4:

break;

default:

printf("\tWrong Choice");
break;

}

}while(choice!=4);

return 0;

}

#define maxsize 100
int stack[maxsize];
int stacktop=0;

void push(int val)
{

if(stacktop<maxsize)

stack[stacktop++]=val;

else

printf("Stack Overflow");

}

int pop()
{

int a;
if(stacktop>0)
{

stacktop–;
a=stack[stacktop];

return a;

}
else
{

printf("Stack is Empty");
return -1;

}

}

void display()
{

int i=0;
if(stacktop>0)
{

printf("\tElements are:");
while(i<stacktop)
{
printf("\t%d",stack[i++]);
}
printf("\n");

}

else

printf("\tStack is Empty\n");

}


Keys : implentation of stack using arrays, stack using array, stack using c, stack implementation using array,implement stack using array in c,array and stack c programming, stack using array c.

VN:F [1.9.22_1171]
Rating: 7.4/10 (94 votes cast)
VN:F [1.9.22_1171]
Rating: +26 (from 46 votes)
implementation of stack using array, 7.4 out of 10 based on 94 ratings

05. August 2008 by Jishnu
Categories: C Programming | Tags: , , | 13 comments

Comments (13)

  1. its really nice

  2. its easy to learn.

  3. its easy to learn.

  4. please try this code
    #include "stdafx.h"

    # define max 5
    int stack_array[max];
    int y=0;
    void push( int x)
    {

    if(y>=max)
    {
    printf("stack overflow\n");
    }
    else
    {
    stack_array[y]=x;
    y+=1;
    }

    }
    void pop()
    {
    if(y<=0)
    {
    printf("stack is empty\n");
    }
    else
    {
    printf("%d\n",stack_array[y-1]);
    y=y-1;
    }

    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    push(10);
    push(20);
    push(30);
    push(40);
    push(50);
    push(60);
    pop();
    pop();
    pop();
    pop();
    pop();
    pop();
    pop();
    push(10);
    push(20);
    push(30);
    push(40);
    push(50);
    push(60);
    pop();
    pop();
    pop();
    pop();
    pop();
    pop();
    pop();

    return 0;
    }

  5. very good

  6. its too good

  7. its very nice&very useful

  8. While pushing the element also, the the stacktop variable should point to the newly added value.You have pointed the stacktop variable while popping the element.Plz clarify me …….

  9. its good

  10. Nice code.

  11. helpfull ….. muaaaa

  12. its a very helpful one…. thanx a loooooooottt….!!!!!

Leave a Reply