EmbLogic's Blog

stack push and pop program using functions .

//header.h//

#include
2 #include
3 #define MAX 5
4 int push(void **,int *);
5 int pop(void **,int *);
6 void display(void **,int *);
//fun.c//

#include”abc.h”
2 int push(void **astack,int *atop)
3 {
4 static char ch=1;
5 if(*atop >= MAX-1)
6 {
7 printf(“stack overflow”);
8 return -1;
9 }
10 if(*atop==-1)
11 {
12 *astack;
13 astack=(char*)malloc(1);
14 (*atop)++;
15 *(char*)(*astack+*atop)=ch;
16 ch++;
17 return 0;
18 }
19 (*atop)++;
20 *astack=(char*)realloc(*astack,(*atop) + 1);
21 *(char*)(*astack+*atop)=ch;
22 ch++;
23 return 0;
24
25 }
26 int pop(void **astack,int *atop)
27 {
28 if(*atop= 0; i–)
42 {
43 printf(“the data at %d pos is =%d\n”,i,*(char*)(*astack+i));
44 }
45 }
//main.c//
#include”abc.h”
2 int main()
3 {
4 int ch;
5 void *stack;
6 int top= -1;
7 do
8 {
9 printf(“1.push\n”);
10 printf(“2.pop\n”);
11 printf(“3.display\n”);
12 printf(“4.quit\n”);
13
14 scanf(“%d”,&ch);
15
16 switch(ch)
17 {
18 case 1: push(&stack,&top);
19 break;
20 case 2: pop(&stack,&top);
21 break;
22 case 3: display(&stack,&top);
23 break;
24 default : printf(“wrong choice\n”);
25 }
26 }
27 while(ch != 4);
28 return 0;
29 }
~

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>