• 删除负数* -链表


    一个带有表头节点的链表中,链表的每个节点保存了非零的整型数据。现请编写函数删除已有的链表中所有数值小于0的节点。

    说明:(1)用带表头的单向链表的方式保存数据,每一个节点的数值域保存一个非零整型数。

    (2)预设代码包括主函数、建立链表函数、输出链表函数,请编写删除链表中所有数值小于0的节点的函数。

    结构的定义:

    struct node

    { int data;  struct node *next;

    };

    typedef struct node NODE;
    typedef struct node * PNODE;

    删除所有无效数值结点的函数原型:void deleteneg(PNODE head),

    其中:参数head是带有头结点的单向链表的头指针。

    程序输入:在建立链表时,每次插入到头结点后的结点数据,以0为结束。

    预设代码

    前置代码

    view plainprint?

    1. /* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */  
    2. #include <stdio.h>  
    3. #include <stdlib.h>  
    4.     
    5. struct node    
    6. {  
    7.     int data;    
    8.     struct node * next;    
    9. };    
    10.     
    11. typedef struct node NODE;   
    12. typedef struct node * PNODE;  
    13.    
    14. PNODE constructlist( PNODE head, int num );  
    15. void outlist( PNODE head );  
    16. void deleteneg( PNODE head );   
    17.     
    18. int main ( )    
    19. {   int num=1;    
    20.     PNODE head;    
    21.     
    22.     head = (PNODE)malloc( sizeof(NODE) );    
    23.     head->next = NULL;    
    24.     head->data = -1;    
    25.     
    26.     while ( num!=0 )    
    27.     {   scanf("%d", &num);    
    28.         if ( num!=0 )    
    29.            constructlist (head, num);    
    30.     }    
    31.     deleteneg( head );  
    32.     outlist( head );    
    33.     return 0;    
    34. }    
    35.     
    36. PNODE constructlist( PNODE head, int num )  
    37. {   PNODE p;  
    38.     p = (PNODE)malloc( sizeof(NODE) );   
    39.     p->data = num;  
    40.     p->next = head->next;   
    41.     head->next = p;  
    42.     return head;  
    43. }  
    44.   
    45. void outlist( PNODE head )    
    46. {   PNODE p;    
    47.     p = head->next;    
    48.     while ( p != NULL )    
    49.     {   printf("%d\n", p->data);    
    50.         p = p->next;    
    51.     }    
    52. }    
    53.     
    54. /* This is an example for list. Please programme your code like it.  
    55. void deleteneg( PNODE head )  
    56. {    
    57. }  
    58. */  
    59.   
    60. /* PRESET CODE END - NEVER TOUCH CODE ABOVE */  
      1. /* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
      2. #include <stdio.h>
      3. #include <stdlib.h>
      4. /*一个带有表头节点的链表中,链表的每个节点保存了非零的整型数据。
      5. 现请编写函数删除已有的链表中所有数值小于0的节点。
      6. 说明:(1)用带表头的单向链表的方式保存数据,每一个节点的数值域保存一个非零整型数。
      7. (2)预设代码包括主函数、建立链表函数、输出链表函数,
      8. 请编写删除链表中所有数值小于0的节点的函数。
      9. 结构的定义:
      10. struct node
      11. { int data; struct node *next;
      12. };
      13. typedef struct node NODE;
      14. typedef struct node * PNODE;
      15. 删除所有无效数值结点的函数原型:void deleteneg(PNODE head),
      16. 其中:参数head是带有头结点的单向链表的头指针。
      17. 程序输入:在建立链表时,每次插入到头结点后的结点数据,以0为结束。
      18. */
      19. struct node
      20. {
      21. int data;
      22. struct node * next;
      23. };
      24. typedef struct node NODE;
      25. typedef struct node * PNODE;
      26. PNODE constructlist( PNODE head, int num );
      27. void outlist( PNODE head );
      28. void deleteneg( PNODE head );
      29. int main ( )
      30. { int num=1;
      31. PNODE head;
      32. head = (PNODE)malloc( sizeof(NODE) );
      33. head->next = NULL;
      34. head->data = -1;
      35. while ( num!=0 )
      36. { scanf("%d", &num);
      37. if ( num!=0 )
      38. constructlist (head, num);
      39. }
      40. deleteneg( head );
      41. outlist( head );
      42. return 0;
      43. }
      44. PNODE constructlist( PNODE head, int num )
      45. { PNODE p;
      46. p = (PNODE)malloc( sizeof(NODE) );
      47. p->data = num;
      48. p->next = head->next;
      49. head->next = p;
      50. return head;
      51. }
      52. void outlist( PNODE head )
      53. { PNODE p;
      54. p = head->next;
      55. while ( p != NULL )
      56. { printf("%d\n", p->data);
      57. p = p->next;
      58. }
      59. }
      60. void deleteneg(PNODE head)
      61. {
      62. PNODE p,temp1,temp2;
      63. p = head;
      64. int flag = 0;
      65. while ( p->next != NULL )
      66. {
      67. temp1 = p -> next;
      68. while( temp1->data <= 0)
      69. {
      70. p->next = temp1->next;
      71. free(temp1);
      72. temp1 -> data= 2;
      73. if(p ->next !=NULL)
      74. temp1 = p -> next;
      75. }
      76. if(p ->next !=NULL)
      77. p = p -> next;
      78. }
      79. }

  • 相关阅读:
    AUTOSAR汽车电子嵌入式编程精讲300篇-基于嵌入式惯导技术的移动靶车设计(中)
    失控的返回值
    《DevOps实践指南》- 读书笔记(四)
    @Async注解的坑,小心
    开源图像分类工具箱MMClassification安装及使用示例
    【HCIP】路由策略、策略路由
    企业开发中名词解析— —加密与脱敏及部分开发规范
    python——基础排坑+经验总结(持续更新)
    javaEE - 2(11000字详解多线程)
    22款奔驰GLS450升级香氛负离子 清除异味
  • 原文地址:https://blog.csdn.net/bitucas/article/details/125530303