1 / 43

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

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.

Télécharger la présentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 418115: การเขียนโปรแกรมโครงสร้างโครงสร้างข้อมูล II ประมุข ขันเงิน

  2. 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. • It may also contain a pointer to the previous linked list element. In this case, we call it a doubly linked list element.

  3. Linked List (cont.) 11 42 7 128 398 4649

  4. Linked List Implementation struct_LLElement { intdata; _LLElement*next,*prev; }; typedefstruct_LLElementLLElement;

  5. Linked List Implementaion (cont.) • LLElement*LLElement_new(intdata) • used to create a new linked list element with the given data. • Usage: LLElement*element=LLElement_new(10);

  6. Linked List Implementation (cont.) LLElement*LLElement_new(intdata) { LLElement*result= (LLElement*)malloc(sizeof(LLElement)); if(result!=NULL) { result->data=data; result->next=NULL; result->prev=NULL; } returnresult; }

  7. Linked List Implementaion (cont.) • voidLLElement_insert_after( LLElement*position,LLElement*e) • inserts a linked list element “e” after “position”.

  8. Linked List Implementation (cont.) 11 42 7 128 398 4649 e 192 this

  9. Linked List Implementation (cont.) 11 42 7 128 398 4649 e 192 this

  10. Linked List Implementation (cont.) 11 42 7 128 398 4649 e 192 this

  11. Linked List Implementation (cont.) 11 42 7 128 398 4649 e 192 this

  12. Linked List Implementation (cont.) 11 42 7 128 398 4649 e 192 this

  13. Linked List Implementation (cont.) 11 42 7 128 398 4649 e 192 this

  14. Linked List Implementation (cont.) voidLLElement_insert_after( LLElement*position,LLElement*e) { e->prev=position; e->next=position->next; if(position->next!=NULL) position->next->prev=e; position->next=e; }

  15. Linked List Implementaion (cont.) • voidLLElement_insert_before(LLElement*position,LLElement*e) • inserts the linked list element “e” before “position”.

  16. Linked List Implementation (cont.) 11 42 7 position 128 398 4649 e 192

  17. Linked List Implementation (cont.) 11 42 7 position 128 398 4649 e 192

  18. Linked List Implementation (cont.) 11 42 7 position 128 398 4649 e 192

  19. Linked List Implementation (cont.) 11 42 7 position 128 398 4649 e 192

  20. Linked List Implementation (cont.) 11 42 7 position 128 398 4649 e 192

  21. Linked List Implementation (cont.) 11 42 7 this 128 398 4649 e 192

  22. Linked List Implementation (cont.) voidLLElement_insert_before( LLElement*position,LLElement*e) { e->next=position; e->prev=position->prev; if(prev!=NULL) position->prev->next=e; position->prev=e; }

  23. Linked List Implementaion (cont.) • voidLLElement_remove(LLElement*e) • removes the linked list element “e” from the chain. • In effect, it links e’s prev with next.

  24. Linked List Implementation (cont.) 11 42 7 128 398 4649 this

  25. Linked List Implementation (cont.) 11 42 7 128 398 4649 e

  26. Linked List Implementation (cont.) 11 42 7 128 398 4649 e

  27. Linked List Implementation (cont.) 11 42 7 128 398 4649 e

  28. Linked List Implementation (cont.) 11 42 7 128 398 4649 e

  29. Linked List Implementation (cont.) 11 42 7 128 398 4649 e

  30. Linked List Implementation (cont.) voidLLElement_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; }

  31. Linked List Element’s Efficiency? • Space: O(1) • Running Time • InsertBefore O(1) • InsertAfter O(1) • Remove O(1)

  32. Implementing List with Linked List • To 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.

  33. Implementing List with Linked List(cont.) head ??? 11 42 7 128 398 4649 tail ???

  34. Implementing List with Linked List (cont.) typedefstruct{ LLElement*head,*tail; intsize; }LinkedList; • size คือจำนวนสมาชิกใน linked list

  35. Implementing List with Linked List (cont.) • voidLinkedList_init(LinkedList*list) • Initializes a linked list. • Create the head element. • Create the tail element. • Link them together. • Set the size to 0. voidLinkedList_init(LinkedList*list) { list->head=LLElement_new(0); list->tail=LLElement_new(0); list->head->next=tail; list->tail->prev=head; list->size=0; } tail head 0 0

  36. Implementing List with Linked List (cont.) intLinkedList_get(LinkedList*list,inti) { intj; LLElement*ptr=list->head->next; for(j=0;j<i;j++) ptr=ptr->next; returnptr->data; }

  37. Implementing List with Linked List (cont.) voidLinkedList_set(LinkedList*list,inti,intx) { intj; LLElement*ptr=list->head->next; for(j=0;j<i;j++) ptr=ptr->next; ptr->data=x; }

  38. Implementing List with Linked List (cont.) voidLinkedList_find(LinkedList*list,intx) { intresult=0; LLElement*ptr=list->head->next; while(ptr!=list->tail&&ptr->data!=x) { ptr=ptr->next; result++; } if(ptr==tail) return-1; else returnresult; }

  39. Implementing List with Linked List (cont.) voidLinkedList_insert(LinkedList*list,inti,intx) { LLElement*ptr=list->head; intj; for(j=0;j<i-1;j++) ptr=ptr->next; LLElement_insert_after(ptr,LLElement_new(x)); list->size++; }

  40. Implementing List with Linked List (cont.) voidLinkedList_remove(LinkedList*list,inti) { LLElement*ptr=head->next; intj; for(j=0;j<i;j++) ptr=ptr->next; LLElement_remove(ptr); free(ptr); list->size--; }

  41. Linked List Implementation’s Efficiency • Space: 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)

  42. Linked List Implementation’s Efficiency (cont.) • Notice that how you specify the operations can have a lot of impact on the implementation’s 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.

  43. Linked List Implementation’s 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.

More Related