user-0 提问: 求链表的中间节点有问题，大神看看

回复:
user: user-1
content: 仅供参考：//不带表头结点的单向链表 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <time.h> #include <locale.h> struct NODE {     int          data;     struct NODE *next; } *head,*p,*q,*s,*p1,*p2,*q1,**ta; int i,k,n,t,m,v,N=10; int main() {     setlocale(LC_ALL,"chs");     srand(time(NULL));      head=NULL;      printf("创建%d个节点的单链表:",N);//创建N个节点的单链表     p=head;     for (i=0;i<N;i++) {         q=(struct NODE *)malloc(sizeof(struct NODE));         if (NULL==q) exit(1);         q->data=rand()%100;//填写0..99的随机值         q->next=NULL;         if (NULL==p) {             head=q;             p=head;         } else {             p->next=q;             p=q;         }     }      //输出整个单链表     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      k=3;     v=5;     printf("将值为%d的结点插入到单链表的第%d个结点前:",v,k);//将值为v的结点插入到单链表的第k个结点前     n=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         n++;         if (k==1) {             q=(struct NODE *)malloc(sizeof(struct NODE));             if (NULL==q) exit(1);             q->data=v;             q->next=head;             head=q;             break;         } else {             if (k-1==n) {                 q=(struct NODE *)malloc(sizeof(struct NODE));                 if (NULL==q) exit(1);                 q->data=v;                 q->next=p->next;                 p->next=q;                 break;             }         }         p=p->next;     }      //输出整个单链表     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      k=5;     printf("删除第%d个节点:",k);//删除第k个节点     n=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         n++;         if (k==1) {             q=head;             head=head->next;             free(q);             break;         } else {             if (k-1==n) {                 q=p->next;                 if (q) {                     p->next=q->next;                     free(q);                 }                 break;             }         }         p=p->next;     }      //输出整个单链表     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      printf("从小到大排序:");//从小到大排序     for (p=head,p1=NULL;p!=NULL;p1=p,p=p->next) {         for (q=p->next,q1=p;q!=NULL;q1=q,q=q->next) {             if (p->data > q->data) {                  //交换data //              printf("swap %02d %02d\n",p->data,q->data); //              t=p->data;p->data=q->data;q->data=t;                  //或者                  //交换next //              printf("swap %02d %02d\n",p->data,q->data);                 if (p==head) {//p是头                     if (p->next==q) {//pq挨着                         head=q;                         p->next=q->next;                         q->next=p;                         q=p;                         p=head;                     } else {//pq不挨着                         head=q;                         p2=p->next;                         p->next=q->next;                         q->next=p2;                         q1->next=p;                         q=p;                         p=head;                     }                 } else {//p不是头                     if (p->next==q) {//pq挨着                         p1->next=q;                         p->next=q->next;                         q->next=p;                         q=p;                         p=p1->next;                     } else {//pq不挨着                         p1->next=q;                         p2=p->next;                         p->next=q->next;                         q->next=p2;                         q1->next=p;                         q=p;                         p=p1->next;                     }                 }                  //输出整个单链表 //              s=head; //              while (1) { //                  if (NULL==s) { //                      printf("\n"); //                      break; //                  } //                  printf("%02d->",s->data); //                  s=s->next; //              } //              getchar();             }         }     }      //输出整个单链表并计算链表长度n     n=0;     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         n++;         s=s->next;     }      printf("将整个链表逆序:");//将整个链表逆序     if (n>=2) {         p=head;         q=p->next;         p->next=NULL;         while (1) {             q1=q->next;             q->next=p;             p=q;             q=q1;             if (NULL==q) break;         }         head=p;     }      //输出整个单链表     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      m=4;     n=6;     printf("将单链表中前%d个结点和后%d个结点进行互换:",m,n);//将单链表中前m个结点和后n个结点进行互换,m+n为链表总长     k=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         k++;         if (m==k) {             q=p;         }         s=p;         p=p->next;     }     q1=head;     head=q->next;     s->next=q1;     q->next=NULL;      //输出整个单链表     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //释放所有节点     p=head;     while (1) {         if (NULL==p) {             break;         }         q=p->next;         free(p);         p=q;     }      return 0; } //创建10个节点的单链表:08->74->07->23->03->99->31->56->88->16-> //将值为5的结点插入到单链表的第3个结点前:08->74->05->07->23->03->99->31->56->88->16-> //删除第5个节点:08->74->05->07->03->99->31->56->88->16-> //从小到大排序:03->05->07->08->16->31->56->74->88->99-> //将整个链表逆序:99->88->74->56->31->16->08->07->05->03-> //将单链表中前4个结点和后6个结点进行互换:31->16->08->07->05->03->99->88->74->56-> //

