[问题] 练习stack结构 资料显示问题

楼主: gcmtw88 (前方黄昏)   2018-08-14 13:46:09
开发平台(Platform): (Ex: Win10, Linux, ...)
WIN XP
问题(Question):
分别
手动加入元素 item0~3
以及for循环
自动加入元素到stack
用for循环加入的元素
感觉怪异
不知道哪边想错了
预期的正确结果(Expected Output):
item=test0
item=test1
item=test2
item=test3
item=test4
item=test5
item=test6
item=test7
s1.length=7
test0
test1
test2
test3
test4
test5
test6
test7
错误结果(Wrong Output):
item=test0
item=test1
item=test2
item=test3
item=test4
item=test5
item=test6
item=test7
s1.length=7
test0
test1
test2
test3
test7
test7
test7
test7
程式码(Code):(请善用置底文网页, 记得排版,禁止使用图档)
完整程式
https://pastebin.com/sHABbnTi
部份程式
void Push(StackADT *S,element item)
{
if(isFull(S))
printf("stack is full \n");
else
{
printf("item=%s\n",item);
S->stack[++(S->length)]=item;
}
}
int main()
{
//初始化STACK
StackADT s1;
s1.length=-1;
//手动加入stack元素
element item0;
item0.data="test0";
Push(&s1,item0);
element item1;
item1.data="test1";
Push(&s1,item1);
element item2;
item2.data="test2";
Push(&s1,item2);
element item3;
item3.data="test3";
Push(&s1,item3);
//自动加入stack元素
int j;
for(j=4;j<8;j++)
{
element item;
char buf[10];
sprintf(buf,"test%d",j);
item.data=buf;
Push(&s1,item);
}
//显示stack目前长度
printf("s1.length=%d\n",s1.length);
//显示stack内容
int i;
for (i=s1.length;i>=0;i
作者: sarafciel (Cattuz)   2018-08-14 14:07:00
你的elem的data是指标 4~7都指到buff上buff内容有改当然4~7都会受到影响只会在stack上配一次而已 写在哪都一样 除非你用到heapchar *buf = (char *)malloc(10*sizeof(char));
作者: MOONRAKER (㊣牛鹤鳗毛人)   2018-08-14 14:35:00
就你记错这样
楼主: gcmtw88 (前方黄昏)   2018-08-14 14:52:00
把下面那篇的问题解决 就知道怎么传递字串了 谢谢各位

Links booklink

Contact Us: admin [ a t ] ucptt.com