1. 程式人生 > >leetcode 707設計連結串列 c語言實現

leetcode 707設計連結串列 c語言實現

這道題我用c寫的,寫過程中出現了好多bug,唉,還是對連結串列不是很熟,比如在插入結點時,如果是首結點該如何處理。好在最後還能正常寫出來了~~~

題目描述

設計連結串列的實現。您可以選擇使用單鏈表或雙鏈表。單鏈表中的節點應該具有兩個屬性:val 和 next。val 是當前節點的值,next 是指向下一個節點的指標/引用。如果要使用雙向連結串列,則還需要一個屬性 prev 以指示連結串列中的上一個節點。假設連結串列中的所有節點都是 0-index 的。

在連結串列類中實現這些功能:

get(index):獲取連結串列中第 index 個節點的值。如果索引無效,則返回-1。
addAtHead(val):在連結串列的第一個元素之前新增一個值為 val 的節點。插入後,新節點將成為連結串列的第一個節點。
addAtTail(val):將值為 val 的節點追加到連結串列的最後一個元素。
addAtIndex(index,val):在連結串列中的第 index 個節點之前新增值為 val 的節點。如果 index 等於連結串列的長度,則該節點將附加到連結串列的末尾。如果 index 大於連結串列長度,則不會插入節點。
deleteAtIndex(index):如果索引 index 有效,則刪除連結串列中的第 index 個節點。

示例

MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1,2);   //連結串列變為1-> 2-> 3
linkedList.get(1);            //返回2
linkedList.deleteAtIndex(1);  //現在連結串列是1-> 3
linkedList.get(1);            //返回3

程式碼


typedef struct MyLinkList{
int data; //資料域 struct MyLinkList *next; //結點 } MyLinkedList; /** Initialize your data structure here. */ MyLinkedList* myLinkedListCreate() { MyLinkedList *obj=(MyLinkedList *)malloc(sizeof(MyLinkedList)); obj->next=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) { MyLinkedList *p=obj->next; int j=0; while(p!=NULL && j<index) //找到第index個結點 { j++; p=p->next; } if(p==NULL) return -1; else { int e=p->data; return e; } } /** 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 *p=obj,*s=(MyLinkedList *)malloc(sizeof(MyLinkedList)); s->data=val; s->next=p->next; p->next=s; } /** Append a node of value val to the last element of the linked list. */ void myLinkedListAddAtTail(MyLinkedList* obj, int val) { MyLinkedList *p=obj,*s=(MyLinkedList *)malloc(sizeof(MyLinkedList)); s->data=val; s->next=NULL; //尾結點指向空 if(!p) { obj=s; return; } while(p->next!=NULL) //找到尾結點 { p=p->next; } p->next=s; } /** 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 j=0; MyLinkedList *p=obj->next,*s; if(index==0) //如果在首結點插入,即插入到第一個結點 { //也可呼叫首結點插入函式 /* myLinkedListAddAtHead(obj,val); return; */ s=(MyLinkedList *)malloc(sizeof(MyLinkedList)); s->data=val; obj->next=s; s->next=p; return; } while(p!=NULL&&j<index-1) { j++; p=p->next; } if(p==NULL) return; else { s=(MyLinkedList *)malloc(sizeof(MyLinkedList)); s->data=val; s->next=p->next; p->next=s; } } /** Delete the index-th node in the linked list, if the index is valid. */ void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) { int j=0; MyLinkedList *p=obj->next,*s; //s=(MyLinkedList *)malloc(sizeof(MyLinkedList)); while(p!=NULL&&j<index-1) { j++; p=p->next; } if(p==NULL) return; else { s=p->next; if(s==NULL) return; else { p->next=s->next; free(s); } } } void myLinkedListFree(MyLinkedList* obj) { MyLinkedList *p=obj,*q=p->next; while(q!=NULL) { free(p); p=q; q=q->next; } free(p); } /** * Your MyLinkedList struct will be instantiated and called as such: * struct MyLinkedList* obj = myLinkedListCreate(); * int param_1 = myLinkedListGet(obj, index); * myLinkedListAddAtHead(obj, val); * myLinkedListAddAtTail(obj, val); * myLinkedListAddAtIndex(obj, index, val); * myLinkedListDeleteAtIndex(obj, index); * myLinkedListFree(obj); */