Fundamentals of Programming

Similar documents
Introduction to the C programming language

Introduction to the C programming language

Fundamentals of Programming

Introduction to the C programming language

Introduction to the C programming language

Informatica e Sistemi in Tempo Reale

Linked Lists in C and C++

CSCE 2014 Final Exam Spring Version A

Fundamentals of Programming

Object Oriented Software Design

Object Oriented Software Design II

ECE 122. Engineering Problem Solving Using Java

CS107, Lecture 9 C Generics Function Pointers

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Advanced Pointer Topics

CS300 Final Review Questions 1

ECE264 Spring 2014 Exam 2, March 11, 2014

CS 610: Intermediate Programming: C/C++ Making Programs General An Introduction to Linked Lists

Data Structures and Algorithms for Engineers

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Kurt Schmidt. October 30, 2018

Object Oriented Software Design - II

Object Oriented Software Design

Fundamentals of Programming

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Linked List. April 2, 2007 Programming and Data Structure 1

a data type is Types

Procedural programming with C

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Design Patterns in C++

CS201 Some Important Definitions

Lecture Notes on Tries

Programming in C. Lecture Tiina Niklander. Faculty of Science

6. Pointers, Structs, and Arrays. 1. Juli 2011

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Design Patterns in C++

Informatica e Sistemi in Tempo Reale

Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal

Overview of today s lecture. Quick recap of previous C lectures. Introduction to C programming, lecture 2. Abstract data type - Stack example

Contents of Lecture 3

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Heap Arrays. Steven R. Bagley

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

Data Structure Series

About this exam review

Introduction to Computer Science Midterm 3 Fall, Points

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Design Patterns in C++

Advanced C Programming and Introduction to Data Structures

C-types: basic & constructed. C basic types: int, char, float, C constructed types: pointer, array, struct

Dynamic Data Structures. CSCI 112: Programming in C

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Algorithms, Data Structures, and Problem Solving

Week 3: Pointers (Part 2)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Generic Programming in C

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

Elementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Lecture Notes on Priority Queues

First of all, it is a variable, just like other variables you studied

Fun facts about recursion

Understanding Pointers

Data Representation and Storage. Some definitions (in C)

Design Patterns in C++

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

Introduction to the C programming language

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Heap Arrays and Linked Lists. Steven R. Bagley

Pointers in C/C++ 1 Memory Addresses 2

C Structures & Dynamic Memory Management

Pointers, Dynamic Data, and Reference Types

Quiz 0 Review Session. October 13th, 2014

Pointers, Arrays and Parameters

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

The time and space are the two measure for efficiency of an algorithm.

Priority queues. Priority queues. Priority queue operations

The University Of Michigan. EECS402 Lecture 07. Andrew M. Morgan. Sorting Arrays. Element Order Of Arrays

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

CSCI 171 Chapter Outlines

Low-Level C Programming. Memory map Pointers Arrays Structures

Intermediate Programming, Spring 2017*

Final CSE 131B Spring 2004

G Programming Languages - Fall 2012

OUTLINE. Review Functions Pointers Function calls Array Pointers Pointer Arrays Data Structures and dynamic Memory Function Pointers quicksort example

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Short Notes of CS201

Algorithms & Data Structures

(Section : Computer Science)

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

CS 61c: Great Ideas in Computer Architecture

MPATE-GE 2618: C Programming for Music Technology. Syllabus

ADT: Design & Implementation

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

MIDTERM EXAM. CS 217 October 28, Name: Precept: Honor Code: Score: Problem Score Max

Transcription:

Fundamentals of Programming Data structures: Lists Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa March 21, 2012 G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 1 / 33

Outline 1 Data structures 2 Sorting and searching Interlude: pointer to functions Searching 3 Lists G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 2 / 33

Outline 1 Data structures 2 Sorting and searching Interlude: pointer to functions Searching 3 Lists G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 3 / 33

Using arrays The C language provides two simple primitive data structures: arrays and structures Structures are for grouping different data relevant to a single object (e.g. a student, a complex number, a bank account, etc.) Arrays are for storing multiple instances of the same data (e.g. an array of integers, of students, of bank accounts, etc.) Both are treated statically: When declaring an array, the size of the array must be a constant known at compile time, because the compiler must compute how much memory to allocate for the array If we do not know the size at compile time, we have to resort to an array created dynamically with malloc G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 4 / 33

Address book Suppose we want to implement an address book Each entry in the book will contain information about a person s name, address, telephone, etc. typedef struct abook_entry { char name[50]; char address[100]; char telephone[20]; } ABOOK_ENTRY; G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 5 / 33

How to store addresses To store a set of addresses, we could prepare an array with a maximum number of entries typedef struct address_book { ABOOK_ENTRY entries[100]; int num; } ABOOK; void abook_init(abook *book); void abook_insert(abook *book, ABOOK_ENTRY *e); ABOOK_ENTRY abook_search(abook *book, char *name); void abook_print(abook *book); See complete code at./examples/07.lists-examples/addressmain.c G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 6 / 33