user: user-0
content: 你们的代码是怎么弄上去的，为什么我的就整个对齐了

user: user-2
content: 我刚看完链表

user: user-3
content: 对你的代码做了修改，对比一下吧； 一些不合理的地方我做了修改，一些代码风格是按照我平时写的风格改的，不一定适合你； 一些逻辑我也做了更改，让逻辑更简单一些； 希望对你有用； 最后运行基本上没问题。

user: user-3
content: 以下是main.c文件： #include <stdio.h> #include "list.h"  int main (void) {     int num;      printf ("input the numbers: ");      scanf ("%d", &num);      List *pList = NULL;      pList = CreateList (num);      if (!pList)         printf ("create failed!!!\r\n");     printf ("create ok\r\n");      ShowList (pList);      int iRet = 0;      iRet = ListLength (pList);      printf ("Listlength = %d\r\n", iRet);      int addnode;      printf ("input add element numbers: ");      scanf ("%d", &addnode);      pList = AddNode (pList, addnode);      ShowList (pList);      int pos;      printf ("input insert position: ");      scanf ("%d", &pos);      pList = InsertListItem (pList, pos);      if (!pList)         return -1;     printf ("insert ok\r\n");      ShowList (pList);      pList = ReverseList1 (pList);      if (!pList)         return -1;     printf ("reverse ok\r\n");      ShowList (pList);      int var = is_LoopList (pList);      printf ("var = %d\r\n", var);      if (var == 1)         printf ("The list is loop");     printf ("no loop\r\n");      List *point = ListMiddle (pList);     if (!point)         return -1;     printf ("find middlepoint\r\n");      ShowList (point);     ListSort (pList);      printf ("sort after");     ShowList (pList);      //delete node     int offset = 0;     data_t data;      printf ("input you want delete position: ");     scanf ("%d", &offset);      pList = DeleteItem (pList, &data, offset);      printf ("delete ok\r\n");      printf ("data = %d\r\n", data);      ShowList (pList);      int modnum;      printf ("input want modify data: ");      scanf ("%d", &modnum);      pList = UpdateList (pList, modnum);      if (NULL == pList)     {         printf ("update error\r\n");     }      ShowList (pList);      int searchnum;      printf ("input want search data: ");      scanf ("%d", &searchnum);      pList = SearchList (pList, searchnum);      DestoryList (pList);     pList = NULL;     return 0; }  以下是Makefile文件： SRC = $(wildcard *.c) OBJ = $(patsubst %.c, %.o, $(SRC))  CC = gcc  TARGET = app  CFLAGS := -Wall -g LDFLAGS := -pthread  all: $(TARGET)  %.o:%.c 	$(CC) -o $@ -c $< $(CFLAGS)  $(TARGET):$(OBJ) 	$(CC) -o $@ $^ $(LDFLAGS)  .PHONY: clean all  clean: 	$(RM) $(OBJ) 	$(RM) $(TARGET) 	$(RM) *~

user: user-3
content: 以下是list.c文件： #include <stdio.h> #include <stdlib.h> #include <string.h> #include "list.h"  struct List {     data_t data;      struct List *pNext; };      List * CreateList (int n) {     int iCount = 0;      List *pNew, *pCur;     List *pList = NULL;     pCur = pList;      if (n < 1)         return pList;      pNew = (List *) malloc (sizeof (List));     if (!pNew) { /* malloc error then return */         printf ("new create failed!\r\n");         return NULL;     }     memset (pNew, 0, sizeof (List));      data_t data;      printf ("please input data: \r\n");      scanf ("%d", &data);      pNew->data = data;      pNew->pNext = NULL;      while (1)     {         if (!pList)             pList = pNew;         else             pCur->pNext = pNew;         pCur = pNew;         iCount++;          if (iCount >= n)             break;         pNew = (List *) malloc (sizeof (List));         scanf ("%d", &(pNew->data));     }     pCur->pNext = NULL;      return pList; }  int ListLength (List * pList) {     int length = 0;      if (!pList)         return 0;      List *pTmp = pList;      while (pTmp) {         length++;         pTmp = pTmp->pNext;     }      return length; }  List *AddNode (List * pList, int numbers) {     List *pNew, *pCur;     data_t data;      pCur = pList;      if (!pList || numbers <= 0) {         printf ("current list is empty,add fail!\r\n");         return pList;     } else {         while (pCur->pNext)             pCur = pCur->pNext;          pNew = (List *) malloc (sizeof (List));          if (!pNew) {             printf ("new create node failed!\r\n");             return pList;         }         memset (pNew, 0, sizeof (List));         printf ("please input data: ");         scanf ("%d", &data);          pNew->data = data;         pNew->pNext = NULL;          int ncount = 0;          while (1) {             pCur->pNext = pNew;             pCur = pNew;             ncount++;              if (ncount >= numbers) {                 break;             }             //再次创建节点，完成number个节点             pNew = (List *) malloc (sizeof (List));              printf ("again input data: ");             scanf ("%d", &data);             pNew->data = data;         }         pCur->pNext = NULL;     }     return pList; }  List *InsertListItem (List * pList, int num) {     data_t data;     int cnt;     List *pCur = NULL;      if (!pList || num < 0)         return pList;       pCur = pList;      List *insertnode = (List *) malloc (sizeof (List));      if (!insertnode)     {         printf ("malloc failed!\r\n");         return pList;     }      printf ("input you what want to insert: ");      scanf ("%d", &data);      insertnode->data = data;      insertnode->pNext = NULL;      if (!pCur) { /* the link is empty. */         pList = insertnode;         return pList;     }     if (num == 1) {         insertnode->pNext = pList;         pList = insertnode;         return pList;     }     cnt = 1;     pCur = pList;     while (pCur->pNext) {         cnt++;         if (cnt == num)                 break;         pCur = pCur->pNext;     }     insertnode->pNext = pCur->pNext;     pCur->pNext = insertnode;     /*     if (1 == num) {         pCur = pCur->pNext;          pList->pNext = insertnode;          insertnode->pNext = pCur;     } else if (num > 1 && num <= len)     {         int ncount = 0;         List *temp = NULL;          while (ncount != num - 1)         {             ncount++;              temp = pCur;              pCur = pCur->pNext;         }         temp->pNext = insertnode;          insertnode->pNext = pCur;     }     else if (len == num)     {         AddNode (pList, 1);     }     */     return pList; }   /* 链表的反转 */ List *ReverseList (List * pList) {     List *Current = pList;     List *Next = Current->pNext;     List *Prev = NULL;      if (!pList)         return pList;      while (Next) {         Prev = Next->pNext;          Next->pNext = Current;          Current = Next;          Next = Prev;     }     pList->pNext = NULL;      pList = Current;      return pList; }  /*链表的反转第二种方法*/     List * ReverseList1 (List * pList) {     if (NULL == pList)     {         return pList;     }     List *Current = pList;     List *pTemp = NULL;     while (Current->pNext)     {         pTemp = Current->pNext;          Current->pNext = pTemp->pNext;          //pTemp->pNext = pList->pNext;1//在头结点没有数据的情况下使用1和2          //pList->pNext = pTemp;2         pTemp->pNext = pList;          pList = pTemp;     }     Current->pNext = pList;      pList = Current->pNext;      Current->pNext = NULL;      return pList; }  /*判断是否有环*/ int is_LoopList (List * pList) {     List *p1, *p2;     p1 = p2 = pList;      if (!pList || !(pList->pNext))     {         return 0;     }      while (p2->pNext != NULL && p2->pNext->pNext != NULL)     {         p1 = p1->pNext;         p2 = p2->pNext->pNext;         if (p1 == p2)         {             return 1;         }     }      return 0; }  /*求链表的中点*/     List * ListMiddle (List * pList) {     if (!pList || !(pList->pNext))         return NULL;      List *pSlow = pList;     List *pFast = pList;      while (1)     {         if (pFast->pNext && pFast->pNext->pNext)         {             pFast = pFast->pNext->pNext;              pSlow = pSlow->pNext;         }         else         {             break;         }     }     //pList = pSlow;     return pSlow; }  /*链表排序*/ void ListSort (List * pList) {     int temp;      if (!pList || !(pList->pNext))     {         return;     }      List *pTmp = pList;      List *Tail = NULL;      while (pTmp != Tail)     {         while (pTmp->pNext != Tail)         {             if (pTmp->data < pTmp->pNext->data)             {                 temp = pTmp->data;                  pTmp->data = pTmp->pNext->data;                  pTmp->pNext->data = temp;             }             pTmp = pTmp->pNext;         }         Tail = pTmp;          pTmp = pList;     }     return; }  /* num is offset */ List *DeleteItem (List * pList, data_t * pData, int num) {     int len = ListLength (pList);      if (!pList || num < 1 || num > len)         return pList;      List *pDel = pList;     if (num == 1) { /* Del the head of link */         *pData = pList->data;         pList = pList->pNext;         free(pDel);         return pList;     }      int ncount = 0;     List *pCur = pList;     pDel = pCur->pNext;     while (pCur->pNext) {         ncount++;         if (ncount == num) {             pCur->pNext = pDel->pNext;             *pData = pDel->data;             free(pDel);             break;         }         pDel = pDel->pNext;     }      return pList;     /*     if (-1 == num)     {         List *pDel = pList;          if (NULL != pDel)         {             *pData = pList->data;              pList->pNext = pDel->pNext;              free (pDel);              pDel = NULL;         }     }      while (ncount != num - 1)     {         ncount++;          temp = pDel;          pDel = pDel->pNext;     }     *pData = pDel->data;      temp->pNext = pDel->pNext;      free (pDel);      pDel = NULL;     return pList;     */ }  List *UpdateList (List * pList, int data) {     if (NULL == pList)     {         return pList;     }     List *pTemp = pList;      int ncount = 0;     int value;      while (pTemp) {         ncount++;          if (pTemp->data == data) {             printf ("data in the list %d\r\n", ncount);             printf ("input new data:");             scanf ("%d", &value);             pTemp->data = value;             return pList;         }         pTemp = pTemp->pNext;      }     printf ("update failed!!!!\r\n");      return pList; }  List *SearchList (List * pList, int data) {     if (!pList) {         return NULL;     }     List *pTemp = pList;     int ncount = 0;      while (pTemp)     {         ncount++;         if (pTemp->data == data) {             printf ("data in the list %d\r\n", ncount);             return pTemp;         }         pTemp = pTemp->pNext;     }     printf("Not existed!\n");     return NULL; }  void DestoryList (List * pList) {     if (!pList)         return;      List *pNode = pList->pNext;     List *pTmp = NULL;      while (pNode) {         pTmp = pNode->pNext;         free (pNode);         pNode = pTmp;     }     free (pList);     pList = NULL; }  void ShowList (List * pList) {     printf ("show list: ");      if (!pList)         return;      List *pTmp = pList;     while (pTmp) {         printf ("%d  ",  pTmp->data);         pTmp = pTmp->pNext;     }     printf ("\r\n"); } 以下是list.h文件： #ifndef LIST_H_ #define LIST_H_  #define LIST_SIZE 10  enum LIST_TYPE { LIST_ERROR = -1,  LIST_OK };  typedef struct List List; typedef int data_t;  List *CreateList(int n);  int ListLength(List *pList);  List *AddNode(List *pList, int numbers);  List *InsertListItem(List *pList, int num);  List *ReverseList(List *pList);  List *ReverseList1(List *pList);  int is_LoopList(List *pList);  List *ListMiddle(List *pList);  void ListSort(List *pList);  List *DeleteItem(List *pList, data_t *pData, int num);  List *UpdateList(List *pList,int data);  List *SearchList(List *pList,int data);  void DestoryList(List *pList);  void ShowList(List *pList);  #endif

user: user-4
content: 你可以描述下具体问题，还有格式弄得美观点，你这样子实在没法看

user: user-5
content: 代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试和设断点调试（VS IDE中编译连接通过以后，按F10或F11键单步执行，按Shift+F11退出当前函数；在某行按F9设断点后按F5执行停在该断点处。）是程序员必须掌握的技能之一。