当前位置:   article > 正文

2024.7.22 作业

2024.7.22 作业

1.将双向链表和循环链表自己实现一遍,至少要实现创建、增、删、改、查、销毁工作

循环链表

looplinklist.h
  1. #ifndef LOOPLINKLIST_H
  2. #define LOOPLINKLIST_H
  3. #include <myhead.h>
  4. typedef int datatype;
  5. typedef struct Node
  6. {
  7. union
  8. {
  9. int len;
  10. datatype data;
  11. };
  12. struct Node *next;
  13. }Node,*NodePtr;
  14. //创建循环链表
  15. NodePtr list_create();
  16. //链表判空
  17. int list_empty(NodePtr L);
  18. //链表申请空间封装节点
  19. NodePtr apply_node(datatype e);
  20. //按位置进行查找
  21. NodePtr list_search_pos(NodePtr L,int pos);
  22. //链表尾插
  23. int list_insert_tail(NodePtr L,datatype e);
  24. //链表遍历
  25. int list_show(NodePtr L);
  26. //链表头删
  27. int list_delete_head(NodePtr L);
  28. //链表销毁
  29. void list_destroy(NodePtr L);
  30. //约瑟夫环
  31. void ysfh(NodePtr L,int m);
  32. #endif
 looplinklist.c
  1. #include "looplinklist.h"
  2. //创建循环链表
  3. NodePtr list_create()
  4. {
  5. NodePtr L=(NodePtr)malloc(sizeof(Node));
  6. if( NULL==L )
  7. {
  8. printf("创建失败\n");
  9. return NULL;
  10. }
  11. L->len = 0;
  12. L->next = L;
  13. printf("创建成功\n");
  14. return L;
  15. }
  16. //链表判空
  17. int list_empty(NodePtr L)
  18. {
  19. return L->next==L;
  20. }
  21. //链表申请空间封装节点
  22. NodePtr apply_node(datatype e)
  23. {
  24. NodePtr p = (NodePtr)malloc(sizeof(Node));
  25. if( NULL==p )
  26. {
  27. printf("申请失败\n");
  28. return NULL;
  29. }
  30. p->data = e;
  31. p->next = NULL;
  32. return p;
  33. }
  34. //按位置进行查找
  35. NodePtr list_search_pos(NodePtr L,int pos)
  36. {
  37. if( NULL==L || pos<0 || pos>L->len)
  38. {
  39. printf("查找失败\n");
  40. return NULL;
  41. }
  42. NodePtr q = L;
  43. for(int i=1;i<=pos;i++)
  44. {
  45. q=q->next;
  46. }
  47. return q;
  48. }
  49. //链表尾插
  50. int list_insert_tail(NodePtr L,datatype e)
  51. {
  52. if( NULL==L )
  53. {
  54. printf("尾插失败\n");
  55. return -1;
  56. }
  57. NodePtr p = apply_node(e);
  58. if( NULL==p )
  59. {
  60. return -1;
  61. }
  62. NodePtr q = list_search_pos(L,L->len);
  63. p->next = q->next;
  64. q->next = p;
  65. L->len++;
  66. return 0;
  67. }
  68. //链表遍历
  69. int list_show(NodePtr L)
  70. {
  71. if( NULL==L || list_empty(L) )
  72. {
  73. printf("遍历失败\n");
  74. return -1;
  75. }
  76. NodePtr q = L->next;
  77. while( q!=L )
  78. {
  79. printf("%d\t",q->data);
  80. q = q->next;
  81. }
  82. printf("\n");
  83. return 0;
  84. }
  85. //链表头删
  86. int list_delete_head(NodePtr L)
  87. {
  88. if( NULL==L || list_empty(L) )
  89. {
  90. printf("头删失败\n");
  91. return -1;
  92. }
  93. NodePtr q = L->next;
  94. L->next = q->next;
  95. L->len--;
  96. free(q);
  97. q=NULL;
  98. return 0;
  99. }
  100. //链表销毁
  101. void list_destroy(NodePtr L)
  102. {
  103. if( NULL==L )
  104. {
  105. printf("销毁失败\n");
  106. return ;
  107. }
  108. while( !list_empty(L) )
  109. {
  110. list_delete_head(L);
  111. }
  112. free(L);
  113. L=NULL;
  114. printf("销毁成功\n");
  115. return;
  116. }

双向链表 

doublelinklist.h
  1. #ifndef DOUBLELINKLIST_H
  2. #define DOUBLELINKLIST_H
  3. typedef char datatype;
  4. typedef struct Node
  5. {
  6. union
  7. {
  8. int len;
  9. datatype data;
  10. }
  11. struct Node *prio;
  12. struct Node *next;
  13. }Node,*NodePtr;
  14. //创建双向链表
  15. NodePtr list_create();
  16. //链表判空
  17. int list_empty(NodePtr L);
  18. //申请节点封装数据
  19. NodePtr apply_node(datatype e);
  20. //链表头插
  21. int list_insert_head(NodePtr L,datatype e);
  22. //链表遍历
  23. int list_show(NodePtr L);
  24. //按位置查找返回节点
  25. NodePtr list_search_pos(NodePtr L,int pos);
  26. //链表任意位置删除
  27. int list_delete_pos(NodePtr L,int pos);
  28. //链表空间释放
  29. void list_destroy(NodePtr L);
  30. #endif
