cs32 week 2
play

CS32 - Week 2 Umut Oztok July 1, 2016 Umut Oztok CS32 - Week 2 - PowerPoint PPT Presentation

CS32 - Week 2 Umut Oztok July 1, 2016 Umut Oztok CS32 - Week 2 Arrays A basic data structure (commonly used). Organize data in a sequential way. Umut Oztok CS32 - Week 2 Arrays A basic data structure (commonly used). Organize data in a


  1. CS32 - Week 2 Umut Oztok July 1, 2016 Umut Oztok CS32 - Week 2

  2. Arrays A basic data structure (commonly used). Organize data in a sequential way. Umut Oztok CS32 - Week 2

  3. Arrays A basic data structure (commonly used). Organize data in a sequential way. Pros: Umut Oztok CS32 - Week 2

  4. Arrays A basic data structure (commonly used). Organize data in a sequential way. Pros: Easy access to elements, i.e., array[5] gets 6 th element. Easy to debug. Cons: Umut Oztok CS32 - Week 2

  5. Arrays A basic data structure (commonly used). Organize data in a sequential way. Pros: Easy access to elements, i.e., array[5] gets 6 th element. Easy to debug. Cons: Fixed size. Not easy to insert new elements to the front/middle. Umut Oztok CS32 - Week 2

  6. Linked Lists Another data structure. Like arrays, organizes data in a sequential way. In general, linked lists are strong when arrays are weak, and vice versa. Umut Oztok CS32 - Week 2

  7. Linked Lists Another data structure. Like arrays, organizes data in a sequential way. In general, linked lists are strong when arrays are weak, and vice versa. Basic component is a node: *next value Umut Oztok CS32 - Week 2

  8. Linked Lists Another data structure. Like arrays, organizes data in a sequential way. In general, linked lists are strong when arrays are weak, and vice versa. Basic component is a node: *next value Self-referential structures: typedef int DataType; struct Node { DataType value; Node* next; }; Umut Oztok CS32 - Week 2

  9. Linked Lists A linked list is a series of nodes, each pointing to next one. Node* head 1 5 3 Nil Head pointer to point the first element. Last node’s next pointer is NULL. Umut Oztok CS32 - Week 2

  10. Linked Lists Some operations: Insert: To the front. To the end. In the middle. Delete. Search. Traverse. Umut Oztok CS32 - Week 2

  11. Traversal How to traverse the nodes of a linked list? head 1 5 3 Nil Umut Oztok CS32 - Week 2

  12. Traversal How to traverse the nodes of a linked list? head 1 5 3 Nil void print() { Umut Oztok CS32 - Week 2

  13. Traversal How to traverse the nodes of a linked list? head 1 5 3 Nil void print() { Node* cur = head; Umut Oztok CS32 - Week 2

  14. Traversal How to traverse the nodes of a linked list? head 1 5 3 Nil void print() { Node* cur = head; while (cur != nullptr) { Umut Oztok CS32 - Week 2

  15. Traversal How to traverse the nodes of a linked list? head 1 5 3 Nil void print() { Node* cur = head; while (cur != nullptr) { cout << cur->value << " "; Umut Oztok CS32 - Week 2

  16. Traversal How to traverse the nodes of a linked list? head 1 5 3 Nil void print() { Node* cur = head; while (cur != nullptr) { cout << cur->value << " "; cur = cur->next; Umut Oztok CS32 - Week 2

  17. Traversal How to traverse the nodes of a linked list? head 1 5 3 Nil void print() { Node* cur = head; while (cur != nullptr) { cout << cur->value << " "; cur = cur->next; } } Output: 1 5 3 Umut Oztok CS32 - Week 2

  18. Insertion [To the front] How to insert a node with value 6 to the front of the list? From: head 1 5 3 Nil To: head 6 1 5 3 Nil Steps: Umut Oztok CS32 - Week 2

  19. Insertion [To the front] How to insert a node with value 6 to the front of the list? From: head 1 5 3 Nil To: head 6 1 5 3 Nil Steps: Create a new node. Point the node’s next pointer to head. Point head to the node. Umut Oztok CS32 - Week 2

  20. Insertion [To the front] How to insert a node with value 6 to the front of the list? From: head 1 5 3 Nil To: head 6 1 5 3 Nil Steps: Create a new node. Point the node’s next pointer to head. Point head to the node. void insertToFront( int val) { Node* p = new Node; p->value = val; p->next = head: head = p; } Umut Oztok CS32 - Week 2

  21. Insertion [To the end] How to insert a node with value 6 to the end of the list? From: head 1 5 3 Nil To: head 1 5 3 6 Nil Steps: Umut Oztok CS32 - Week 2

  22. Insertion [To the end] How to insert a node with value 6 to the end of the list? From: head 1 5 3 Nil To: head 1 5 3 6 Nil Steps: Create a new node. Go to the end of the list. Point last node’s next pointer to the new node. Umut Oztok CS32 - Week 2

  23. Insertion [To the end] void insertToEnd( int val) { 1 Umut Oztok CS32 - Week 2

  24. Insertion [To the end] void insertToEnd( int val) { 1 Node* p = new Node; 2 p->value = val; 3 p->next = nullptr; 4 5 Node* cur = head; 6 Umut Oztok CS32 - Week 2

  25. Insertion [To the end] void insertToEnd( int val) { 1 Node* p = new Node; 2 p->value = val; 3 p->next = nullptr; 4 5 Node* cur = head; 6 if (cur == nullptr) head = p; 7 else { 8 while (cur->next != nullptr) 9 cur = cur->next; 10 cur->next = p; 11 } 12 } 13 Umut Oztok CS32 - Week 2

  26. Insertion [To the K th slot] How to insert a node with value 6 to the 1 st slot? (Node with value 1 is located at the 0 th slot.) From: head 1 5 3 Nil To: head 1 6 5 3 Nil Steps: Umut Oztok CS32 - Week 2

  27. Insertion [To the K th slot] How to insert a node with value 6 to the 1 st slot? (Node with value 1 is located at the 0 th slot.) From: head 1 5 3 Nil To: head 1 6 5 3 Nil Steps: Find the node N at K − 1 st slot. Create a new node. "Add" the node to K th slot. That is: Make new node’s next pointer point to the node at K th slot. Make N ’s next pointer point to the new node. Umut Oztok CS32 - Week 2

  28. Insertion [To the K th slot] How to insert a node with value 6 to the 1 st slot? (Node with value 1 is located at the 0 th slot.) From: head 1 5 3 Nil To: head 1 6 5 3 Nil Steps: Find the node N at K − 1 st slot. Create a new node. "Add" the node to K th slot. That is: Make new node’s next pointer point to the node at K th slot. Make N ’s next pointer point to the new node. What if K = 0? Umut Oztok CS32 - Week 2

  29. Insertion [To the K th slot] void insertToKth( int val, int index) { 1 Umut Oztok CS32 - Week 2

  30. Insertion [To the K th slot] void insertToKth( int val, int index) { 1 if (head == nullptr || index == 0) insertToFront(val); 2 else { 3 Umut Oztok CS32 - Week 2

  31. Insertion [To the K th slot] void insertToKth( int val, int index) { 1 if (head == nullptr || index == 0) insertToFront(val); 2 else { 3 Node* cur = head; 4 while (cur->next != nullptr) { 5 if (--index == 0) break ; 6 cur = cur->next; 7 } 8 Umut Oztok CS32 - Week 2

  32. Insertion [To the K th slot] void insertToKth( int val, int index) { 1 if (head == nullptr || index == 0) insertToFront(val); 2 else { 3 Node* cur = head; 4 while (cur->next != nullptr) { 5 if (--index == 0) break ; 6 cur = cur->next; 7 } 8 //cur points to either the last element 9 //or (index-1)st element 10 Node* p = new Node; 11 p->value = val; 12 p->next= cur->next; 13 cur->next = p; 14 } 15 } 16 Umut Oztok CS32 - Week 2

  33. Deletion How to delete the first node containing 5? From: head 1 5 3 Nil To: head 1 3 Nil Steps: (Let N denote the node we want to delete.) Umut Oztok CS32 - Week 2

  34. Deletion How to delete the first node containing 5? From: head 1 5 3 Nil To: head 1 3 Nil Steps: (Let N denote the node we want to delete.) Find the node just before N . Have its next pointer point to the node just after N . Delete N . Watch out for special cases. List is empty. List contains a single node. List doesn’t have a node containing the given value. Umut Oztok CS32 - Week 2

  35. Deletion void delete ( int val) { 1 Umut Oztok CS32 - Week 2

  36. Deletion void delete ( int val) { 1 if (head == nullptr) return ; 2 Umut Oztok CS32 - Week 2

  37. Deletion void delete ( int val) { 1 if (head == nullptr) return ; 2 if (head->value == val) { 3 Node* p = head; 4 head = p->next; 5 delete p; 6 } 7 else { 8 Umut Oztok CS32 - Week 2

  38. Deletion void delete ( int val) { 1 if (head == nullptr) return ; 2 if (head->value == val) { 3 Node* p = head; 4 head = p->next; 5 delete p; 6 } 7 else { 8 Node* cur = head; 9 while (cur->next != nullptr) { 10 if (cur->next->value == val) break ; 11 cur = cur->next; 12 } 13 Umut Oztok CS32 - Week 2

  39. Deletion void delete ( int val) { 1 if (head == nullptr) return ; 2 if (head->value == val) { 3 Node* p = head; 4 head = p->next; 5 delete p; 6 } 7 else { 8 Node* cur = head; 9 while (cur->next != nullptr) { 10 if (cur->next->value == val) break ; 11 cur = cur->next; 12 } 13 if (cur->next == nullptr) return ; 14 Node* p = cur->next; 15 cur->next = p->next; 16 delete p; 17 }} 18 Umut Oztok CS32 - Week 2

  40. Exercise 1 Append one linked list to another. Given two linked lists: head 1 5 3 Nil head2 8 4 Nil Append the second one to the first one: head 1 5 3 8 4 Nil Umut Oztok CS32 - Week 2

  41. Exercise 1 Append one linked list to another. Given two linked lists: head 1 5 3 Nil head2 8 4 Nil Append the second one to the first one: head 1 5 3 8 4 Nil Any volunteers? void append(Node* head2) { ... } Umut Oztok CS32 - Week 2

  42. Exercise 1 void append(Node* head2) { Umut Oztok CS32 - Week 2

Recommend


More recommend