我的代码:
主要错在输出上(红色部分)
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;
}