Click here to load reader

418115: การเขียนโปรแกรมโครงสร้าง โครงสร้างข้อมูล II

Embed Size (px)

DESCRIPTION

418115: การเขียนโปรแกรมโครงสร้าง โครงสร้างข้อมูล II. ประมุข ขันเงิน. Linked List. A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains a pointer to the next linked list element. - PowerPoint PPT Presentation

Citation preview

418115: II

418115: II1Linked ListA linked list can be thought of a chain of linked list elements.A linked list element contains a single data item, and contains a pointer to the next linked list element.It may also contain a pointer to the previous linked list element. In this case, we call it a doubly linked list element.2Linked List (cont.)1142712839846493Linked List Implementationstruct _LLElement{ int data; _LLElement *next, *prev;};

typedef struct _LLElement LLElement;4Linked List Implementaion (cont.)LLElement *LLElement_new( int data ) used to create a new linked list element with the given data.Usage:LLElement *element = LLElement_new(10);5Linked List Implementation (cont.)LLElement *LLElement_new( int data ){ LLElement *result = (LLElement *)malloc(sizeof(LLElement)); if (result != NULL) { result->data = data; result->next = NULL; result->prev = NULL; } return result;}6Linked List Implementaion (cont.)void LLElement_insert_after( LLElement *position, LLElement *e) inserts a linked list element e after position.7Linked List Implementation (cont.)114271283984649192thise8Linked List Implementation (cont.)114271283984649192thise9Linked List Implementation (cont.)114271283984649192thise10Linked List Implementation (cont.)114271283984649192thise11Linked List Implementation (cont.)114271283984649192thise12Linked List Implementation (cont.)114271283984649192thise13Linked List Implementation (cont.)void LLElement_insert_after( LLElement *position, LLElement *e){ e->prev = position; e->next = position->next; if (position->next != NULL) position->next->prev = e; position->next = e;}14Linked List Implementaion (cont.)void LLElement_insert_before( LLElement *position, LLElement *e) inserts the linked list element e before position.15Linked List Implementation (cont.)114271283984649192positione16Linked List Implementation (cont.)114271283984649192eposition17Linked List Implementation (cont.)114271283984649192eposition18Linked List Implementation (cont.)114271283984649192eposition19Linked List Implementation (cont.)114271283984649192eposition20Linked List Implementation (cont.)114271283984649192thise21Linked List Implementation (cont.)void LLElement_insert_before( LLElement *position, LLElement *e){ e->next = position; e->prev = position->prev; if (prev != NULL) position->prev->next = e; position->prev = e;}22Linked List Implementaion (cont.)void LLElement_remove(LLElement *e) removes the linked list element e from the chain.In effect, it links es prev with next.23Linked List Implementation (cont.)114271283984649this24Linked List Implementation (cont.)114271283984649e25Linked List Implementation (cont.)114271283984649e26Linked List Implementation (cont.)114271283984649e27Linked List Implementation (cont.)114271283984649e28Linked List Implementation (cont.)114271283984649e29Linked List Implementation (cont.)void LLElement_remove(LLElement *e){ if (e->prev != NULL) e->prev->next = e->next; if (e->next != NULL) e->next->prev = e->prev; e->prev = NULL; e->next = NULL;}30Linked List Elements Efficiency?Space: O(1)Running TimeInsertBeforeO(1)InsertAfterO(1)RemoveO(1)

31Implementing List with Linked ListTo simplify implementation, we will use two dummy elements to act as the first element and the last element of the list.These two dummies do not hold real data.Elements between them do.32Implementing List with Linked List(cont.)114271283984649???head???tail33Implementing List with Linked List (cont.)typedef struct { LLElement *head, *tail; int size;} LinkedList;

size linked list34Implementing List with Linked List (cont.)void LinkedList_init(LinkedList *list)Initializes a linked list.Create the head element.Create the tail element.Link them together.Set the size to 0.

void LinkedList_init(LinkedList *list){ list->head = LLElement_new(0); list->tail = LLElement_new(0); list->head->next = tail; list->tail->prev = head; list->size = 0;}0head0tail35Implementing List with Linked List (cont.)int LinkedList_get(LinkedList *list, int i){ int j; LLElement *ptr = list->head->next; for(j=0;jnext; return ptr->data;}36Implementing List with Linked List (cont.)void LinkedList_set(LinkedList *list, int i, int x){ int j; LLElement *ptr = list->head->next; for(j=0;jnext; ptr->data = x;}37Implementing List with Linked List (cont.)void LinkedList_find(LinkedList *list, int x){ int result = 0; LLElement *ptr = list->head->next; while (ptr != list->tail && ptr->data != x) { ptr = ptr->next; result++; } if (ptr == tail) return -1; else return result;}

38Implementing List with Linked List (cont.)void LinkedList_insert(LinkedList *list, int i, int x){ LLElement *ptr = list->head; int j; for(j=0;jnext; LLElement_insert_after(ptr, LLElement_new(x)); list->size++;}

39Implementing List with Linked List (cont.)void LinkedList_remove(LinkedList *list, int i){ LLElement *ptr = head->next; int j; for(j=0;jnext; LLElement_remove(ptr); free(ptr); list->size--;}

40Linked List Implementations EfficiencySpace: O(n)Running Time:Get(i)O(i) = O(n)Set(x,i)O(i) = O(n)Find(x)O(n)Insert(x,i)O(i) = O(n)Remove(x,i)O(i) = O(n)41Linked List Implementations Efficiency (cont.)Notice that how you specify the operations can have a lot of impact on the implementations efficiency.We can insert a linked list element into a linked list in O(1) if you know the element just before or after it.But, if we are given the position i to insert, it takes O(i) time just to get there.42Linked List Implementations Efficiency (cont.)How is linked list better than array?The space is O(n) at all time.More efficient use of memory.It performs some operation faster.Insert(x, 0)Remove(0)Both are O(1) in LinkedList, but O(n) in ArrayList.So, a queue implemented by a linked list takes O(1) per operation.

43