读书人

单链表没法输出

发布时间: 2013-03-21 10:08:17 作者: rapoo

单链表无法输出


#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0

typedef struct Lnode{
int data;
struct Lnode *next;
int length;
}Lnode;

void list_create(Lnode *L);
void list_insert(Lnode *L,int i);
void list_delete(Lnode *L,int i);
void get_tail_data(Lnode L,int *e);
void print(Lnode L);
int main(void)
{
Lnode L;
list_create(&L);
print(L);
}
void list_create(Lnode *L)
{
int i,n;
Lnode *p;
L=(Lnode *)malloc(sizeof(Lnode));
if(L==NULL){
exit(-1);
}
L->next=NULL;
printf("Input the length of the list:\n");
scanf("%d",&n);//the next node is null
for(i=1;i<=n;i++){
printf("Input data%d:",i);
p=(Lnode *)malloc(sizeof(Lnode));
scanf("%d",&p->data);
++L->length;
L->next=p;//add the next node
p->next=L->next;
}
printf("The length of the list is %d\n",L->length);
}//CreateList_l

void print(Lnode L)
{
Lnode *p;
p=&L;
printf("Now the datas int the linked list is\n");
for(p=p->next;p!=NULL;p=p->next){
printf("%d ",*(p->data));//Linked_list.c:52: error: invalid type argument of ‘unary *’ (have ‘int’)
printf("\n");
}

[解决办法]
p->data本身就是一个int变量,前边加*成啥了
[解决办法]
随便看了下,问题好多啊。
1.你malloc的空间从不free
2.你在main里面定义了一个结构体L,这个L是有空间的,在调用list_create是你传入的是L的地址,而在list_create里面你又用L=(Lnode *)malloc(sizeof(Lnode));将L指向了malloc的空间,也就是说你后面的操作与你主函数的L完全没关系了
3.编译都通不过,*(p->data)有问题,而且后面还缺少括号
4.逻辑没仔细看,++L->length;
L->next=p;//add the next node
p->next=L->next;
这里的逻辑好像有问题
[解决办法]

#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0

typedef struct Lnode{
int data;
struct Lnode *next;
int length;
}Lnode;

void list_create(Lnode **L);
void list_insert(Lnode *L,int i);
void list_delete(Lnode *L,int i);
void get_tail_data(Lnode L,int *e);
void print(Lnode *L);
int main(void)
{
Lnode *L;
list_create(&L);
print(L);
}
void list_create(Lnode **L)
{
int i,n;
Lnode *p;
*L=(Lnode *)malloc(sizeof(Lnode));
if(L==NULL){
exit(-1);
}
(*L)->next=NULL;
(*L)->length = 0;
printf("Input the length of the list:\n");
scanf("%d",&n);//the next node is null
for(i=1;i<=n;i++){
printf("Input data%d:",i);
p=(Lnode *)malloc(sizeof(Lnode));
scanf("%d",&p->data);
++((*L)->length);
p->next=(*L)->next;
(*L)->next=p;//add the next node


}
printf("The length of the list is %d\n",(*L)->length);
}//CreateList_l

void print(Lnode *L)
{
Lnode *p;
p=L;
printf("Now the datas int the linked list is\n");
for(p=p->next;p!=NULL;p=p->next){
printf("%d ",p->data);//Linked_list.c:52: error: invalid type argument of ‘unary *’ (have ‘int’)
printf("\n");
}
}


[解决办法]



引用:
引用:随便看了下,问题好多啊。
1.你malloc的空间从不free
2.你在main里面定义了一个结构体L,这个L是有空间的,在调用list_create是你传入的是L的地址,而在list_create里面你又用L=(Lnode *)malloc(sizeof(Lnode));将L指向了malloc的空间,也就是说你后面……


我帮你写了就不是自学了。。你尝试着写,有问题问,这样才能提高。
[解决办法]

#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0

typedef struct Lnode{
int data;
struct Lnode *next;
int length;
}Lnode;

void list_create(Lnode *L);
void list_insert(Lnode *L,int i);
void list_delete(Lnode *L,int i);
void get_tail_data(Lnode L,int *e);
void print(Lnode *L);
int main(void)
{
Lnode *L;
L=(Lnode *)malloc(sizeof(Lnode));
if(L==NULL){
exit(-1);
}
L->next=NULL;
L->length = 0;
list_create(L);
print(L);
}
void list_create(Lnode *L)
{
int i,n;
Lnode *p;

printf("Input the length of the list:\n");
scanf("%d",&n);//the next node is null
for(i=1;i<=n;i++){
printf("Input data%d:",i);
p=(Lnode *)malloc(sizeof(Lnode));
scanf("%d",&p->data);
++(L->length);
p->next=L->next;
L->next=p;//add the next node
}
printf("The length of the list is %d\n",L->length);
}//CreateList_l

void print(Lnode *L)
{
Lnode *p;
p=L;
printf("Now the datas int the linked list is\n");
for(p=p->next;p!=NULL;p=p->next){
printf("%d ",p->data);//Linked_list.c:52: error: invalid type argument of ‘unary *’ (have ‘int’)
printf("\n");
}
}

[解决办法]
引用:
...

LZ最好自己画一下这个步骤。
看一下不调换的话,你的指针指向。

PS:最简单的是L->next为NULL的时候,链表只增加一个节点
 L->next=p;//add the next node
p->next=L->next;

原来的写法,很容易发现,L的下一个指向P,p的下一个也指向P
[解决办法]
引用:
引用:
C/C++ code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657#include<malloc.h>#include<stdio.h>#include<stdlib.h>#define OK ……
画个图就知道这两句的意思了。如果换过来就成了L的下一个是P,而P的下一个是L的下一个(也就是P)这样就有问题了

读书人网 >C语言

热点推荐