Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Summary. Exercise – Remove Duplicates Download remove_duplicates_ex.c and implement the function void remove_duplicates(Node *head); remove_duplicates()

Similar presentations


Presentation on theme: "Programming Summary. Exercise – Remove Duplicates Download remove_duplicates_ex.c and implement the function void remove_duplicates(Node *head); remove_duplicates()"— Presentation transcript:

1 Programming Summary

2 Exercise – Remove Duplicates Download remove_duplicates_ex.c and implement the function void remove_duplicates(Node *head); remove_duplicates() accepts a list sorted in ascending order and deletes duplicate nodes from the list, leaving a single node for every value. Ideally, the list should only be traversed once.

3 Clues in the Question remove_duplicates does not return a value – what does it mean? Why do we care that the list is sorted? Does the sorting order makes a difference?

4 Solution void remove_duplicates(Node* head) { Node *current = head, *tmp = NULL; /* do nothing if the list is empty */ if (current == NULL) return; /* Compare current node with next node */ while (current->next != NULL) { if (current->data == current->next->data) { tmp = current->next; current->next = current->next->next; free(tmp); } else { /* only advance if no deletion */ current = current->next; }

5 Sample Theoretical Question What’s printed on the screen when the following program is run?  change_val.c And what do these function do? What’s missing and how will you fix it?  secret1.c  secret2.c

6 Parentheses Checking Implement a function such that –  Input: string s that contains (among other things) parentheses  Output: 0 if the parentheses’ nesting is illegal, non- zero otherwise  For example – (()())() is legal, while ())() and (()(()) are not. Write a program that accepts a string from the user and checks the parentheses.

7 Palindrome A palindrome is string that reads the same backward or forward. For example: amanaplanacanalpanama (A man, a plan, a canal, Panama!( Write a recursive function that checks if a string is a palindrome. Write a program that reads a string from the user and checks if it’s a palindrome.

8 Max Distance Write a structure of a point in 2D space and a distance function between two points Write a program that gets a series of points from the user and outputs the largest distance between a pair of points The number of points is given by the user before the points are read in The distance between points [x1, y1] and [x2, y2] is Solution – max_distance.c

9 Max Distance Write a structure of a point in 2D space and a distance function between two points Write a program that gets a series of points from the user and outputs the largest distance between a pair of points and the points themselves The number of points is given by the user before the points are read in Solution – max_distance2.c, max_distance3.c

10 Is In Circle? Define two structures – a point and a circle Implement a function is_in_circle that accepts a point and a circle, and returns 1 if the point is in the circle, 0 otherwise Write a program that accepts n circles from the user and one point, and outputs the number of circles containing that point  n is user input!

11 Split List Implement a linked list where each item simply contains an integer Input a number n from the user, and split the original list into two lists such that the first contains all the elements smaller than n, and the other contains all the others Display both linked lists on the screen

12 Functions To Remember stdio.h  printf, scanf, putchar, getchar string.h  strlen, strcpy, strcat, strcmp, strchr, strstr ctype.h  tolower, toupper, islower, isupper, … stdlib.h  atof, atoi, malloc, free, exit

13 Solving A Problem Read the entire question, beginning to end, then read it again. Make sure you understand the requirements. Break down the problem into logical steps  how do I get the input?  what kind of processing is needed?  what about the output? Write your solution in a clear and concise manner (make sure you cover all your bases) “compile and run”

14 The No. 1 Rule Your grader is human, treat him as such!

15


Download ppt "Programming Summary. Exercise – Remove Duplicates Download remove_duplicates_ex.c and implement the function void remove_duplicates(Node *head); remove_duplicates()"

Similar presentations


Ads by Google