Singly Linked Lists 5Peter 524Peter 4 next 6Peter
- Slides: 38
單向鏈結串列 Singly Linked Lists
若要刪除 5號Peter 5節點,只需改了2個地方,4號Peter 4節點的 next指向 6號的Peter 6: 1 2 3 4 5 6 7 8 9 10 name Peter 1 Peter 2 Peter 3 Peter 4 Peter 5 Peter 6 Peter 7 Peter 8 Peter 9 Peter 10 next (指標) 指向 2 號節點 指向 3 號節點 指向 4 號節點 指向 6 號節點 指向 7 號節點 指向 8 號節點 指向 9 號節點 指向 10 號節點 NULL number 接著把5號Peter 5節點釋放掉, 使用Free: number name next (指標) 1 2 3 4 6 7 8 9 10 Peter 1 Peter 2 Peter 3 Peter 4 Peter 6 Peter 7 Peter 8 Peter 9 Peter 10 指向 10 號節點 NULL 指向 2號 指向 3號 指向 4號 指向 6號 指向 7號 指向 8號 指向 9號 節點 節點
單向鏈結串列的反轉 u 如果鏈結串列如下: number 1 2 3 4 5 6 7 8 9 name Peter 1 Peter 2 Peter 3 Peter 4 Peter 5 Peter 6 Peter 7 Peter 8 Peter 9 next (指標) 指向 2 指向 3 指向 4 指向 5 指向 6 指向 7 指向 8 指向 9 NULL 號節點 號節點 u 改成: 1 2 3 4 5 6 7 8 9 name Peter 1 Peter 2 Peter 3 Peter 4 Peter 5 Peter 6 Peter 7 Peter 8 Peter 9 next (指標) NULL 指向 1 指向 2 指向 3 指向 4 指向 5 指向 6 指向 7 指向 8 號節點 號節點 number
鍵結串列的節點走訪 C語言程式 NODE find_node(NODE *head, int num) { NODE *ptr; } ptr = head; while ( ptr != NULL ) { if ( ptr->num == num ) return (ptr); ptr = ptr->next; } return (ptr); /*指向串列起始*/ /*走訪串列*/ /*找尋data*/ /*指向下一節點*/
虛擬碼 NODE *insert_node ( NODE *head, NODE *ptr, int value) { 配置記憶體給new; if (ptr is NULL) 插入第一個節點; else if ( ptr->next = = NULL ) 插入最後一個節點; else 插入成為中間節點; return (head); }
/* 第一種情況: 刪除第一個節點 */ { head = head->next; return(head); /*reset 起始節點指標*/ } else { previous = head; while ( previous->next != ptr ) /*找節點ptr的前節點*/ previous = previous->next; if ( ptr->next == NULL ) /*是否是串列結束*/
假設兩個多項式 與 u 若以單向鏈結串列方式呈現 則其C語言程式如下: u 相加 void poly_add(struct node *eq_hl, struct node *eq_h 2, struct node *ans_h, struct node *ptr) { struct poly *this_nl, *this_n 2, *prev; this_n 1 = eq_h 1; this_n 2 = eq_h 2; prev = NULL;
while(this_n 1 != NULL || this_n 2 != NULL) { /*當兩個多項式皆相加完則結束*/ ptr = (struct poly*) malloc(sizeof(struct poly)); ptr ->next = NULL; //第一個多項式指數大於第二個多項式 if (this_n 1 != NULL&& (this_n 2 == NULL | | this_n 1 ->exp > this_n 2 ->exp)) { ptr->coef = this_n 1 ->coef; ptr->exp = this_n 1 ->exp; this_n 1 = this_n 1 ->next; }
//第一個多項式指數大於第二個多項式 else { if (this_n 1 != NULL&& (this_n 2 == NULL | | this_n 1 ->exp < this_n 2 ->exp)) { ptr->coef = this_n 2 ->coef; ptr->exp = this_n 2 ->exp; this_n 2 = this_n 2 ->next; } }
//兩個多項式指數相等,進行相加 else { ptr->coef = this_n 1 ->coef; ptr->exp = this_n 1 ->exp; this_n 1 = this_n 1 ->next; ptr->coef = this_n 1 ->coef; + this_n 2 ->coef; ptr->exp = this_n 1 ->exp; if (this_n 1 != NULL) this_n 1 = this_n 1 ->next; if (this_n 1 != NULL) this_n 2 = this_n 2 ->next; } if (ptr->coef != 0) { if (ans_h = = NULL) ans_h = ptr; else prev -> next = ptr; prev = ptr; } else free (ptr); } }
- Singly linked list vs doubly linked list
- Introduction to linked list
- Insertion in linked list algorithm
- Singly linked list
- Java data structures
- Representation of polynomial using linked list
- Singly vs doubly linked list
- Disadvantages of doubly linked list
- X.next = x.next.next
- Pengertian single linked list
- Singly reinforced beam design
- Difference between singly and doubly reinforced beam
- Blockly lists
- Wish lists year
- Political lists new
- Blind sort words their way
- Clwords
- Ow sound
- Ei wirds
- Python list methods
- Lists of tuples python
- "new bookmarking lists 2018"
- Read wrote inc
- Soft c words
- Ucl library reading lists
- Mexica tribute lists ap world history
- Qri word lists
- Cons in lisp
- Semi colon vs colon
- Https://diigo.com
- Prolog head tail
- Edinburgh resource lists
- Strengths lists
- Words without vowels
- Wish lists year
- Java types of lists
- Lesson 3 lists practice
- Political wish lists
- Parallel structure in lists