doublelinklist.c
  1. #include "doublelinklist.h"
  2. //创建双向链表
  3. NodePtr list_create()
  4. {
  5. NodePtr L = (NodePtr)malloc(sizeof(Node));
  6. if( NULL==L )
  7. {
  8. printf("创建失败\n");
  9. return NULL;
  10. }
  11. L->len = 0;
  12. L->prio = NULL;
  13. L->next = NULL;
  14. printf("创建成功\n");
  15. return L;
  16. }
  17. //链表判空
  18. int list_empty(NodePtr L)
  19. {
  20. return L->next == NULL;
  21. }
  22. //申请节点封装数据
  23. NodePtr apply_node(datatype e)
  24. {
  25. NodePtr p =(NodePtr)malloc(sizeof(Node));
  26. if( NULL==p )
  27. {
  28. printf("节点申请失败\n");
  29. return NULL;
  30. }
  31. p->data = e;
  32. p->prio = NULL;
  33. p->next =NULL;
  34. return
  35. }
  36. //链表头插
  37. int list_insert_head(NodePtr L,datatype e)
  38. {
  39. if( NULL==L )
  40. {
  41. printf("头插失败\n");
  42. return -1;
  43. }
  44. NodePtr p = apply_node(e);
  45. if( NULL==p )
  46. {
  47. return -1;
  48. }
  49. if( list_empty(L) )
  50. {
  51. p->prio = L;
  52. L-next = p;
  53. }
  54. else
  55. {
  56. p->prio = L;
  57. p->next = L->next;
  58. L->next->prio = p;
  59. L-next = p;
  60. }
  61. L->len++;
  62. return 0;
  63. }
  64. //链表遍历
  65. int list_show(NodePtr L)
  66. {
  67. if( NULL==L || list_empty(L) )
  68. {
  69. printf("遍历失败\n");
  70. return -1;
  71. }
  72. printf("当前数据为:");
  73. NodePtr q = L->next;
  74. while( q )
  75. {
  76. printf("%c\t",q->data);
  77. q=q->next;
  78. }
  79. printf("\n");
  80. return 0;
  81. }
  82. //按位置查找返回节点
  83. NodePtr list_search_pos(NodePtr L,int pos)
  84. {
  85. if( NULL==L || list_empty(L) || pos<0 || pos>L->len )
  86. {
  87. printf("查找失败\n");
  88. return NULL;
  89. }
  90. NodePtr q = L;
  91. for(int i=1;i<=pos;i++)
  92. {
  93. q = q->next;
  94. }
  95. return q;
  96. }
  97. //链表任意位置删除
  98. int list_delete_pos(NodePtr L,int pos)
  99. {
  100. if( NULL==L || list_empty(L) || pos<1 || pos>L->len )
  101. {
  102. printf("删除失败\n");
  103. return -1;
  104. }
  105. NodePtr q = list_search_pos(L,pos);
  106. if( q->next==NULL )
  107. {
  108. q->prio->next==NULL;
  109. }
  110. else
  111. {
  112. q->prio->next = q->next;
  113. q->next->prio = q->prio;
  114. }
  115. free(q);
  116. q=NULL;
  117. L->len--;
  118. return 0;
  119. }
  120. //链表空间释放
  121. void list_destroy(NodePtr L)
  122. {
  123. if( NULL==L )
  124. {
  125. printf("释放失败\n");
  126. return ;
  127. }
  128. while( !list_empty(L) )
  129. {
  130. list_delete_pos(L,1);
  131. }
  132. free(L);
  133. L=NULL;
  134. printf("链表释放成功\n");
  135. return ;
  136. }

 

2.使用循环链表完成约瑟夫环问题

  1. //约瑟夫环
  2. void ysfh(NodePtr L,int m)
  3. {
  4. NodePtr p = L->next;
  5. NodePtr q = L;
  6. while( p->next!=p )
  7. {
  8. for(int i=1;i<m;i++)
  9. {
  10. q = p;
  11. p = p->next;
  12. if(p==L)
  13. {
  14. q=q->next;
  15. p=p->next;
  16. }
  17. }
  18. printf("%d\t",p->data);
  19. NodePtr a = p; //标记
  20. q->next = p->next; //p->next 孤立
  21. p=p->next;
  22. free(a); //释放
  23. a = NULL;
  24. }
  25. printf("\n最后剩下的节点: %d\n", p->data);
  26. free(p); // 释放最后一节点
  27. }

3.使用栈,完成进制转换

输入:一个整数,进制数

输出:该数的对应的进制数

  1. void DC(StackPtr S,int m,int n)
  2. {
  3. while(m/n!=0)
  4. {
  5. S->top++;
  6. S->data[S->top]=m%n;
  7. m=m/n;
  8. }
  9. S->top++;
  10. S->data[S->top]=1;
  11. while(S->top!=-1)
  12. {
  13. printf("%d",S->data[S->top]);
  14. S->top--;
  15. }
  16. printf("\n");
  17. }

思维导图

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

闽ICP备14008679号