user-0 提问: 哪位大佬帮忙按照我的思路改一下，是主函数的错误，不要大幅度修改，刚接触链表，求大佬指点

回复:
user: user-0
content: 引用 6 楼 ?Travelers的回复:[quote=引用 4 楼 自信男孩的回复:]#include <stdio.h> #include <stdlib.h> struct node {     int data;     struct node *next; };// *node[5]={{1},{3},{5},{7},{9},{11}}; int main() {     struct node *insertnode(struct node *head,int key,int vaule);     struct node *head = NULL, *p;     //printf("insertnode(head,3,5)");     head = insertnode(head,3,5);     head = insertnode(head,4,7);      p = head;      while(p) {          printf("%d\t", p->data);          p = p->next;      }      putchar(10);      return 0;  } struct node *insertnode(struct node *head,int key,int vaule)     /*head是链表头指针，在值为key的结点前插入值为vaule的结点*/ {     struct node *p,*r;     r=(struct node*)malloc(sizeof(struct node));/*为待插入的结点分配空间*/     r->data=vaule;/*赋值数据域*/     //if(head=NULL||head->next=key)/*如果颠倒顺序可能错误*/     if(head==NULL||head->data==key)/*如果颠倒顺序可能错误*/     {         r->next=head;         head=r;         return(r);     }/*链表为空或者 结点为key的情况*/     p=head;     //while(p->next!=NULL&&p->data=key)     while(p->next!=NULL&&p->data !=key)     {         p=p->next;         //return(r);     } /*寻找key的结点*/     r->next=p->next;     p->next=r;/*插入新结点*/      return head; } 供参考～  不是很理解你的需求，上面的代码执行没问题～ 就是用struct node *(struct node *head,int key,int value )这个函数编写一个程序[/quote] 是不是还需要增加一个输出函数？

user: user-0
content: 引用 4 楼 自信男孩的回复:#include <stdio.h> #include <stdlib.h> struct node {     int data;     struct node *next; };// *node[5]={{1},{3},{5},{7},{9},{11}}; int main() {     struct node *insertnode(struct node *head,int key,int vaule);     struct node *head = NULL, *p;     //printf("insertnode(head,3,5)");     head = insertnode(head,3,5);     head = insertnode(head,4,7);      p = head;      while(p) {          printf("%d\t", p->data);          p = p->next;      }      putchar(10);      return 0;  } struct node *insertnode(struct node *head,int key,int vaule)     /*head是链表头指针，在值为key的结点前插入值为vaule的结点*/ {     struct node *p,*r;     r=(struct node*)malloc(sizeof(struct node));/*为待插入的结点分配空间*/     r->data=vaule;/*赋值数据域*/     //if(head=NULL||head->next=key)/*如果颠倒顺序可能错误*/     if(head==NULL||head->data==key)/*如果颠倒顺序可能错误*/     {         r->next=head;         head=r;         return(r);     }/*链表为空或者 结点为key的情况*/     p=head;     //while(p->next!=NULL&&p->data=key)     while(p->next!=NULL&&p->data !=key)     {         p=p->next;         //return(r);     } /*寻找key的结点*/     r->next=p->next;     p->next=r;/*插入新结点*/      return head; } 供参考～  不是很理解你的需求，上面的代码执行没问题～ 就是用struct node *(struct node *head,int key,int value )这个函数编写一个程序

