typedef struct ListNode Node;
typedef struct {
/* 一个虚拟的节点,node.next才是链表的头节点 */
Node node;
/* 用于记录链表的尾节点 */
Node *rear;
/* 用于记录链表的长度,根据长度判断,检索插入等操作是否合法 */
int len;
} MyLinkedList;
/** Initialize your data structure here. */
void print(Node *head){
return;
while(head){
printf("%d->", head->val);
head = head->next;
}
printf("\n");
}
MyLinkedList* myLinkedListCreate() {
MyLinkedList * list = (MyLinkedList *)malloc(sizeof(MyLinkedList));
list->node.next = NULL;
/* 尾节点指向虚拟节点,便于从尾添加 */
list->rear = &(list->node);
list->len = 0;
return list;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int myLinkedListGet(MyLinkedList* obj, int index) {
Node *head = obj->node.next;
int i=0;
/* 先判断是否合法 */
if(index<0 || index>= obj->len){
return -1;
}
while(head){
if(i == index){
return head->val;
}
head = head->next;
i++;
}
return -1;
}
/** Append a node of value val to the last element of the linked list. */
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
Node *node = (Node *)malloc(sizeof(Node));
node->val = val;
node->next = NULL;
obj->rear->next = node;
obj->rear = node;
obj->len++;
print(obj->node.next);
return;
}
/** 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. */
void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
Node *head = obj->node.next;
/* 当添加第一个节点时, 从尾巴添加,因为要修改rear指针,所以直接调用尾添加的接口即可 */
if(obj->len == 0){
myLinkedListAddAtTail(obj, val);
return;
}
Node *node = (Node *)malloc(sizeof(Node));
node->val = val;
node->next = head;
obj->node.next = node;
obj->len++;
print(obj->node.next);
return;
}
/** 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. */
void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
Node *head = obj->node.next;
Node *last = &obj->node;
int i = 0;
/* 先判断是否合法, 小于0的时候,转化为在0位置添加 */
if(index < 0){
index = 0;
}
/* 判断是否合法 */
if( index>obj->len){
return;
}
/* 0位置添加表示从头添加 */
if(index==0){
myLinkedListAddAtHead(obj,val);
return;
}
/* 当位置为在len位置时,表示从尾添加,直接调用尾添加接口即可 */
if(index == obj->len){
myLinkedListAddAtTail(obj, val);
return;
}
Node *node = (Node *)malloc(sizeof(Node));
node->val = val;
obj->len++;
while(head){
if(i == index){
last->next = node;
node->next = head;
print(obj->node.next);
return;
}
last = head;
head = head->next;
i++;
}
print(obj->node.next);
free(node);
}
/** Delete the index-th node in the linked list, if the index is valid. */
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
Node *head = obj->node.next;
Node *last = &obj->node;
int i= 0;
/* 对于非法的位置,以及长度为零时,直接返回 */
if(obj->len <=0 ||index<0 || index>= obj->len){
return;
}
obj->len--;
while(head){
if(i == index){
if(head == obj->rear){
obj->rear = last;
}
last->next = head->next;
free(head);
print(obj->node.next);
return;
}
last = head;
head = head->next;
i++;
}
}
void myLinkedListFree(MyLinkedList* obj) {
Node *head = obj->node.next;
Node *last = &obj->node;
while(head){
if(last != &obj->node){
free(last);
}
last = head;
head = head->next;
}
free(obj);
}
LeetCode-707-设计链表-C语言
最新推荐文章于 2022-03-31 20:19:46 发布