当前位置:   article > 正文

Day4.

Day4.

单链表

  1. #include <head.h>
  2. typedef struct List{
  3. int value;
  4. struct List *pointe;
  5. }*list;
  6. list create_space()
  7. {
  8. list s=(struct List *)malloc(sizeof(struct List)); //向堆区申请空间
  9. s->pointe = NULL;//初始化
  10. s->value = 0;
  11. return s;
  12. }
  13. list inserhead_list(list head,int value)
  14. {
  15. list s = create_space();
  16. if(s == NULL)
  17. {
  18. return head;
  19. }
  20. s->value = value;
  21. if(head == NULL)
  22. {
  23. head = s;
  24. return head;
  25. }else{
  26. s->pointe = head;
  27. head = s;
  28. }
  29. return head;
  30. }
  31. //头删
  32. list delete_list(list head)
  33. {
  34. list p = head;
  35. if(head == NULL)
  36. {
  37. free(head);
  38. return head;
  39. }
  40. head = p->pointe;
  41. free(p);
  42. return head;
  43. }
  44. void output(list head)
  45. {
  46. if(head == NULL)
  47. {
  48. return;
  49. }
  50. while(head)
  51. {
  52. printf("%d ",head->value);
  53. head = head->pointe;
  54. }
  55. }
  56. int main(int argc,const char *argv[])
  57. {
  58. list head = NULL;
  59. int value;
  60. for (int i = 0; i < 5; i++)
  61. {
  62. printf("请输入插入的值:");
  63. scanf("%d",&value);
  64. head = inserhead_list(head,value);
  65. }
  66. output(head);
  67. head=delete_list(head);
  68. puts("");
  69. output(head);
  70. return 0;
  71. }

双链表

  1. #include <head.h>
  2. typedef struct List{
  3. int value;
  4. struct List *next;
  5. struct List *priv;
  6. }*list;
  7. list create_space()
  8. {
  9. list s=(struct List *)malloc(sizeof(struct List)); //向堆区申请空间
  10. s->next = NULL;//初始化
  11. s->priv = NULL;
  12. s->value = 0;
  13. return s;
  14. }
  15. 头插
  16. list inserhead_list(list head,int value)
  17. {
  18. list s = create_space();
  19. if(s == NULL)
  20. {
  21. return head;
  22. }
  23. s->value = value;
  24. if(head == NULL)
  25. {
  26. head = s;
  27. return head;
  28. }else{
  29. s->next = head;
  30. head->priv = s;
  31. head = s;
  32. }
  33. return head;
  34. }
  35. //头删
  36. list delete_list(list head)
  37. {
  38. list p = head;
  39. if(head == NULL)
  40. {
  41. free(head);
  42. return head;
  43. }
  44. head = p->next;
  45. head->priv = NULL;
  46. free(p);
  47. return head;
  48. }
  49. int output(list head)
  50. {
  51. //1,判断链表为空
  52. if(NULL ==head)
  53. return -1;
  54. //2.正向遍历
  55. puts("正向遍历");
  56. list p=head;
  57. while(p->next!=NULL)
  58. {
  59. printf("%d\t",p->value);
  60. p=p->next;
  61. }
  62. printf("%d\t",p->value);
  63. puts("\n逆向遍历");
  64. while(p!=NULL)
  65. {
  66. printf("%d\t",p->value);
  67. p=p->priv;
  68. }
  69. puts("");
  70. return 0;
  71. }
  72. int main(int argc,const char *argv[])
  73. {
  74. list head = NULL;
  75. int value;
  76. for (int i = 0; i < 5; i++)
  77. {
  78. printf("请输入插入的值:");
  79. scanf("%d",&value);
  80. head = inserhead_list(head,value);
  81. }
  82. output(head);
  83. head=delete_list(head);
  84. puts("------------");
  85. output(head);
  86. return 0;
  87. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/76371
推荐阅读
相关标签
  

闽ICP备14008679号