EmbLogic's Blog

push and pop program implement in stack

head    1.1;
access;
symbols;
locks
root:1.1; strict;
comment    @ * @;

1.1
date    2014.04.01.11.46.19;    author root;    state Exp;
branches;
next    ;

desc
@push and pop in stack .
@

1.1
log
@Initial revision
@
text
@#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int push(void **,int *);
int pop(void **,int *);
void display(void **,int *);
int main()
{
void *astack;
int atop=-1;

int ch;
do
{
printf(“\n1:push”);
printf(“\n2:pop”);
printf(“\n3:display”);
printf(“\n enter the choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:push(&astack,&atop);
break;
case 2:pop(&astack,&atop);
break;
case 3:display(&astack,&atop);
break;
default:printf(“\nwrong choice”);
break;

}
}while(ch!=4);
return 0;
}
int push(void **astack,int *atop)
{
static char ch=1;
if(*atop>=MAX-1)
{
printf(“\n overflow”);
return-1;
}
if(*atop==-1)
{
*astack= (char*)malloc(1);
(*atop)++;
*(char*)(*astack+*atop)=ch;
ch++;
return -1;
}
*astack=realloc(*astack,(*atop+2));
(*atop)++;
*(char*)(*astack+*atop)=ch;
ch++;
}
int pop(void **astack,int *atop)
{
if(*atop==-1)
{
printf(“stack underflow\n”);
}
printf(“\n %d element %d delete”,*atop,*(char*)(*astack+*atop));
(*atop)–;

}
void display(void **astack,int *atop)
{
int i=0;
for(i=(*atop);i>=0;i–)
{
printf(“\nstack at %d is %d\n”,i,*(char*)(*astack+i));
}
}
@

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>