Skip to content

Instantly share code, notes, and snippets.

@tsundokul
Created February 1, 2019 10:27
Show Gist options
  • Save tsundokul/1c82444bd97dde25895ee8fb60f24f50 to your computer and use it in GitHub Desktop.
Save tsundokul/1c82444bd97dde25895ee8fb60f24f50 to your computer and use it in GitHub Desktop.
Double linked list implementation for leetcode 707. Design Linked List
typedef struct Node Node;
struct Node {
int val;
Node* prev;
Node* next;
};
typedef struct {
int count;
Node* start;
} MyLinkedList;
/** Initialize your data structure here. */
MyLinkedList* myLinkedListCreate() {
MyLinkedList* list = (MyLinkedList*)malloc(sizeof(MyLinkedList));
list->count = 0;
list->start = NULL;
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) {
int val = -1;
if ((index < obj->count) && obj->count) {
Node* start = obj->start;
for(size_t c = 0; c <= index; c++) {
val = start->val;
start = start->next;
}
}
return val;
}
/** 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) {
if(index < 0 || index > obj->count)
return;
Node* node = (Node*)malloc(sizeof(Node));
node->val = val;
node->prev = NULL;
node->next = NULL;
if(!index) {
node->next = obj->start;
if(obj->start != NULL) {
Node* tmp = obj->start;
tmp->prev = node;
}
obj->start = node; }
else {
Node* pos = obj->start;
for(size_t c = 0; c < index-1; c++)
pos = pos->next;
node->prev = pos;
node->next = pos->next;
pos->next = node;
if(node->next != NULL) {
Node* tmp = node->next;
tmp->prev = node;
}
}
obj->count++;
}
/** 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) {
myLinkedListAddAtIndex(obj, 0, val);
}
/** Append a node of value val to the last element of the linked list. */
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
myLinkedListAddAtIndex(obj, obj->count, val);
}
void myLinkedListFree(MyLinkedList* obj) {
Node* tofree = obj->start;
// Free nodes one by one
while(obj->count--) {
obj->start = tofree->next;
tofree = obj->start;
}
free(obj);
}
/** Delete the index-th node in the linked list, if the index is valid. */
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
if(!obj->count || index < 0 || index > obj->count-1)
return;
Node* todel = obj->start;
for(size_t c = 0; c < index; c++)
todel = todel->next;
if(todel->prev != NULL) {
Node* tmp = todel->prev;
tmp->next = todel->next;
}
if(todel->next != NULL) {
Node* tmp = todel->next;
tmp->prev = todel->prev;
}
free(todel);
obj->count--;
if(!obj->count)
obj->start = NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment