码迷,mamicode.com
首页 > 其他好文 > 详细

链表的常用操作

时间:2020-05-15 11:37:56      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:nbsp   last   --   you   color   get   ati   length   ==   

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <stdbool.h>
  4 
  5 typedef struct {
  6     int val;
  7     struct MyLinkedList *next;
  8 } MyLinkedList;
  9 
 10 /** Initialize your data structure here. */
 11 
 12 MyLinkedList* myLinkedListCreate() {
 13     MyLinkedList *result = (MyLinkedList *)malloc(sizeof(MyLinkedList));
 14     result->val = 0;
 15     result->next = NULL;
 16     return result;
 17 }
 18 
 19 /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
 20 int myLinkedListGet(MyLinkedList* obj, int index) {
 21     if (index < 0 || index >= obj->val) {
 22         return -1;
 23     }
 24 
 25     while (index >= 0) {
 26         obj = obj->next;
 27         index--;
 28     }
 29 
 30     return obj->val;
 31 }
 32 
 33 /** 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. */
 34 void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
 35     if (obj == NULL) {
 36         return;
 37     }
 38 
 39     obj->val++;
 40     MyLinkedList *temp = (MyLinkedList*)malloc(sizeof(MyLinkedList));
 41     temp->val = val;
 42     temp->next = obj->next;
 43     obj->next = temp;
 44 }
 45 
 46 /** Append a node of value val to the last element of the linked list. */
 47 void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
 48     if (obj == NULL) {
 49         return;
 50     }
 51 
 52     obj->val++;
 53     MyLinkedList *temp = (MyLinkedList*)malloc(sizeof(MyLinkedList));
 54     temp->next = NULL;
 55     temp->val = val;
 56 
 57     while (obj->next != NULL) {
 58         obj = obj->next;
 59     }
 60 
 61     obj->next = temp;
 62 }
 63 
 64 /** 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. */
 65 void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
 66     if (obj == NULL || index < 0 || index > obj->val) {
 67         return;
 68     }
 69 
 70     if (index == obj->val) {
 71         myLinkedListAddAtTail(obj, val);
 72         return;
 73     }
 74 
 75     obj->val++;
 76     MyLinkedList *temp = (MyLinkedList*)malloc(sizeof(MyLinkedList));
 77     temp->val = val;
 78     temp->next = NULL;
 79 
 80     while (index > 0) {
 81         obj = obj->next;
 82         index--;
 83     }
 84 
 85     temp->next = obj->next;
 86     obj->next = temp;
 87 }
 88 
 89 /** Delete the index-th node in the linked list, if the index is valid. */
 90 void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
 91     if (obj == NULL || obj->val == 0 || index < 0 || index + 1 > obj->val) {
 92         return;
 93     }
 94 
 95     obj->val--;
 96     while (index > 0) {
 97         obj = obj->next;
 98         index--;
 99     }
100 
101     MyLinkedList *temp = obj->next;
102     obj->next = temp->next;
103     free(temp);
104 }
105 
106 void myLinkedListFree(MyLinkedList* obj) {
107     if (obj->next == NULL) {
108         free(obj);
109         return;
110     }
111 
112     myLinkedListFree(obj->next);
113     obj->next = NULL;
114     myLinkedListFree(obj);
115 }
116 
117 /**
118  * Your MyLinkedList struct will be instantiated and called as such:
119  * MyLinkedList* obj = myLinkedListCreate();
120  * int param_1 = myLinkedListGet(obj, index);
121 
122  * myLinkedListAddAtHead(obj, val);
123 
124  * myLinkedListAddAtTail(obj, val);
125 
126  * myLinkedListAddAtIndex(obj, index, val);
127 
128  * myLinkedListDeleteAtIndex(obj, index);
129 
130  * myLinkedListFree(obj);
131 */
132 
133 void show(MyLinkedList *obj)
134 {
135     while (obj != NULL) {
136         printf("%d ", obj->val);
137         obj = obj->next;
138     }
139     printf("\n");
140 }
141 
142 int main()
143 {
144     MyLinkedList *obj = NULL;
145     obj = myLinkedListCreate();
146     myLinkedListAddAtHead(obj, 1);
147     show(obj);
148     myLinkedListAddAtTail(obj, 2);
149     show(obj);
150     myLinkedListAddAtIndex(obj, 0, 3);
151     show(obj);
152     myLinkedListAddAtIndex(obj, 2, 4);
153     show(obj);
154     myLinkedListDeleteAtIndex(obj, 0);
155     show(obj);
156     myLinkedListDeleteAtIndex(obj, 2);
157     show(obj);
158 //    myLinkedListFree(obj);
159 
160     return 0;
161 }

 

链表的常用操作

标签:nbsp   last   --   you   color   get   ati   length   ==   

原文地址:https://www.cnblogs.com/hemeiwolong/p/12893694.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!