user-0 提问: 求大神指点单链表

回复:
user: user-1
content: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node {     int data;     struct node *link; } head; typedef struct node NODK; void createList(NODK *head, int n) {     NODK *p, *q;     int i;     p = head; //    p = (NODK *)malloc(sizeof(NODK)); //    head->link = p;     for (i = 1; i <= n; i++)     {         q = (NODK *)malloc(sizeof(NODK));         scanf("%d", &q->data);         q->link = NULL;         p->link = q;         p = q;     } } void outputList(NODK *head) {     NODK *p;     p = head->link;     while (p)     {         printf("%6d", p->data);         p = p->link;     } } int main() {     printf("input 5 number: ");     createList(&head, 5);     outputList(&head);     return 0; }

user: user-0
content: 引用 7 楼 5250的回复:或者你可以这样改，有点儿啰嗦  #include <stdio.h> #include <stdlib.h> #include <malloc.h> struct node {     int data;     struct node *link; } head; typedef struct node NODK; void createList(NODK *head, int n) {     NODK *p, *q;     int i;     p = (NODK *)malloc(sizeof(NODK));     head->link = p;     for (i = 1; i <= n; i++)     {         q = (NODK *)malloc(sizeof(NODK));         scanf("%d", &q->data);         q->link = NULL;         p->link = q;         p = q;     } } void outputList(NODK *head) {     NODK *p;     p = head->link;     while (p)     {         printf("%6d", p->link->data);         p = p->link;     } } int main() {     printf("input 5 number: ");     createList(&head, 5);     outputList(&head);     return 0; } 好的，万分感谢，，，

user: user-0
content: 引用 1 楼 自信男孩的回复:create函数里把head = p;改成head->next = p; 建议直接贴代码，还可以帮你调试一下～ 改了之后有输出，但是好像是错误的

user: user-0
content: 引用 1 楼 自信男孩的回复:create函数里把head = p;改成head->next = p; 建议直接贴代码，还可以帮你调试一下～ #include<stdio.h> #include<stdlib.h> #include<malloc.h> struct node{ 	 int data; 	 struct node *link; }head; typedef struct node NODK; void createList(NODK *head,int n){ 	 NODK *p,*q; 	 int i; 	 p=(NODK *)malloc(sizeof(NODK)); 	 head=p; 	 for(i=1;i<=n;i++){ 		 q=(NODK *)malloc(sizeof(NODK)); 		 scanf("%d",&q->data); 		 q->link=NULL; 		 p->link=q; 		 p=q; 	 } } void outputList(NODK *head){ 	 NODK *p; 	 p=head->link; 	 while(p->link!=NULL){ 		printf("%6d",p->data); 		p=p->link; 	} } int main(){ 	 printf("请输入5个结点的值："); 	 createList(&head,5); 	 outputList(&head); 	 return 0; }

user: user-2
content: create函数里把head = p;改成head->next = p; 建议直接贴代码，还可以帮你调试一下～

user: user-3
content: 或者你可以这样改，有点儿啰嗦  #include <stdio.h> #include <stdlib.h> #include <malloc.h> struct node {     int data;     struct node *link; } head; typedef struct node NODK; void createList(NODK *head, int n) {     NODK *p, *q;     int i;     p = (NODK *)malloc(sizeof(NODK));     head->link = p;     for (i = 1; i <= n; i++)     {         q = (NODK *)malloc(sizeof(NODK));         scanf("%d", &q->data);         q->link = NULL;         p->link = q;         p = q;     } } void outputList(NODK *head) {     NODK *p;     p = head->link;     while (p)     {         printf("%6d", p->link->data);         p = p->link;     } } int main() {     printf("input 5 number: ");     createList(&head, 5);     outputList(&head);     return 0; }

user: user-3
content: 引用 2 楼 鲁宇科 的回复:[quote=引用 1 楼 自信男孩的回复:]create函数里把head = p;改成head->next = p; 建议直接贴代码，还可以帮你调试一下～ #include<stdio.h> #include<stdlib.h> #include<malloc.h> struct node{ 	 int data; 	 struct node *link; }head; typedef struct node NODK;  void createList(NODK *head,int n){ 	 NODK *p,*q; 	 int i; 	 p=(NODK *)malloc(sizeof(NODK)); 	 head=p; 	 for(i=1;i<=n;i++){ 		 q=(NODK *)malloc(sizeof(NODK)); 		 scanf("%d",&q->data); 		 q->link=NULL; 		 p->link=q; 		 p=q; 	 } } void outputList(NODK *head){ 	 NODK *p; 	 p=head->link; 	 while(p->link!=NULL){ 		printf("%6d",p->data); 		p=p->link; 	} } int main(){ 	 printf("请输入5个结点的值："); 	 createList(&head,5); 	 outputList(&head); 	 return 0; }[/quote]  你的head指向了p(head=p) p又指向了q(p = q)； 链表结尾处q-link = null，也就等同于head= null 你的head一直都在改变，所以就没有输出了。

user: user-4
content: 数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633

user: user-2
content: #include<stdio.h> #include<stdlib.h> #include<malloc.h>  struct node{     int data;     struct node *link; }head;  typedef struct node NODK;  void createList(NODK *head,int n) {     NODK *p,*q;     int i;     //p=(NODK *)malloc(sizeof(NODK));     //head->link = p;     p = head;     for(i=1;i<=n;i++){         q=(NODK *)malloc(sizeof(NODK));         scanf("%d", &q->data);         q->link=NULL;         p->link=q;         p=q;     } } void outputList(NODK *head) {     NODK *p;     p = head->link;     //while(p->link!=NULL){     while(p){         printf("%6d",p->data);         p=p->link;     } } int main(){     printf("请输入5个结点的值：");     createList(&head,5);     outputList(&head);     return 0; } 供参考～