有关stack的问题 我写的为什么达不到要求?
使用如下的man() 确认push(int v) 和int pop()动作正常
#include <stdio.h>
main()
{
int d;
push(1);
push(2);
push(3);
d = pop();
printf("First data = %d\n", d);
d = pop();
printf("Second data = %d\n", d);
push(4);
d = pop();
printf("Third data = %d\n", d);
push(5);
d = pop();
printf("Fourth data = %d\n", d);
d = pop();
printf("Fifth data = %d\n", d);
}
----------------
要求得到如下显示
First data = 3
Second data = 2
Third data = 4
Fourth data = 5
Fifth data = 1
以下是我写的程序
得出来的结果却是
First data = 1
Second data = 2
Third data = 3
Fourth data = 4
Fifth data = 5
#include <stdio.h>
int buffer[1024];
int inp=0;
int outp=0;
push(int v)
{
if (inp<1024) {
buffer[inp]=v;
inp=inp+1;
}
}
int pop()
{
int v=0;
if (outp<inp) {
v=buffer[outp];
outp=outp+1;
}
return v;
}
main()
{
int d;
push(1);
push(2);
push(3);
d = pop();
printf("First data = %d\n", d);
d = pop();
printf("Second data = %d\n", d);
push(4);
d = pop();
printf("Third data = %d\n", d);
push(5);
d = pop();
printf("Fourth data = %d\n", d);
d = pop();
printf("Fifth data = %d\n", d);
}
到底错在哪里?
[ 本帖最后由 martyxu 于 2010-7-21 14:02 编辑 ]