170 likes | 434 Views
資料結構 (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
E N D
資料結構 (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 • Doubly Linked Lists
… 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
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
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
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
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;}
listPointer 10 struct listNode Example 4-1: Data structure • typedef struct listNode *listPointer; • typedef struct listNode { • int data; • listPointer link; • };
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 x p NULL tmp 30 20 Insertion tmp->link = x->link; x->link =tmp;
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; • } • }
10 20 Deletion y x p NULL x->link = y->link; free(y);
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?
Example 4-1: Print list • void printList(listPointer ptr) { • printf("\n "); • while(ptr!=NULL) { • printf("%d --> ",ptr->data); • ptr = ptr->link; • } • printf("NULL."); • }
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.
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);
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