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

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Pseudocode A way to make programming easier Start with a verbal description of what the program is supposed to do! Slowly transform it into C, defining.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Sample Theoretical Question What’s printed on the screen when the following programs are run? printing.c change_val.c And what does this function do? secret.c.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Chapter 9 Character Strings
Introduction to C Programming CE Lecture 20 Insertion and Deletion with Linear Linked Lists.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
PRACTICAL DATA STRUCTURES USING C/C++ Chapter 2 Strings Tel: ext.3242 彰師大 數學系 蔡政容 (Cheng-Jung Tsai)
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Characters and Strings File Processing Exercise C Programming:Part 3.
Today’s Agenda  Reminder: HW #1 Due next class  Quick Review  Input Space Partitioning Software Testing and Maintenance 1.
Intro to C Part 3: © Walter Milner 2005: Slide 1 Introduction to ANSI C Part Three  Strings  Strings in structs  File handling  Linked list example.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
C/C++ Basics. Basic Concepts Basic functions of each language: Input, output, math, decision, repetition Types of errors: Syntax errors, logic errors,
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
ALGORITHMS.
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
 Last Lecture Reminder  Array Sorts  Exam Questions Examples  Open Questions ◦ Search Engines ◦ Operating Systems ◦ Multiple Files in Projects  Additional.
LINKED LISTS.
CSE 220 – C Programming malloc, calloc, realloc.
C Characters and Strings
CSCE 3110 Data Structures & Algorithm Analysis
Linked List :: Basic Concepts
C Characters and Strings
Programming Languages and Paradigms
Pointers Department of Computer Science-BGU יום שלישי 31 יולי 2018.
A First Book of ANSI C Fourth Edition
CSE 303 Lecture 14 Strings in C
Programming Paradigms
Exercises on String Operations
Linked List Sudeshna Sarkar.
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
slides adapted from Marty Stepp and Hélène Martin
Recursion Chapter 11.
Linked List.
slides created by Marty Stepp
Exercise Arrays.
Strings #include <stdio.h>
Tree.
Programming Languages and Paradigms
Programming Strings.
slides adapted from Marty Stepp
Characters and Strings Functions
C Characters and Strings
slides created by Marty Stepp
Presentation transcript:

Programming Summary

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.

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?

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; }

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

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.

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.

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

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

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!

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

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

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”

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