user-0 提问: C语言链表问题

回复:
user: user-1
content: #include<stdio.h> #include<stdlib.h>  typedef struct Node* PtrToNode; typedef int ElementType; //typedef struct Node{  //后面没有别名所以去掉typedef struct Node{     ElementType Data;     PtrToNode Next; };  typedef PtrToNode List;  List createList() {     List head = (PtrToNode)malloc(sizeof(struct Node));     if (!head)         exit(0);     head->Data = 0;     head->Next = NULL;     printf("头节点信息：\n");     //printf("list=%3d\n",head);     printf("list=%p\n",head);     printf("list->Data=%3d\n",head->Data);     //printf("list->Next=%3d\n",head->Next);     printf("list->Next=%p\n",head->Next);      return head; }  PtrToNode createNode(int Data) {     PtrToNode Node = (PtrToNode)malloc(sizeof(struct Node));     if (!Node)         exit(0);     Node->Data = Data;     Node->Next = NULL;      return Node; }  void printList(List head) {     PtrToNode cursor = head->Next;     while(cursor) {         printf("结点信息：\n");         //printf("cursor=%3d\n",cursor);         printf("cursor=%p\n",cursor);         printf("cursor->Data=%3d\n",cursor->Data);         //printf("cursor->Next=%3d\n",cursor->Next);         printf("cursor->Next=%p\n",cursor->Next);         cursor = cursor->Next;         printf("\n");     } }  void insertNode(List head,int data) {     PtrToNode newNode = createNode(data);     newNode->Next = head->Next;     head->Next = newNode;      printf("insert ok!\n"); }  PtrToNode findNode(List head, int x) {     PtrToNode cursor = head->Next;      //while(cursor->Next!=NULL) {    //如果cursor为NULL，那么cursor->Next操作就会出现段错误     while(cursor){         if(x==cursor->Data) {             return cursor;         }else{             cursor = cursor->Next;         }     }      return NULL;    //注意返回NULL }  int delNode(List head,int x) {     PtrToNode cursor = head->Next;     PtrToNode cursorFront = head;       if (!cursor) {         printf("list is empty!\n");         return -1;     }      while (cursor) {         if (cursor->Data == x) {    //删除所有的等于x的节点             cursorFront->Next = cursor->Next;             free(cursor);             cursor = cursorFront->Next;             continue;         }          cursor = cursor->Next;         cursorFront = cursorFront->Next;     }     /*     if(cursor==NULL){         printf("list is empty!\n");     }else{         while(cursor->Data != x){             cursorFront = cursor;             cursor = cursorFront->Next;             if(cursor==NULL){                 printf("not found element,delete failed!\n");                 return 0;             }         }         cursorFront->Next = cursor->Next;         free(cursor);     }     */      return 0; }  int main() {     List list = createList();     PtrToNode node;      if (!list) {         printf("create failed!\n");         return -1;     }      //if(list){         printf("create ok!\n\n");         for(int i=1;i<=5;i++){             insertNode(list, i*10);         }         //int x = (int)findNode(list, 40);         node = findNode(list, 40);         //printf("\nquery ok！\nlocation is:%3d\n\n",x);         printf("\nquery ok！\nlocation is:%p\n\n", node);         delNode(list, 20);         printList(list);     //}else{      //   printf("create failed!\n");     //}      system("pause");     return 0; } 供参考～

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

user: user-3
content: newNode->Next = head->Next; 一般要先判断一下head是否为NULL，然后 head->Next=newNode; newNode->Next=NULL; head=newNode;

user: user-4
content: 分析代码的基操，要用动态的发展的眼光看问题。 那个NULL是结束标记，后来insert的每个结点都是从那里复制来的结束标记，没有它就没有结束标记，当然不结束了。 至于你看见的输出信息，那是没有后续结点的那个时刻输出的。  很多人大学毕业了回头一看户口本上写着自己的文化程度小学，那就是因为户口本是小学时候办的，很正常。