求助C语言单链表问题,输出链表总是错误

来源:百度知道 编辑:UC知道 时间:2024/05/10 23:02:54
#include <string.h>
#include <stdio.h>
#include <alloc.h>
#define M 10

typedef char datatype;
typedef struct node
{
datatype data[M];
struct node *next;
} linklist;

int LENGTH(linklist *head)
{
int i=0;linklist *p;
p=head->next;
while(p)
{i++; p=p->next;}
return i;
}

void main()
{
linklist *head,*s;
int n;
datatype x[M];
clrscr();
head=malloc(sizeof(linklist));
head->next=NULL;
printf("please input the data[M]:\n");
gets(x);
while(strcmp(x,"")!=0)
{
s=malloc(sizeof(linklist));
s->data[M]=x[M];
s->next=head->next;
head->next=s;
gets(x);
}

n=LENGTH(head);
printf("the total number is %d\n",n);
sprintf(x,"%d",n);
strcpy(head->data,x);

#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
#define M 10

typedef char datatype;
typedef struct node
{
datatype data[M];
struct node *next;
} linklist;

int LENGTH(linklist *head)
{
int i=0;linklist *p;
p=head->next;
while(p)
{i++; p=p->next;}
return i;
}

void main()
{
linklist *head,*s;
int n;
datatype x[M];
clrscr();
head=(linklist*)malloc(sizeof(linklist));
head->next=NULL;
printf("please input the data[M]:\n");
gets(x);
while(strcmp(x,"")!=0)
{
s=(linklist*)malloc(sizeof(linklist));
s->data[M]=x[M];
s->next=head->next;
head->next=s;
gets(x);
}

n=LENGTH(head);
printf("the total number is %d\n&qu