Presentation is loading. Please wait.

Presentation is loading. Please wait.

TUTORIAL 11 CS 137 F18 November 27th.

Similar presentations


Presentation on theme: "TUTORIAL 11 CS 137 F18 November 27th."— Presentation transcript:

1 TUTORIAL 11 CS 137 F18 November 27th

2 Overview a8 Common Mistakes sum_to_zero.c using qsort
multipleChoice.c solutions a9 Progress rotate_ll.c loop_ll.c

3 a8 Common Mistakes a8p1: MultipleChoice.c a8p2: insert_or_merge.c
Very Well Done! a8p2: insert_or_merge.c Lots of ways to go about it Needed to compare the entire array for a merge step a8p3: sum_to_zero.c Dealing with the array of size 2 Finding the sum closest to zero in O(n)

4 Recall qsort In practice, one usually does not create their own sorting algorithm In C, <stdlib.h> contains qsort: void qsort ( void * base , size_t n , size_t , size , int (* compare )( const void *a , const void * b )); base is the beginning of the array n is the length of the array size is the size of a byte in the array *compare is a function pointer to a comparison criteria How can it be implemented in a8p3? void sum_closest_to_zero(int a[], int n, int *first, int *second);

5 Solution to multipleChoice.c Question 1
int question_1(int n) { int s = n; for(int i = ; i > 0; i /= 2) printf("Fun!"); return s; }

6 Solution to multipleChoice.c Question 2
int question_2(int n) { int s = 0; for(int i = 0; i < n; i++) for(int j = i/2; j < n; j++) s++; return s; }

7 Solution to multipleChoice.c Question 3
nt question_3(int n) { int s = 0; for(int i = n*n; i>0; i /= n) s++; return s; }

8 Solution to multipleChoice.c Question 4
int q4_q5_helper(int n) { int s = 0; for(int i = 0; i < n; i++) s++; return s; } int question_4(int n){ int t = 0; for(int i = 0; i < n; i++) for(int j = 0; j < q4_q5_helper(n); j++) t++; return t;

9 Solution to multipleChoice.c Question 5
int q4_q5_helper(int n) { int s = 0; for(int i = 0; i < n; i++) s++; return s; } int question_5(int n) { int t = 0; for(int i = 0; i < q4_q5_helper(n); i++) for(int j = 0; j < n; j++) t++; return t;

10

11 a9 Progress? Questions? Avoid Memory Leaks Boolean Expressions
Care in keeping track of nodes Care in traversing nodes Boolean Expressions For converting a linked list to a double linked list, make sure you are not mutating the linked list DO NOT use lst->head to traverse the linked list. Only destroy and remove_item should free any structure on the heap.

12 rotate_ll.c Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Can assume k is a non-negative integer. void rotate(struct ll *lst, int k); Example Linked List: 1->2->3->4->5->6->7 Rotated Linked List by k = 4: 5->6->7->1->2->3->4

13 loop_ll.c Given a linked list, check if the linked list has loop or not. bool detectLoop(struct ll *lst);

14 loop_ll.c continued Given a linked list with a loop, remove the loop.
void removeLoop(struct ll *lst, struct llnode *loop_node); For more efficient implementations

15 return 0; }


Download ppt "TUTORIAL 11 CS 137 F18 November 27th."

Similar presentations


Ads by Google