当前位置: 代码迷 >> C语言 >> [求助]栈的一个输出问题!
  详细解决方案

[求助]栈的一个输出问题!

热度:132   发布时间:2007-11-04 10:25:55.0
[求助]栈的一个输出问题!

我的代码:
主要错在输出上(红色部分)
3-2define.h
/***************************************************************/
typedef float elemtype ;
typedef struct node
{
elemtype data;
struct node *next;
}*linkstack;
/****************************************************************/
3-2lib.h

#include<stdio.h>

int initstack(linkstack s)
{
s=(linkstack)malloc(sizeof(linkstack));
if(s==NULL)
return 0;
s->next=NULL;
printf("OK!!!!!!!!!!created!\n");
return 1;
}
float gettop(linkstack s)
{
if(s->next==NULL)
return 0;
else
return s->next->data;
}
int getlen(linkstack s)
{
linkstack q;
int i;
q=s->next;
while(q!=0)
{
i++;
q=q->next;
}
return i;
}
int push(linkstack s,elemtype e)
{

linkstack p;
if(s->next=NULL)
return 0;
p=(linkstack)malloc(sizeof(linkstack));
if(!p)
return 0;
p->data=e;
p->next=s->next;
s->next=p;//前端插入法
return 1;
}
int pop(linkstack s)
{
linkstack p;
if(s->next=NULL)
return 0;
p=s->next;
s->next=p->next;
free(p);
return 1;
}
int emptystack(linkstack s)
{
if(s->next==NULL)
return 1;
else
return 0;
}
void list(linkstack s)
{
linkstack p;
p=s;
while(p->next!=NULL)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
}


/************************************************/
3-2main.c
#include<stdio.h>
#include<malloc.h>
#include"3-2define.h"
#include"3-2lib.h"

int
main()
{
linkstack s;
int i,j;
float e;
s=(linkstack)malloc(sizeof(linkstack));
initstack(s);
printf("Input how much linkstack do you want to creat:\n");
scanf("%d",&i);
for(j=0;j<i;j++)
{
printf("INput the data:\n");
scanf("%f",&e);
push(s,e);

}
printf("OK,the data have been pushed in!\n");
list(s);
getlen(s);
while(emptystack(s))
pop(s);
list(s);
getlen(s);
return 0;
}

搜索更多相关的解决方案: 输出  

----------------解决方案--------------------------------------------------------
自我觉得没错!
我还怀疑在压栈是可能有错误!

----------------解决方案--------------------------------------------------------
没搞明白你要说什么

你想用 linked list 做一个stack? 数组很好用呀

根据你的push来看 s是没存数据的 那就是第一个是空node
从第二个开始储存 s->next
int getlen(linkstack s)
这部分
你没初始化i
如果i是0
那么 i++; q=q->next; 这个while loop 就少算1个node 抛开第一个空node


typedef float elemtype ;

声明的数据类型是float

但是你printf("%d") 是int

----------------解决方案--------------------------------------------------------
有时真的是旁观者清啊!
----------------解决方案--------------------------------------------------------
也是自己的不扎实!
----------------解决方案--------------------------------------------------------
  相关解决方案