求助C语言单链表问题,输出链表总是错误
#include<string.h>#include<stdio.h>#include<alloc.h>#defineM10typedefchardatatype;typ...
#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);
s=head;
while(s)
{
puts(s->data);
s=s->next;
}
getch();
}
展开
#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);
s=head;
while(s)
{
puts(s->data);
s=s->next;
}
getch();
}
展开
展开全部
#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",n);
sprintf(x,"%d",n);
strcpy(head->data,x);
s=head;
while(s)
{
puts(s->data);
s=s->next;
}
getch();
}
#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",n);
sprintf(x,"%d",n);
strcpy(head->data,x);
s=head;
while(s)
{
puts(s->data);
s=s->next;
}
getch();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询