【链表】设计链表

typedef struct myLinkedList {
int val;
struct myLinkedList *next;
struct myLinkedList *prev;
} MyLinkedList;
/** Initialize your data structure here. */
MyLinkedList* myLinkedListCreate() {
MyLinkedList *obj = (MyLinkedList *)malloc(sizeof(MyLinkedList));
obj->val = -1;
obj->next = NULL;
obj->prev = NULL;
return obj;
}
/** 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 count;
MyLinkedList *t;
/*t = obj;
while (t)
{
printf("%d ", t->val);
t = t->next;
}
printf("\\n");
*/
count = -1;
t = obj;
while (1)
{
t = t->next;
if (t == NULL)
break;
count++;
if (count == index)
return t->val;
}
return -1;
}
/** 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) {
MyLinkedList *node = (MyLinkedList *)malloc(sizeof(MyLinkedList));
node->val = val;
if (obj->next == NULL)
{
obj->next = node;
node->next = NULL;
node->prev = obj;
}
else
{
MyLinkedList *t = obj->next;
obj->next = node;
node->prev = obj;
node->next = t;
t->prev = node;
}
}
/** Append a node of value val to the last element of the linked list. */
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
MyLinkedList *t = obj;
while (t->next != NULL)
{
t = t->next;
}
MyLinkedList *node = (MyLinkedList *)malloc(sizeof(MyLinkedList));
node->val = val;
t->next = node;
node->prev = t;
node->next = NULL;
}
/** 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) {
int len;
MyLinkedList *t, *tprev;
if (index < 0)
{
myLinkedListAddAtHead(obj, val);
return;
}
len = 0;
t = obj;
while (t->next != 0)
{
t = t->next;
len++;
}
if (len < index)
return;
if (len == index)
{
myLinkedListAddAtTail(obj, val);
return;
}
int count = -1;
t = obj;
while (1)
{
t = t->next;
if (t == NULL)
break;
count++;
if (count == index)
break;
}
MyLinkedList *node = (MyLinkedList *)malloc(sizeof(MyLinkedList));
node->val = val;
tprev = t->prev;
tprev->next = node;
node->prev = tprev;
node->next = t;
t->prev = node;
}
/** Delete the index-th node in the linked list, if the index is valid. */
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
if (index < 0)
return;
int len;
MyLinkedList *t, *tprev, *tnext;
len = -1;
t = obj;
while (t->next != 0)
{
t = t->next;
len++;
}
if (len < index)
return;
if (len == index)
{
tprev = t->prev;
t->prev = NULL;
tprev->next = NULL;
return;
}
int count = -1;
t = obj;
while (1)
{
t = t->next;
if (t == NULL)
break;
count++;
if (count == index)
break;
}
tprev = t->prev;
tnext = t->next;
tprev->next = tnext;
tnext->prev = tprev;
}
void myLinkedListFree(MyLinkedList* obj) {
MyLinkedList *t, *cur;
t = obj;
while (1)
{
cur = t->next;
free(t);
if (cur == NULL)
break;
t = cur;
}
}
