user-0 提问: 链表求助

回复:
user: user-1
content: 基本符合要求了，是否有bug未详细测试  #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h>  typedef struct node {     int data;     struct node *next; }linknode, *linklist;  //创建链表 linklist createLink() {     linklist pHead = (linknode*)malloc(sizeof(linknode));     if ( pHead == NULL )     {         printf("内存分配失败\n");         return NULL;     }     pHead->data = 0;     pHead->next = NULL;     return pHead; }  //链表赋值 void AddNode(linklist pHead) {     int a;     linklist pTemp = NULL, pCurr = pHead;     printf("输入链表结点的值，-1结束\n");     while ( 1 )     {         scanf("%d", &a);         if ( a != -1 )         {             pTemp = (linknode*)malloc(sizeof(linknode));             pTemp->data = a;             pTemp->next = NULL;             pCurr->next = pTemp;             pCurr = pTemp;         }         else         {             break;         }     } }  //输出链表 void printLink(linklist pHead) {     linklist pTemp = pHead->next;     while ( pTemp != NULL )     {         printf("%d ", pTemp->data);         pTemp = pTemp->next;     }     printf("\n"); }  //删除大于x不大于y的结点值 void deletedata(linklist L, int x, int y) {     linklist pOne = L, pTwo = L->next, pTemp = NULL;          //遍历查找符合条件的结点     while ( pTwo != NULL )     {         if ( pTwo->data > x && pTwo->data <= y )         {             if ( pTwo->next != NULL )             {                 pOne->next = pTwo->next;                 pTemp = pTwo;                 free(pTemp);                 pTemp = NULL;                 pTwo = pOne;             }             else             {                 pOne->next = NULL;                 free(pTwo);                                 pTwo = pOne;             }         }         pOne = pTwo;         pTwo = pTwo->next;     } }  int main() {     linklist pHead;     pHead = createLink();     AddNode(pHead);     printLink(pHead);      deletedata(pHead, 60, 70);     printLink(pHead);      system("pause");     return 0; }

user: user-2
content: 先自己写写吧，比如先创建一个带头结点的链表，然后输出链表。这两个函数先写出来，后面再写删除的函数～

user: user-2
content: //删除大于x不大于y的结点值 void deletedata(linklist L, int x, int y) {     linklist pOne = L, pTwo = L->next, pTemp = NULL;      //遍历查找符合条件的结点     while ( pTwo != NULL )     {         if ( pTwo->data > x && pTwo->data <= y )         {             pTemp = pTwo;             pOne->next = pTwo->next;             free(pTemp);             pTwo = pOne->next;             continue; #if 0             if ( pTwo->next != NULL )             {                 pOne->next = pTwo->next;                 pTemp = pTwo;                 free(pTemp);                 pTemp = NULL;                 pTwo = pOne;             }             else             {                 pOne->next = NULL;                 free(pTwo);                 pTwo = pOne;             } #endif         }         pOne = pTwo;         pTwo = pTwo->next;     } } 供参考～ 楼上写的没问题，如上是我在他的基础上修改的，可以对比一下～

user: user-3
content: 最好将main修改为 int main() {      int x,y;  	linklist pHead;     pHead = createLink();     AddNode(pHead);     printLink(pHead);  	scanf("%d%d",&x,&y);//指出x,y的范围     deletedata(pHead, x, y);     printLink(pHead);       system("pause");     return 0; }

user: user-3
content: deletedata函数可以简化为如下 void deletedata(linklist L, int x, int y) {     linklist pOne = L, pTwo = L->next, pTemp = NULL;           //遍历查找符合条件的结点     while ( pTwo != NULL )     {         if (pTwo && pTwo->data > x && pTwo->data <= y )         {                 pOne->next = pTwo->next;                 pTemp = pTwo;                 free(pTemp);                 pTemp = NULL;                 pTwo = pOne;         }         pOne = pTwo;         pTwo = pTwo->next;     } }