1 / 17

資料結構 (Data Structure) Ch 04a: Linked List

資料結構 (Data Structure) Ch 04a: Linked List. 楊 吳 泉. 資訊工程學系副教授 http://www.twcrypt.org wcyang@isu.edu.tw 2007.09~2008.01. Outline. Singly Linked Lists and Chains Representing Chains in C Linked Stacks and Queues Polynomials Additional List Operations Equivalence Classes Sparse Matrices

lis
Download Presentation

資料結構 (Data Structure) Ch 04a: Linked List

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. 資料結構 (Data Structure)Ch 04a: Linked List 楊 吳 泉 資訊工程學系副教授 http://www.twcrypt.org wcyang@isu.edu.tw 2007.09~2008.01

  2. Outline • Singly Linked Lists and Chains • Representing Chains in C • Linked Stacks and Queues • Polynomials • Additional List Operations • Equivalence Classes • Sparse Matrices • Doubly Linked Lists

  3. WAT EAT CAT BAT First NULL 1. Singly Linked Lists and Chains • Linked List • consists of a sequence of nodes • each node containing arbitrary data fields and one (or/and more) links pointing to the next (and/or other) nodes. • Disadvantages of arrays • Data movements during insertion and deletion • Waste space in storing n ordered lists of varying size • Possible solution to solve the disadvantages of arrays • Usual way to draw a linked list

  4. Basic operations of List • Create • Create a new list. • Insert • Insert a node after a specific node • Delete • Delete a node after a specific node

  5. A A A B C C C D E D D Example – List using Array • Create • e.g. create a four-node list • Delete • e.g. delete the node after B • Insert • e.g. Insert node E after C

  6. A B C D A C D A C D E Example – List using Links • Create • e.g. create a four-node list • Delete • e.g. delete the node after B • Insert • e.g. Insert node E after C

  7. 2. Representing Chains in C • Representing chains in C • Self-referenced structure • Dynamic memory allocation: • C: malloc, free • C++: new, delete • Example • typedef struct listNode *listPointer;typedef struct listNode { char data[4]; listPointer link;} typedef struct listNode { char data[4]; struct listNode *link;}

  8. listPointer 10 struct listNode Example 4-1: Data structure • typedef struct listNode *listPointer; • typedef struct listNode { • int data; • listPointer link; • };

  9. p NULL 20 10 q Example 4-1: Create a new linked list • listPointer create() { • listPointer p,q; • p = (listPointer) malloc(sizeof(struct listNode)); • p->data = 10; • q = (listPointer) malloc(sizeof(struct listNode)); • q->data = 20; • p->link = q; • q->link = NULL; • return p; • }

  10. 10 x p NULL tmp 30 20 Insertion tmp->link = x->link; x->link =tmp;

  11. Example 4-1: Insert data after x • void insert(listPointer *p, listPointer x, int num) { • listPointer tmp; • tmp = (listPointer) malloc(sizeof(struct listNode)); • tmp->data = num; • if(*p!=NULL) { • tmp->link = x->link; • x->link = tmp; • } • else { /*insert num into a null list */ • tmp->link = NULL; • *p = tmp; • } • }

  12. 10 20 Deletion y x p NULL x->link = y->link; free(y);

  13. Example 4-1: Delete data after x • void delete(listPointer *p, listPointer x, listPointer y) { • if(x != NULL) • x->link = y->link; • else /* delete the first element*/ • *p = (*p)->link; • free(y); • } Q: Why does the program use two pointer x and y to delete y?

  14. Example 4-1: Print list • void printList(listPointer ptr) { • printf("\n "); • while(ptr!=NULL) { • printf("%d --> ",ptr->data); • ptr = ptr->link; • } • printf("NULL."); • }

  15. Example 4-1: Test Program I • Scenario • Create a new list with 2 nodes:10,20 • 10 --> 20 --> NULL. • Insert 30 after 20 • 10 --> 20 --> 30 --> NULL. • Insert 40 after 30 • 10 --> 20 --> 30 --> 40 --> NULL. • Delete 20 after 10 • 10 --> 30 --> 40 --> NULL. • Delete the first element 10 • 30 --> 40 --> NULL.

  16. Example 4-1: Test Program II • listPointer ptr = NULL,temp;ptr = create(); printList(ptr); • insert(&ptr,ptr->link,30); printList(ptr); • insert(&ptr,ptr->link->link,40);printList(ptr); • delete(&ptr,ptr,ptr->link); printList(ptr); • delete(&ptr,NULL,ptr); printList(ptr);

  17. Practice • Scenario • Create a new list with 3 nodes:10,20,30 • Insert 25 after 20 • Delete 20 after 10 • Insert 35 after 30 • Delete the first element 10 • Insert 15 at the first element of the list

More Related