470,849 Members | 1,141 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Data Structure in C

you can construct stack in easy way in C as following
#include<stdio.h>
# define MaxStack 10
int top=0;
void clearstack(int stack[MaxStack]);
int emptystack(int stack[MaxStack]);
int fullstack(int stack[MaxStack]);
void pushstack(int stack[MaxStack],int newelements);
void popstack(int stack[MaxStack],int *element);

void main()
{
int x,i,stack[MaxStack];
clearstack(stack);
for(i=0;i<MaxStack;i++)
{scanf("%d",&x);
pushstack(stack,x);
}
for(i=0;i<MaxStack;i++)
{
apopstack(stack,&x);
printf("%d\n",x);
}

}
void clearstack(int stack[MaxStack])
{
stack[top]=0;
return;
}
int emptystack(int stack[MaxStack])
{return (stack[top]==0);
}
int fullstack(int stack[MaxStack])
{
return (stack[top]==MaxStack)
;
}

void pushstack(int stack[MaxStack],int newelement)
{
stack[top]=stack[top]+1;
stack[stack[top]]=newelement;
}
popstack(int stack[MaxStack],int *element)
{
*element =stack[stack[top]];
stack [top]=stack[top]-1;
}

Mar 1 '06 #1
3 1662
# include said:
you can construct stack in easy way in C as following
#include<stdio.h>
# define MaxStack 10
int top=0;
void clearstack(int stack[MaxStack]);
int emptystack(int stack[MaxStack]);
int fullstack(int stack[MaxStack]);
void pushstack(int stack[MaxStack],int newelements);
void popstack(int stack[MaxStack],int *element);

void main()


At this point, you reveal your ignorance of C. I see little point in looking
through the rest of your code. You need a better C book.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 1 '06 #2
# include wrote:
you can construct stack in easy way in C as following
#include<stdio.h>
# define MaxStack 10
int top=0;
void clearstack(int stack[MaxStack]);
int emptystack(int stack[MaxStack]);
int fullstack(int stack[MaxStack]);
void pushstack(int stack[MaxStack],int newelements);
void popstack(int stack[MaxStack],int *element);

void main()
{
int x,i,stack[MaxStack];
clearstack(stack);
for(i=0;i<MaxStack;i++)
{scanf("%d",&x);
pushstack(stack,x);
}


<snip code snippet>

Why have you pasted this program ? Is there something wrong in it that
you want someone to help you or you just want a confirmation from us ?

BTW, main() **should** only return int. Your program may compile but
it is bound to give undefined behavior since your main is returning
void.

Thats the reason for the post by Richard.

Mar 2 '06 #3
"# include" <wa*********@yahoo.com> writes:
you can construct stack in easy way in C as following
#include<stdio.h>
# define MaxStack 10
It's conventional (but not required) to use all-caps for macro names:

#define MAXSTACK 10
int top=0;
void clearstack(int stack[MaxStack]);
int emptystack(int stack[MaxStack]);
int fullstack(int stack[MaxStack]);
void pushstack(int stack[MaxStack],int newelements);
void popstack(int stack[MaxStack],int *element);
MaxStack in each of these function declarations is silently ignored.
The first argument of each of these functions is really an int*.

void main()
No, main() returns int.
{
int x,i,stack[MaxStack];
clearstack(stack);
for(i=0;i<MaxStack;i++)
{scanf("%d",&x);
pushstack(stack,x);
}
for(i=0;i<MaxStack;i++)
{
apopstack(stack,&x);
printf("%d\n",x);
}

[snip]

Your code is very difficult to read. Proper use of indentation and
whitespace would help tremendously. There may be problems beyond what
I've pointed out, but it's not worth the pain of reading your code to
find them.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by dmb000006 | last post: by
3 posts views Thread by Mike Jones | last post: by
11 posts views Thread by theshowmecanuck | last post: by
3 posts views Thread by Kiran B. | last post: by
3 posts views Thread by aurora | last post: by
29 posts views Thread by zoltan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.