Problems The problem of this approach is that we don t know how many addresses we will need to store If we need more than 100, then the program fails If we need much less, then we are wasting memory A better approach is to re-size the array depending on the number of elements Also, most code can be generalised G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 7 / 33

Vector of pointers Let us define a generic vector of pointers typedef struct vector_ptr { void **array; int num_elem; int array_size; } VECTOR_PTR; void vptr_init(vector_ptr *v); void vptr_push_back(vector_ptr *v, void *elem); void * vptr_pop_back(vector_ptr *v); void * vptr_elem_at(vector_ptr *v, int i); int vptr_mem_size(vector_ptr *v); int vptr_num_elem(vector_ptr *v); Pointer to pointer to void! Array is a pointer to an array of pointers to void G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 8 / 33

Interface Notice how we define an interface, and we access the data structure only through functions we say that vector_ptr is an opaque data structure, because the user should access the structure only through functions and never directly access the data fields G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 9 / 33

Enlarging vector.c static void vptr_enlarge(vector_ptr *v) { if (v->array_size == 0) vptr_init(v); else v->array_size *= 2; void **tmp = malloc(v->array_size * sizeof(void *)); memcpy(tmp, v->array, v->num_elem * sizeof(void *)); free(v->array); v->array = tmp; return; } void vptr_push_back(vector_ptr *v, void *elem) { if (v->num_elem == v->array_size) vptr_enlarge(v); v->array[v->num_elem++] = elem; return; } void * vptr_pop_back(vector_ptr *v) { if (v->num_elem == 0) return 0; else return v->array[--v->num_elem]; } G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 10 / 33

Exercises An example of usage of the data structure can be found in./examples/07.lists-examples/vector-ptr-main.c As an exercise, create a similar data structure by storing copies of objects instead of pointers Advantage: we can copy an entire data structure; we can operate on copies without modifying the original G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 11 / 33

Outline 1 Data structures 2 Sorting and searching Interlude: pointer to functions Searching 3 Lists G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 12 / 33

Searching Searching the data structure takes linear time in the number of elements. We could improve is we keep the data structure sorted according to field on which we want to search If we want to search by name, we should order alphabetically by name Then, we apply binary search start looking in the middle If we have found the entry, finish with success If the entry is greater than the one we look for, continue looking in the first half If the entry is less than the one we look for, continue looking in the second half G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 13 / 33

Sorting There are many algorithms for sorting Insertion, Selection, Bubble, Shell, Merge, Heap, Quicksort, etc. A good summary is here http://www.sorting-algorithms.com/ The most popular is quicksort, a very good compromise in efficiency in many different cases G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 14 / 33

Exercise Implement the quicksort and bubblesort algorithms for integers Compare their performance on randomly generated arrays G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 15 / 33

Generic sorting Now suppose we want to implement an address book using the vector of pointers we just provided We also want to sort the array, and apply binary search We could write our own sorting algorithm (e.g. quicksort) However, the sorting algorithm is always the same; we don t want to rewrite it from scratch every time we need to sort something therefore, the standard c library provides a quicksort algorithm already implemented void qsort (void *array, size_t count, size_t size, cmp_fn_t compare); where cmp_fn_t is the type of the compare function int cmp_fn_t (const void *, const void *); G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 16 / 33

Outline 1 Data structures 2 Sorting and searching Interlude: pointer to functions Searching 3 Lists G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 17 / 33

Pointers to functions In C it is possible to define a pointer to a function The syntax is a little strange, pay attention: void (*pf1)(int); int (*pf2)(double, double); char* (*pf3)(char*); Variable pf1 is a pointer to a function that takes an integer and returns nothing (void) G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 18 / 33

Pointers to functions In C it is possible to define a pointer to a function The syntax is a little strange, pay attention: void (*pf1)(int); int (*pf2)(double, double); char* (*pf3)(char*); Variable pf1 is a pointer to a function that takes an integer and returns nothing (void) Variable pf2 is a pointer to a function that takes two doubles and returns an integer G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 18 / 33

Pointers to functions In C it is possible to define a pointer to a function The syntax is a little strange, pay attention: void (*pf1)(int); int (*pf2)(double, double); char* (*pf3)(char*); Variable pf1 is a pointer to a function that takes an integer and returns nothing (void) Variable pf2 is a pointer to a function that takes two doubles and returns an integer Variable pf3 is a pointer to a function that takes a pointer to char and returns a pointer to char G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 18 / 33

Other examples How to use pointers to functions typedef int (cmp_fn_t)(void *, void *); int myfun(void *a, void *b); cmp_fn_t pf = myfun; int x, y; pf(&x, &y); Defines cmp_fn_t as the type of pointer to function that takes two pointers to void and returns an integer G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 19 / 33

Other examples How to use pointers to functions typedef int (cmp_fn_t)(void *, void *); int myfun(void *a, void *b); cmp_fn_t pf = myfun; int x, y; pf(&x, &y); Defines cmp_fn_t as the type of pointer to function that takes two pointers to void and returns an integer pf is a variable that points to myfun G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 19 / 33

