Data Structure in C

I

# include

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;
}
 
R

Richard Heathfield

# 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.
 
J

Jaspreet

# 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()
{
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.
 
K

Keith Thompson

# include said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top