最佳答案:堆栈就是先入后出的数据结构。如果用c语言来实现的话用个struct先定义一个栈的节点structnode;typedefstrcutnode*position;typedefpositionstack
堆栈就是先入后出的数据结构。
如果用c语言来实现的话用个struct
先定义一个栈的节点
struct
node;
typedef
strcut
node
*
position;
typedef
position
stack;
stack
creat();
void
push(int,stack);
int
pop(stack);
int
top();
struct
node
{
int
element;
position
next;
}
stack
creat()
{
stack
s;
s=malloc(sizeof(struct
node));
s->next==NULL;
}
void
push(int
a,stack
s)
{
position
temp;
temp=malloc(sizeof(struct
node));
temp->element=a;
temp->next=s->next;:
s->next=temp;
}
int
pop(stack
s)
{
int
res=s->next->element;
position
temp=s->next;
s->next=temp->next;
free
temp;
}
int
top(stack
s)
{
return
s->next->element;
}
好啦,先creat()一个栈,再进行push
pop等。程序中忽略了麻烦的错误检测给出了重点,当然还可以添加其他操作。。对了,头文件也要加上。本人还是习惯用c++弄成一个类^_^