当前位置:   article > 正文

力扣(LeetCode) -设计单链表(C语言)_lletcode用c语言如何初始化结点

lletcode用c语言如何初始化结点

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 nextval 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。

在链表类中实现这些功能:

  • get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1
  • addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
  • addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
  • addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val  的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
  • deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。

示例:

  1. MyLinkedList linkedList = new MyLinkedList();
  2. linkedList.addAtHead(1);
  3. linkedList.addAtTail(3);
  4. linkedList.addAtIndex(1,2); //链表变为1-> 2-> 3
  5. linkedList.get(1); //返回2
  6. linkedList.deleteAtIndex(1); //现在链表是1-> 3
  7. linkedList.get(1); //返回3

 

提示:

  • 所有val值都在 [1, 1000] 之内。
  • 操作次数将在  [1, 1000] 之内。
  • 请不要使用内置的 LinkedList 库。



 

  1. typedef struct MyLinkedList {
  2. int val;
  3. struct MyLinkedList *next;
  4. } MyLinkedList;
  5. /** Initialize your data structure here. */
  6. MyLinkedList* myLinkedListCreate() {
  7. MyLinkedList *my_linked_list = (MyLinkedList*)malloc(sizeof(MyLinkedList));
  8. my_linked_list->next = NULL;
  9. return my_linked_list;
  10. }
  11. /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
  12. int myLinkedListGet(MyLinkedList* obj, int index) {
  13. int i = 0;
  14. MyLinkedList *p;
  15. p = obj->next;
  16. if (index < 0)
  17. return -1;
  18. while (p && i < index)
  19. {
  20. p = p->next;
  21. i++;
  22. }
  23. if (!p || i > index)
  24. return -1;
  25. return p->val;
  26. }
  27. /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
  28. void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
  29. MyLinkedList *q = (MyLinkedList*)malloc(sizeof(MyLinkedList));
  30. q->next = obj->next;
  31. q->val = val;
  32. obj->next = q;
  33. }
  34. /** Append a node of value val to the last element of the linked list. */
  35. void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
  36. MyLinkedList *p;
  37. MyLinkedList *q = (MyLinkedList*)malloc(sizeof(MyLinkedList));
  38. p = obj;
  39. while (p->next)
  40. {
  41. p = p->next;
  42. }
  43. p->next = q;
  44. q->val = val;
  45. q->next = NULL;
  46. }
  47. /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
  48. void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
  49. MyLinkedList *p ;
  50. MyLinkedList *q = (MyLinkedList*)malloc(sizeof(MyLinkedList));
  51. int i = 0;
  52. if (index <= 0)
  53. {
  54. q->next = obj->next;
  55. q->val = val;
  56. obj->next = q;
  57. }
  58. else
  59. {
  60. p = obj;
  61. while (p && i < index)
  62. {
  63. p = p->next;
  64. i++;
  65. }
  66. if (!p || i > index)
  67. return;
  68. q->next = p->next;
  69. p->next = q;
  70. q->val = val;
  71. }
  72. }
  73. /** Delete the index-th node in the linked list, if the index is valid. */
  74. void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
  75. int i = 0;
  76. MyLinkedList *p, *q;
  77. p = obj;
  78. if (index < 0)
  79. return;
  80. while (p->next && i < index)
  81. {
  82. p = p->next;
  83. i++;
  84. }
  85. if (!p->next || i > index)
  86. return;
  87. q = p->next;
  88. p->next = q->next;
  89. free(q);
  90. }
  91. void myLinkedListFree(MyLinkedList* obj) {
  92. MyLinkedList *p, *q;
  93. p = obj->next;
  94. while (p)
  95. {
  96. q = p->next;
  97. free(p);
  98. p = q;
  99. }
  100. obj->next = NULL;
  101. }
  102. /**
  103. * Your MyLinkedList struct will be instantiated and called as such:
  104. * MyLinkedList* obj = myLinkedListCreate();
  105. * int param_1 = myLinkedListGet(obj, index);
  106. * myLinkedListAddAtHead(obj, val);
  107. * myLinkedListAddAtTail(obj, val);
  108. * myLinkedListAddAtIndex(obj, index, val);
  109. * myLinkedListDeleteAtIndex(obj, index);
  110. * myLinkedListFree(obj);
  111. */

 

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

闽ICP备14008679号