这段代码不知道错在那里,帮忙找一下。谢谢了
# include <stdlib.h>
# include <stdio.h>
typedef struct list {
int data;
struct list *next;
} Llist, *NewList;
// 单链表的声明函数系列
void list(void); // 单链表的主函数;
void createlist(NewList); // 加长链表的长度;
void printlist(NewList); // 输出链表的数据;
// 源代码开始
main()
{
list();
}
// 单链表的主函数
void list (void)
{
NewList head;
head->data = 12;
head->next = NULL;
createlist(head);
printlist(head);
}
// 延长单链表的长度并存储数据
void createlist(NewList head)
{
int data;
NewList p;
NewList nextlist; // 作为交换的中间量
int n = 1;
while(1)
{
if(n == 2) // 作为链表长度的限定
break;
printf("Enter new data: \n");
scanf("%d", &data);
nextlist = malloc(sizeof(NewList)); // 分配资源
nextlist->data = data;
nextlist->next = NULL;
if(head->next == NULL)
{
head->next = nextlist;
p = nextlist;
}
else
{
p->next = nextlist;
p = nextlist;
}
n++;
// free(nextlist);
}
}
void printlist(NewList head)
{
printf("Enter the list");
while(head->next != NULL)
{
printf("%d",(head++)->data);
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Sund71/archive/2010/04/28/5538763.aspx
[解决办法]
void list (void)
{
NewList head;
head=(*NewList)malloc(sizeof(Llist));
head->data = 12;
head->next = NULL;
createlist(head);
printlist(head);
}
[解决办法]
NewList head;//这里只是声明了一个头指针,还没有分配空间啊
head->data = 12;
head->next = NULL
[解决办法]
顶楼上的
[解决办法]
编译通过了,但是功能没有测试
- C/C++ code
# include <stdlib.h># include <stdio.h>typedef struct list { int data; struct list *next;} Llist, *NewList;// 单链表的声明函数系列void list(void); // 单链表的主函数;void createlist(NewList); // 加长链表的长度;void printlist(NewList); // 输出链表的数据;// 源代码开始 int main(){ list(); return 0;}// 单链表的主函数void list (void){ NewList head; head->data = 12; head->next = NULL; createlist(head); printlist(head);}// 延长单链表的长度并存储数据void createlist(NewList head){ int data; NewList p; NewList nextlist; // 作为交换的中间量 int n = 1; while(1) { if(n == 2) // 作为链表长度的限定 break; printf("Enter new data: \n"); scanf("%d", &data); nextlist =(struct list*) malloc(sizeof(NewList)); // 分配资源 nextlist->data = data; nextlist->next = NULL; if(head->next == NULL) { head->next = nextlist; p = nextlist; } else { p->next = nextlist; p = nextlist; } n++;// free(nextlist); }}void printlist(NewList head){ printf("Enter the list"); while(head->next != NULL) { printf("%d",(head++)->data); }}