Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)

Similar presentations


Presentation on theme: "CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)"— Presentation transcript:

1 CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)

2 Reminder & Agenda Discussion Slides http://cadlab.cs.ucla.edu/~cody/cs32/ Linked List Doubly Linked List 1 st Midterm next Thursday Project 2 due next Tuesday 9:00 p.m.

3 Linked Lists Component Node value can be any kind of data variables/structures next points to either the next Node or null Example value*next typedef int ItemType; struct Node { ItemType value; Node *next; };

4 Linked Lists Single linked lists Are we done? value*nextvalue*nextvalue*next null NO!!

5 Linked Lists You have to know where it begins We call it head pointer Node *head; value*nextvalue*nextvalue*next null

6 Linked Lists Check list A description of a node with a next pointer A head pointer points to the first node The list must be loop-free Unless it is a circularly linked list

7 Linked Lists Operations Search Insert Remove Sanity Check!!

8 Linked Lists (Search) Simply traverse the linked list 1*next12*next LinkedList::search(int expect) { Node *currNode = head; if (currNode == null) return null; while (currNode->next != null) { if (currNode->value == expect) return currNode; currNode = currNode->next; } return null; } null

9 Linked Lists (Insert) 1.New a node 12*next null Node *newNode = new Node; newNode->value = 12; newNode->next = null;

10 Linked Lists (Insert) 1.New a node 2.Fine the tail 1*next4 Node *currNode = head; while (currNode->next != null) currNode = currNode->next; null

11 Linked Lists (Insert) 1.New a node 2.Fine the tail 3.Link the new node and the tail 12*next null 1*next4 currNode->next = newNode;

12 Linked Lists (Insert) LinkedList::insert(int v) { Node *newNode = new Node; newNode->value = v; newNode->next = null; Node *currNode = head; if (currNode == null) { // Empty list head = newNode; return ; } while (currNode->next != null) currNode = currNode->next; currNode->next = newNode; } Are we done?NO!! What if head is null ?

13 Linked Lists (Advance Insert) Now we want to build a SORTED linked list This function still works? No, we may insert a new node in any place! 12*next null 1*next4 LinkedList::insert(int v) { Node *newNode = new Node; newNode->value = v; newNode->next = null; Node *currNode = head; if (currNode == null) { // Empty list head = newNode; return ; } while (currNode->next != null) currNode = currNode->next; currNode->next = newNode; }

14 Linked Lists (Advance Insert) 1.Find the right position 12*next null 1*next Node *currNode = head; Node *nextNode = null; while (currNode->next != null) { nextNode = currNode->next; if (currNode->value value > v) break; currNode = currNode->next; }

15 Linked Lists (Advance Insert) 2. New a node and link pointers Node *currNode = head; Node *nextNode = null; while (currNode->next != null) { nextNode = currNode->next; if (currNode->value value > v) break; currNode = currNode->next; } 12*next null 1*next4 Node *newNode = new Node; newNode->value = v; newNode->next = currNode->next = currNode->next; newNode;

16 Linked Lists (Advance Insert) Node *newNode = new Node; newNode->value = v; Node *currNode = head; Node *nextNode = null; if (currNode == null) { // Empty list head = newNode; return ; } if (currNode->value > v) { // Update head newNode->next = head; head = newNode; return ; } while (currNode->next != null) { nextNode = currNode->next; if (currNode->value value > v) break; currNode = currNode->next; } newNode->next = currNode->next; currNode->next = newNode; return ; Are we done?NO!! What if head is null ? What if we want to insert the smallest value (head)? What if we want to insert the largest value (tail)?

17 Linked Lists (Remove) Similar to the advance insert 1.Find the right position 2.Fix pointers 3.Delete the node 12*next null 1*next4 Node *prevNode = // Find the previous node Node *currNode = // Find the node prevNode->next = currNode->next; delete currNode; Previous nodeCurrent node

18 Linked Lists (Remove) LinkedList::remove(int v) { Node *prevNode = null; Node *currNode = head; if (currNode == null) // Empty list return ; while (currNode->next != null) { if (currNode->value == v) break; prevNode = currNode; currNode = currNode->next; } if (currNode->next == null) { if (currNode->value != v) // Not found return ; else // Delete tail prevNode->next = null; } else if (prevNode == null) // Delete head head = currNode->next; else // Delete middle prevNode->next = currNode->next; delete currNode; return ; } Are we done?NO!! What if head is null ? What if we want to delete the tail? What if we want to delete the head? What if we cannot find the node to be deleted?

19 Discussion: Linked Lists and Arrays Linked Lists Pros Memory efficiency Easy to remove nodes Easy to insert nodes in the middle Array Pros Quickly search Easy to be implemented

20 Variations Sorted Linked List Changed insertion method Doubly Linked List Each node has prev and next pointers A tail pointer is kept to point to the last node Circularly Linked List The tail node points to the first node Essentially there is no first node

21 Doubly Linked Lists Create links in reversed order Discussion: Why you want to use doubly linked list? Search:  Insert: Maybe Remove: 141215

22 Doubly Linked Lists (Search) Do you really need to do anything else especially for doubly linked list? NO!

23 Doubly Linked Lists (Insert) 1.Find the position Always tail (unsorted) or specific (sorted) 2.New a node and link pointers Note: Order! 1 4 12158 1.newNode->next = 2.newNode->prev = 3. currNode->next = 4. currNode->next->prev = One possible order (not only): 1  2  4  3 currNode->next; currNode; newNode;

24 Doubly Linked Lists (Insert) Discussion: Other cases? Insert to empty Insert to head Insert to tail

25 Doubly Linked Lists (Remove) LinkedList::remove(int v) { Node *prevNode = null; Node *currNode = head; if (currNode == null) // Empty list return ; while (currNode->next != null) { if (currNode->value == v) break; prevNode = currNode; currNode = currNode->next; } if (currNode->next == null) { if (currNode->value != v) // Not found return ; else // Delete tail prevNode->next = null; } else if (prevNode == null) // Delete head head = currNode->next; else // Delete middle prevNode->next = currNode->next; delete currNode; return ; } Can you modify this function? currNode->prev->next = null; currNode->prev->next = currNode->next;

26 1 st Midterm Constructor/Deconstructor Initialization ordering Copy constructor Operator overloading Array  Linked list Implement a linked list Any kind of linked lists Make sure you understand EVERY components! Stack and queue Make sure you pay attention to all lectures on next week!


Download ppt "CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)"

Similar presentations


Ads by Google