Other examples How to use pointers to functions typedef int (cmp_fn_t)(void *, void *); int myfun(void *a, void *b); cmp_fn_t pf = myfun; int x, y; pf(&x, &y); Defines cmp_fn_t as the type of pointer to function that takes two pointers to void and returns an integer pf is a variable that points to myfun calls myfun by passing the address of x and y G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 19 / 33

Using qsort Using qsort with integers: int cmp_int(const void *x, const void *y) { return *((int *)x) > *((int *)y); } int main() { int arrayint[] = {4, 5, 6, 1, 2, 3, 0, 9, 7, 8};... qsort(arrayint, 10, sizeof(int), cmp_int);... } G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 20 / 33

Using qsort with strings Using qsort with array of strings int cmp_str(const void *x, const void *y) { const char **p = (const char **)x; const char **q = (const char **)y; } return strcmp(*p, *q); int main() { char *array[] = {"ABC", "ZGF", "HLK", "SDF", "PLM", "BSD", "KKK", "JFL", "VMZ", "CDA"};... qsort(array, 10, sizeof(char *), cmp_str);... } G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 21 / 33

Outline 1 Data structures 2 Sorting and searching Interlude: pointer to functions Searching 3 Lists G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 22 / 33

Binary search Also binary search is a well-established algorithm, so it can be generalised void * bsearch (const void *key, const void *array, size_t count, size_t size, comparison_fn_t compare); key is the pointer to the element to search The comparison function should return -1, 0 or 1 if the key is less than, equal to or greater than the element in the array You have to be particularly careful with strings (as always) see./examples/07.lists-examples/stringsort.c G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 23 / 33

Exercise Continue the addressbook exercise Implement sorting and searching by name Also implement sorting and searching by address using a second vector of pointers If you use a second vector, how it is possible to perform addition and removal of elements? (assume unique keys) G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 24 / 33

Outline 1 Data structures 2 Sorting and searching Interlude: pointer to functions Searching 3 Lists G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 25 / 33

Lists With vector we can efficiently search and sort However, there are many cases where other data structures are more efficient for example, when we have frequent additions and deletions of elements in the middle Adding an element and then sort has complexity O(n 2 ) or O(n log n) Removing an element has always O(n) Searching has O(log n) (if ordered) The list data structure can have some advantages over vector sometimes G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 26 / 33

Ordered Insertion Problem: suppose we have an ordered array of integers, from smalles to largest Suppose that we need to insert another number, and that after insertion the array must still be ordered Solution 1: Insert at the end, then run a sorting algorithm (i.e. insert sort or bubble sort) Solution 2: Identify where the number has to be inserted, and move all successive numbers one position forth Both solutions require additional effort to maintain the data structured ordered Another solution is to have completely different data structure G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 27 / 33

Lists A linked list is a collection of data structures, each one contains a pointer to the next one head 3 5 9 10 null Every element of the list contains the data (in this case an integer), and a pointer to the following element in the list G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 28 / 33

List interface list.h #ifndef LIST_H #define LIST_H typedef struct l_node { int dato; struct l_node *next; } LNODE; typedef struct List { LNODE *head; int nelem; } LIST; void list_init(list *l); void list_insert_h(list *l, int d); void list_insert_t(list *l, int d); int list_extract_h(list *l); int list_extract_t(list *l); void list_print(list *l); #endif G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 29 / 33

Implementation list.c void list_insert_h(list *l, int d) { LNODE *p = (LNODE *)malloc(sizeof(lnode)); p->dato = d; p->next = l->head; l->head = p; } void list_insert_t(list *l, int d) { LNODE *q = (LNODE *)malloc(sizeof(lnode)); q->dato = d; q->next = 0; } LNODE *p = l->head; // caso particolare: lista vuota if (p == 0) l->head = q; else { // scorri fino all ultimo elemento while (p->next!= 0) p = p->next; // collega il nuovo elemento p->next = q; } G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 30 / 33

Problems with lists One of the problems with the list is that searching is a O(n) operation while the previous algorithm on the array was O(log(n)) The list is useful if we frequently insert and extract from the head For example, inside an operating system, the list of processes (executing programs) may be implemented as a list ordered by process priority In general, when most of the operations are inserting/estracting from the headm the list is the simplest and most effective solution G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 31 / 33

Data structures so far Stack Insertion/extraction only at/from the top (LIFO) All operations are O(1) Queue (Circular Array) Insertion at tail, extraction from head (FIFO) All operations are O(1) Array (random access) Insertion at any point requires O(n) Extraction from any point requires O(n) Sorting requires O(n log(n)) Searching (in sorted array) requires O(log(n)) List (ordered) Insertion at any point requires O(n) Extraction from any point requires O(1) Searching requires O(n) G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 32 / 33

Exercise Implement a stack, using a list as a reference implementation Implement AddressBook as a list Implement a double-linked list (with pointers to go back and forth) Implement a method to visit a list in order (use the visitor pattern) G. Lipari (Scuola Superiore Sant Anna) Lists March 21, 2012 33 / 33