user: user-1
content: #include <stdio.h> #include <stdlib.h> struct node {     int data;     struct node *next; };  struct node *insertnode(struct node *head,int key,int vaule); void printLink(struct node * pHead);  int main() {     struct node * head = NULL, *t=NULL;     int i=0;     for ( ; i<5; i++) {         if ( NULL == head ) {             t = head = (struct node *) malloc(sizeof(struct node));             head->data = 1;             continue;         }         t->next = (struct node *) malloc( sizeof(struct node));         t->next->data = 2*i+1;         t = t->next;     }     t->next = NULL;     printf("InitLink:");     printLink(head);     printf("Insert 3,4:");     insertnode(head, 3, 4);     printLink(head);     printf("Insert 6, 6:");     insertnode(head, 6, 6);     printLink(head);     printf("Insert 6,12:");     insertnode(head, 6,12);     printLink(head);     return 0; } struct node *insertnode(struct node *head,int key,int vaule) /*head是链表头指针，在值为key的结点前插入值为vaule的结点*/ {     struct node *p, *r;     r=(struct node*)malloc(sizeof(struct node));/*为待插入的结点分配空间*/     r->data=vaule; /*赋值数据域*/     p = head;     while ( p->next !=NULL && p->data !=key ) p=p->next;     if ( p->data==key) {         r->next = p->next;         p->next = r;     } else if ( p->next == NULL ) {         p->next = r;         r->next = NULL;     }     return r; }  void printLink(struct node * pHead) {     struct node * p = pHead;     while(p != NULL ) {         printf("%d -> ", p->data);         p = p->next;     }     printf("NULL\n"); }

user: user-2
content: #include <stdio.h> #include <stdlib.h> struct node {     int data;     struct node *next; };// *node[5]={{1},{3},{5},{7},{9},{11}}; int main() {     struct node *insertnode(struct node *head,int key,int vaule);     struct node *head = NULL, *p;     //printf("insertnode(head,3,5)");     head = insertnode(head,3,5);     head = insertnode(head,4,7);      p = head;      while(p) {          printf("%d\t", p->data);          p = p->next;      }      putchar(10);      return 0;  } struct node *insertnode(struct node *head,int key,int vaule)     /*head是链表头指针，在值为key的结点前插入值为vaule的结点*/ {     struct node *p,*r;     r=(struct node*)malloc(sizeof(struct node));/*为待插入的结点分配空间*/     r->data=vaule;/*赋值数据域*/     //if(head=NULL||head->next=key)/*如果颠倒顺序可能错误*/     if(head==NULL||head->data==key)/*如果颠倒顺序可能错误*/     {         r->next=head;         head=r;         return(r);     }/*链表为空或者 结点为key的情况*/     p=head;     //while(p->next!=NULL&&p->data=key)     while(p->next!=NULL&&p->data !=key)     {         p=p->next;         //return(r);     } /*寻找key的结点*/     r->next=p->next;     p->next=r;/*插入新结点*/      return head; } 供参考～  不是很理解你的需求，上面的代码执行没问题～

user: user-3
content: #include <stdio.h> #include <malloc.h>  struct node {     int data;     struct node *next; };   int append(struct node *head, int data) {     if (head == NULL) {         return 0;     }     struct node *temp = (struct node *) malloc(sizeof(struct node));     struct node *tmp = head;     while (tmp->next != NULL) {         tmp = tmp->next;     }     temp->data = data;     temp->next = NULL;     tmp->next = temp;     return 1; }  int showChain(struct node *head) {     if (head == NULL || head->next == NULL) {         return 0;     }     struct node *temp = head->next;     int count = 1;     while (temp->next != NULL) {         printf("第%d个结点的值为%d\n", count, temp->data);         temp = temp->next;         count++;     }     return 1; }  int insert(struct node *head, int key, int data) {     if (head == NULL) {         return 0;     }     int loop = 0;     struct node *temp = head;     while (temp->next != NULL) {         if (temp->data != key) {             temp = temp->next;             continue;         }         loop = 1;         struct node *tmp = (struct node *) malloc(sizeof(struct node));         tmp->data = data;         tmp->next = temp->next;         temp->next = tmp;         break;     }     if (loop == 0) {         printf("未找到指定结点");         return 0;     }     return 1; }  int main(void) {     struct node *head;     for (int i = 0; i < 7; i++) {         append(head, 2 * i + 1);     }     insert(head, 7, 13);     showChain(head);     return 0; }

user: user-3
content: head->next->data打错了~~

user: user-3
content: head=NULL||head->next=key？？？？？== printf("insertnode(head,3,5)");？？？？？输出字符串呢这是 if(head=NULL||head->next=key)？？？？head->next.data==key ~~~~无语了，这程序可能跑起来