560 likes | 588 Views
Introduction to Data Structures 자료구조개론 2019 Spring Semester. Joonwon Lee College of Software. Instructor information. Joonwon Lee Professor in College of Software Research areas Computer Systems Contact information joonwon@skku.edu http:// csl.skku.edu/People/Joon
E N D
Introduction to Data Structures 자료구조개론2019 Spring Semester JoonwonLee College of Software
Instructor information • Joonwon Lee • Professor in College of Software • Research areas • Computer Systems • Contact information • joonwon@skku.edu • http://csl.skku.edu/People/Joon • Corporate Collaboration Center #85572 • Office hour: by appointment
Course information • Class hours • Tuesday 12:00 – 13:15 • Thursday 13:30 – 14:45 • Language: English • Classroom: 23217 • Prerequisite: C programming language • You SHOULD know how to program using C (e.g., pointer) • If you don’t know how to use pointer, please drop this course! • Course website • http://www.icampus.ac.kr
What is this course? • In computer science, a data structure is a data organization, management and storage format that enables efficient access and modification.More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data. [Wikipedia] • Class goals • Learn data structure, which is a way of collecting and organizing data in a computer to perform operations on the data efficiently • Cover arrays, stacks, queues, linked lists, tree, graph, hashing, search and sorting algorithms
Lecture notes • Textbook • Fundamentals of data structures in C by Horowits, Sanhni and Anderson-Freed (2nd, 2008) • Lecture Notes • Adapt Prof. Jinkyu Lee’s slides • which is also from Prof. Jongwuk Lee’s slides.
Grading • Attendance: 10% • You will be given F if you are absent eight times or more. • Alternative attendance approval: to follow SKKU rule • Assignments: 30% • Four programming homeworks • Academic honesty is truly needed since the penalty is severe • Mid-term exam: 30% • Final exam: 30%
Grading • If you have any plan to be absent with reasonable reasons, please tell me in advance! • Cheatingwill lead you to fail this course with “F” grade. • You will be given F if you are absent eight times or more. • The 50% A and 90% guideline is merely an upper-limit by SKKU.
TA • To be updated
Do we have to use only English? • You are recommended to use English only. • Assignment • Your program (C code): only in English • Your report: either in English or Korean • Exam • All questions: written in English • Your answers: either in English or Korean • Outside of the class • Questions through email: either in English or Korean • In-class • ?
Any question? • And, any suggestion?
Definition • Definition of data structure • An organization of information, usually in memory, for better algorithm efficiency • Queue, stack, linked list, heap, dictionary, tree, etc. • Definition of algorithm • What is an algorithm? • What is an efficient algorithm? • An algorithm which spends less resources: time and space
Definition • Definition of an algorithm: a finite set of instructions that should satisfy 1) Input: zero or more inputs 2) Output: at least one output 3) Definiteness: clear and unambiguous instructions 4) Finiteness: terminating after a finite number of steps 5) Effectiveness (Machine-executable): basic enough to be carried out
Program • Program Program = Data Structure + Algorithm How to store data in computer memory How to handle the stored data
Example • Example 1 • Question 1: sort 100 records. • Question 2: sort 1,000,000 records.
Example • Example 2 • Question 1: You have a lot sorted data. You frequently search for some data. • Question 2: You have the same data. You frequently insert and delete some data.
Overview • What to learn in Data Structure • Some theory and data structure to efficiently store and manipulate data • Some algorithms and examples • Example • Data structure: array, list, queue, tree, graph, sorting, hashing, heap, etc. • Algorithm: sorting, searching, minimum spanning tree, shortest path algorithm, etc. • Theory: abstract data types, performance analysis
Algorithm Course Data Structure Course Data Structure + Algorithm Data Structure+ Algorithm Overview • Data Structure course vs. Algorithm course Program = Data Structure + Algorithm
Review on C Programming • What should you do with the C programming? • How to use an array and a pointer • Understand the relationship between the array and the pointer. • Use dynamic memory allocation. • How to define a structure and make use of it • Understand self-referential structures. • How to use recursiveprogramming • Transform iterative programming into recursive programming. • To take this course, Istronglyrecommendthat youhavetakenCprogramming!
WhatisArray? • Definition • A collection of elements with same data types • Each element is sequential in memory. • Array index • Each element can be referenced by an index. • Index ranges: [0] ~ [size – 1] intscore[10]; Data type Array name Array size
Example: Array • Input five numbers and print them reversely. #include<stdio.h> #defineARRAY_SIZE 5 int main() { int numbers[ARRAY_SIZE], i; printf("Input five numbers\n"); for (i = 0; i < ARRAY_SIZE; i++) scanf("%d", &numbers[i]); for (i = ARRAY_SIZE - 1; i >= 0; i--) printf("%d ", numbers[i]); return 0; }
Function Call with Array • Calculating the average of values in array #include<stdio.h> #defineARRAY_SIZE 5 voidinputNumbers(intnum[], intlen); doublecomputeAverage(intnum[], intlen); int main() { int numbers[ARRAY_SIZE]; inputNumbers(numbers, ARRAY_SIZE); printf("average: %.3lf", computeAverage(numbers, ARRAY_SIZE)); return 0; }
Function Call with Array • Calculating the average of values in array voidinputNumbers(intnum[], intlen) { inti; for (i = 0; i < len; i++) scanf("%d", &num[i]); } doublecomputeAverage(intnum[], intlen) { int total = 0, i; for (i = 0; i < len; i++) total = total + num[i]; return total / (double)len; }
Two-Dimensional Array • Filling all entries in the two-dimensional matrix int row, col, matrix[6][6]; for (row = 0; row < 6; row++) { for (col = 0; col < 6; col++) { if (row < col) matrix[row][col] = 1; elseif (row == col) matrix[row][col] = 0; else matrix[row][col] = -1; } }
int scores[10]; int matrix[6][6];
What is Pointer? • Definition • A variable to store a memory address instead of a value intn = 3; 0070FA44 Address 00FDFC18 n pn Name Value 3 00FDFC18 int* pn = &n; Variable
& (Ampersand) Operator • Reference operator • Return the address of an variable. char* pc 00FDFC18 #include<stdio.h> int main() { char c = 'A'; char *pc = &c; printf("%c %p\n", c, pc); printf("%p %p\n", &c, &pc); printf("%d %d\n", sizeof(c), sizeof(pc)); return 0; } 00FDFC1C 00FDFC19 pc 00FDFC1A 00FDFC1B c 00FDFC1C 65 (‘A’) 00FDFC1D 00FDFC1E 00FDFC1F pc c 65 (‘A’)
& (Ampersand) Operator • What is the size of a pointer variable? int* pn 00FDFC18 #include<stdio.h> int main() { int n = 3; int*pn= &n; printf("%d %p\n", n, pn); printf("%p %p\n", &n, &pn); printf("%d %d\n", sizeof(n), sizeof(pn)); return 0; } 00FDFC1C 00FDFC19 pn 00FDFC1A 00FDFC1B 00FDFC1C 3 00FDFC1D n 00FDFC1E 00FDFC1F pn n 3
* (Asterisk) Operator • Dereference operator • Return the value at the pointer address. #include<stdio.h> int main() { char c = 'A'; char *pc = &c; printf("%c %c\n", c, *pc); *pc = 'C'; printf("%c %c\n", c, *pc); return 0; } char *pc 00FDFC18 00FDFC1C 00FDFC19 pc 00FDFC1A 00FDFC1B c 00FDFC1C 67 (‘C’) 00FDFC1D 00FDFC1E 00FDFC1F
Example: Pointer • Use address and dereference operators correctly. #include<stdio.h> int main() { int a, b, c; int*p, *q, *r; a = 6, b = 10; p = &b, q = p, r = &c; p = &a, *q = 8, *r = *p; *r = a + *q + *&c; printf("%d %d %d", a, b, c); return 0; }
Example: Pointer #include<stdio.h> int main() { int a, b, c; int*pa = &a, *pb = &b, *pc = &c; *pa = 10, *pb = 20; *pc = *pa + *pb; printf("%d %d %d", a, b, c); return 0; }
Functional Call with Pointer • Two types for inter-function communication • Call by value: passing by value • Call by reference: passing by address #include<stdio.h> void swap1(int x, inty); void swap2(int* px, int* py); int main() { int a = 5, b = 7; swap1(a, b); printf("%d %d\n", a, b); swap2(a, b); printf("%d %d\n", a, b); return 0; } void swap1(int x, inty) { int temp = x; x = y; y = temp; } void swap2(int* px, int* py) { int temp = *px; *px = *py; *py = temp; }
Pointer to Pointer • Wecan use a pointer that points to another pointer. char **ppc #include<stdio.h> int main() { char c = 'A'; char* pc = &c; char** ppc = &pc; printf("%p %p\n", pc, ppc); printf("%d %d\n", sizeof(pc), sizeof(ppc)); return 0; } &ppc 0062FD78 00FDFD84 0062FD79 ppc 0062FD7A 0062FD7B 00FDFD84 &pc 0062Fd90 00FDFD85 pc 00FDFD86 00FDFD87 c 0062Fd90 65 (‘A’) &c 0062Fd91
Arithmetic Operation for Pointer • How does the char pointer work? #include<stdio.h> int main() { char c = 'A'; char* pc = &c; char** ppc = &pc; printf("%p %p\n", pc, ppc); printf("%p %p\n", pc + 1, ppc + 1); printf("%p %p\n", &c, &c + 1); printf("%p %p\n", &pc, &ppc); printf("%p %p\n", &pc + 1, &ppc + 1); return 0; } char **ppc &ppc 0062FD78 00FDFD84 0062FD79 0062FD7A 0062FD7B 00FDFD84 &pc 0062Fd90 00FDFD85 00FDFD86 00FDFD87 c 0062Fd90 65 (‘A’) &c 0062Fd91
Arithmetic Operation for Pointer • How does the int pointer work? int** ppn #include<stdio.h> int main() { int n = 10; int* pn = &n; int** ppn = &pn; printf("%p %p\n", pn, ppn); printf("%p %p\n", pn + 1, ppn + 1); printf("%p %p\n", &n, &n + 1); printf("%p %p\n", &pn, &ppn); printf("%p %p\n", &pn + 1, &ppn + 1); return 0; } &ppn 0062FD78 00FDFD84 +1 0062FD79 ppn 0062FD7A 0062FD7B &pn 00FDFD84 0062Fd90 00FDFD85 pn 00FDFD86 00FDFD87 &n 0062Fd90 20 0062Fd91 n 0062Fd92 0062Fd93
Array and Pointer • The pointer to the first element of the array can be used as the name of the array. #include<stdio.h> int main() { int a[6] = { 5, 3, 1, 2, 4, 6 }; int* pa = a; printf("%d %d\n", *a, *pa); printf("%p %p\n", a, pa); printf("%p %p\n", &a, &pa); printf("%d %d\n", a[0], pa[0]); printf("%d %d\n", a[1], pa[1]); return 0; } inta[6]; 4Byte a 00FDFC18 a[0] *(a + 0) 5 00FDFC1C a[1] 3 *(a + 1) 00FDFC20 1 a[2] *(a + 2) 2 00FDFC24 a[3] *(a + 3) 4 00FDFC28 a[4] *(a + 4) 6 00FDFC2C a[5] *(a + 5) 00FDFC30 pa 00FDFC18 00FDFC3C
Example: Array and Pointer • Is it a correct code? #include<stdio.h> int main() { int a[6] = { 1, 2, 3, 4, 5, 6 }; int*pend = a + 6; int *pi = NULL; for (pi = a; pi < pend; pi++) printf("%d\n", *pi); return 0; } a pi 00FDFC18 1 00FDFC1C 2 00FDFC20 3 00FDFC24 4 5 00FDFC28 6 00FDFC2C pend -1440986684 00FDFC30
Example: Array and Pointer • Print the smallest array value using pointers. #include<stdio.h> int main() { int a[6] = { 32, 12, 31, 42, 15, 24 }; int*pend = a + 6; int*psmallest = a; int *pi = NULL; for (pi = a; pi < pend; pi++) { if (*pi < *psmallest) psmallest = pi; } printf("%d", *psmallest); return 0; } psmallest a 00FDFC18 32 00FDFC1C 12 00FDFC20 31 00FDFC24 42 15 00FDFC28 24 00FDFC2C pend -1440986684 00FDFC30
Passing Array to Function • Passing a pointer (instead of an array) to a function #include<stdio.h> voidprintArray(int* pa, intlen); int main() { int a[5] = { 5, 3, 2, 1, 4 }; printArray(a, 5); return 0; } voidprintArray(int* pa, intlen) { inti; for (i = 0; i < len; i++) printf("%d\n", pa[i]); } inta[5]; 4Byte a 00FDFC18 a[0] 5 00FDFC1C a[1] 3 00FDFC20 2 a[2] 1 00FDFC24 a[3] 4 00FDFC28 a[4] 00FDFC2C pa 00FDFC18 00FDFC30
Example: Passing Array to Function • Multiplying 4 for each element in an array #include<stdio.h> void multiply4(int* pa, intlen); int main() { int a[5] = { 5, 3, 2, 1, 4 }, i; multiply4(a, 5); for (i = 0; i < 5; i++) printf("%d\n", a[i]); return 0; } void multiply4(int* pa, intlen) { inti; for (i = 0; i < len; i++) pa[i] = pa[i] * 4; } inta[5]; 4Byte a 00FDFC18 a[0] 5 00FDFC1C a[1] 3 00FDFC20 2 a[2] 1 00FDFC24 a[3] 4 00FDFC28 a[4] 00FDFC2C pa 00FDFC18 00FDFC30
How to Use Memory in C • Conceptual view of memory used in C program
Dynamic Memory Allocation #include<stdio.h> #include<stdlib.h> int main() { int size, i; scanf("%d", &size); // Allocate dynamic memory int* pn = malloc(sizeof(int)* size); for (i = 0; i < size; i++) scanf("%d", &pn[i]); for (i = 0; i < size; i++) printf("%d\n", pn[i]); free(pn); // Release memory return 0; }
Dynamic Memory Allocation • Is it a correct code? #include<stdio.h> #include<stdlib.h> int*genNumbers(int size); int main() { int size, i; scanf("%d", &size); int*pn = genNumbers(size); for (i = 0; i < size; i++) printf("%d\n", pn[i]); return 0; } int*genNumbers(intsize) { inti; int*pn = malloc(4 * size); for (i = 0; i < size; i++) scanf("%d", &pn[i]); returnpn; }
Memory Leak • It occurs when a computer program incorrectly manages memory allocations. • Memory that is no longer used is not released yet.
Solving Memory Leak • Use a pair of allocation and deallocation together! #include<stdio.h> #include<stdlib.h> voidgenNumbers(int* pn, ints); int main() { int size, i; scanf("%d", &size); int* pn = malloc(4 * size); genNumbers(pn, size); for (i = 0; i < size; i++) printf("%d\n", pn[i]); free(pn); return 0; } voidgenNumbers(int* pn, ints) { inti; for (i = 0; i < s; i++) scanf("%d", &pn[i]); }
What is Structure? • Definition • A collection of multiple related elements • A single name including possibly several different types
What is Structure? • How to declare a structure • All elements in the structure should be related logically. #include<stdio.h> typedefstruct { char name[10]; int scores[3]; } STUDENT; int main() { STUDENT s1 = { "Alice", 80, 70, 60 }; printf("%s\n", s1.name); for (inti = 0; i < 3; i++) printf("%d\n", s1.scores[i]); return 0; }