数据结构问题:在栈顶指针为top的链栈中,计算该链栈中结点的个数,用C语言实现。

来源:百度知道 编辑:UC知道 时间:2024/05/24 00:41:10
请各位高手指教怎么编啊?

可以这么设栈的结构体
struct stack
{
int data;
stack *down;
};
然后
stack *top = new stack;
top->down = NULL;
给出push的编法
void push( int n,stack *top )
{
stack *p = new stack;
p->data = n;
p->down = top;
top = p;
}
然后经过push,如果要计算结点个数,可以这样编。
int lenth( stack *top )
{
stacp *p = top;
int t = 0;
while ( p->down->down )
{
t++;
p = p->down;
}
return t;
}

.....不太看得动,应该节点就是top-1个啊