56
Introduction to Data Structures 2019 Spring Semester Joonwon Lee College of Software

Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

Introduction to Data Structures ������

2019 Spring Semester

Joonwon Lee

College of Software

Page 2: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

2

Instructor informationn Joonwon Lee

n Professor in College of Softwaren Research areas

n Computer Systems

n Contact informationn [email protected] http://csl.skku.edu/People/Joonn Corporate Collaboration Center #85572n Office hour: by appointment

Page 3: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

3

Course information

n Class hours

n Tuesday 12:00 – 13:15

n Thursday 13:30 – 14:45

n Language: English

n Classroom: 23217

n Prerequisite: C programming languagen You SHOULD know how to program using C (e.g., pointer)

n If you don’t know how to use pointer, please drop this course!

n Course website

n http://www.icampus.ac.kr

Page 4: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

4

What is this course?

n 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]

n Class goalsn Learn data structure, which is a way of collecting and organizing data in a

computer to perform operations on the data efficientlyn Cover arrays, stacks, queues, linked lists, tree, graph, hashing, search and

sorting algorithms

Page 5: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

5

Lecture notesn Textbook

n Fundamentals of data structures in C by Horowits, Sanhni and Anderson-Freed (2nd, 2008)

n Lecture Notesn Adapt Prof. Jinkyu Lee’s slidesn which is also from Prof. Jongwuk Lee’s slides.

Page 6: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

6

Gradingn Attendance: 10%

n You will be given F if you are absent eight times or more.n Alternative attendance approval: to follow SKKU rule

n Assignments: 30%n Six programming homeworksn Academic honesty is truly needed since the penalty is severe

n Mid-term exam: 30%

n Final exam: 30%

Page 7: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

7

Gradingn If you have any plan to be absent with reasonable reasons, please tell

me in advance!

n Cheating will lead you to fail this course with “F” grade.

n You will be given F if you are absent eight times or more.

n The 50% A and 90% guideline is merely an upper-limit by SKKU.

Page 8: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

8

Tentative schedule and topics

Week 1: Course introduction, C overview

Week 2: Performance analysis, recursion Homework 1

Week 3: Stacks

Week 4: Stacks Homework 2

Week 5: Queues

Week 6: Lists Homework 3

Week 7: Trees

Week 8: Mid-term examApril 22nd Tuesday 12:00-15:00

Page 9: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

9

Tentative schedule and topics

Week 9: Mid-term review, Trees 10/29: Homework 4 out

Week 10: Graphs

Week 11: Graphs 11/12: Homework 5 out

Week 12: Sorting

Week 13: Sorting 11/26: Homework 6 out

Week 14:Hashing

Week 15: Reserved

Week 16: Final exam June 18th Tuesday 12:00 – 15:00

Page 10: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

10

TAn To be updated

Page 11: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

11

Do we have to use only English?n You are recommended to use English only.

n Assignmentn Your program (C code): only in English n Your report: either in English or Korean

n Examn All questions: written in Englishn Your answers: either in English or Korean

n Outside of the classn Questions through email: either in English or Korean

n In-classn ?

Page 12: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

12

Any question?n And, any suggestion?

Page 13: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

Basics of data structures

Page 14: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

14

Definitionn Definition of data structure

n An organization of information, usually in memory, for better algorithm efficiency

n Queue, stack, linked list, heap, dictionary, tree, etc.

n Definition of algorithmn What is an algorithm?n What is an efficient algorithm?

n An algorithm which spends less resources: time and space

Page 15: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

15

Definitionn 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

Page 16: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

16

Programn Program

Program = Data Structure + Algorithm

How to store data in computer memory

How to handle the stored data

Page 17: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

17

Examplen Example 1

n Question 1: sort 100 records.n Question 2: sort 1,000,000 records.

Question 1 Question 2

Insertion Sort c1*104 unit time c1*1012 unit time

Heap sort c2*102 unit time c2*106 unit time

Page 18: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

18

Examplen Example 2

n Question 1: You have a lot data. You frequently search for some data.n Question 2: You have the same data. You frequently insert and delete some

data.

Question 1 Question 2

Array c11* log n unit time c12* n unit time

Linked-List c21* n unit time c22 unit time

Page 19: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

19

Overviewn What to learn in Data Structure

n Some theory and data structure to efficiently store and manipulate datan Some algorithms and examples

n Examplen Data structure: array, list, queue, tree, graph, sorting, hashing, heap, etc.n Algorithm: sorting, searching, minimum spanning tree, shortest path

algorithm, etc.n Theory: abstract data types, performance analysis

Page 20: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

20

Overviewn Data Structure course vs. Algorithm course

Program = Data Structure + Algorithm

Data Structure + Algorithm

Data Structure Course

Data Structure + Algorithm

Algorithm Course

Page 21: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

Review on C Programming

Page 22: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

22

Review on C Programmingn What should you do with the C programming?

n How to use an array and a pointern Understand the relationship between the array and the pointer.n Use dynamic memory allocation.

n How to define a structure and make use of itn Understand self-referential structures.

n How to use recursive programmingn Transform iterative programming into recursive programming.

n To take this course, I strongly recommend that you have taken Cprogramming!

Page 23: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

23

What is Array?n Definition

n A collection of elements with same data typesn Each element is sequential in memory.

n Array indexn Each element can be referenced by an index.n Index ranges: [0] ~ [size – 1]

int score[10];

Data type Array name Array size

Page 24: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

24

Example: Arrayn Input five numbers and print them reversely.

#include <stdio.h>

#define ARRAY_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;

}

Page 25: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

25

Function Call with Arrayn Calculating the average of values in array

#include <stdio.h>#define ARRAY_SIZE 5

void inputNumbers(int num[], int len);double computeAverage(int num[], int len);

int main(){

int numbers[ARRAY_SIZE];

inputNumbers(numbers, ARRAY_SIZE);printf("average: %.3lf", computeAverage(numbers, ARRAY_SIZE));

return 0;}

Page 26: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

26

Function Call with Arrayn Calculating the average of values in array

void inputNumbers(int num[], int len){

int i;for (i = 0; i < len; i++)

scanf("%d", &num[i]);}

double computeAverage(int num[], int len){

int total = 0, i;for (i = 0; i < len; i++)

total = total + num[i];return total / (double)len;

}

Page 27: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

27

Two-Dimensional Arrayn 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;

else if (row == col)matrix[row][col] = 0;

elsematrix[row][col] = -1;

}}

Page 28: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

28

int scores[10]; int matrix[6][6];

Page 29: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

29

What is Pointer?n Definition

n A variable to store a memory address instead of a value

3

00FDFC18

n

Value

AddressName

00FDFC18

0070FA44

pn

int n = 3;

int* pn = &n;

Variable

Page 30: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

30

& (Ampersand) Operatorn Reference operator

n Return the address of an variable.

65 (‘A’) c

00FDFC18

pc00FDFC1C

00FDFC19

00FDFC1A

00FDFC1B

00FDFC1C

00FDFC1D

00FDFC1E

00FDFC1F

char* pc

65 (‘A’)

cpc

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

}

Page 31: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

31

& (Ampersand) Operatorn What is the size of a pointer variable?

n

00FDFC18

pn00FDFC1C00FDFC19

00FDFC1A

00FDFC1B

00FDFC1C

00FDFC1D

00FDFC1E

00FDFC1F

int* pn

3

npn

3

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

Page 32: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

32

* (Asterisk) Operatorn Dereference operator

n Return the value at the pointer address.

67 (‘C’) c

00FDFC18

pc00FDFC1C00FDFC19

00FDFC1A

00FDFC1B

00FDFC1C

00FDFC1D

00FDFC1E

00FDFC1F

char* pc#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;}

Page 33: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

33

Example: Pointern 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;}

Page 34: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

34

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

Page 35: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

35

Functional Call with Pointern Two types for inter-function communication

n Call by value: passing by valuen Call by reference: passing by address

#include <stdio.h>

void swap1(int x, int y);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, int y){

int temp = x;x = y;y = temp;

}

void swap2(int* px, int* py){

int temp = *px;*px = *py;*py = temp;

}

Page 36: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

36

Pointer to Pointern We can use a pointer that points to another pointer.

#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;} 65 (‘A’) c

pc0062Fd90

ppc00FDFD84

char **ppc

&pc

&c

&ppc 0062FD78

0062FD79

0062FD7A

0062FD7B

00FDFD84

00FDFD85

00FDFD86

00FDFD87

0062Fd90

0062Fd91

Page 37: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

37

Arithmetic Operation for Pointern 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;}

65 (‘A’) c

0062Fd90

00FDFD84

char **ppc

&pc

&c

&ppc 0062FD78

0062FD79

0062FD7A

0062FD7B

00FDFD84

00FDFD85

00FDFD86

00FDFD87

0062Fd90

0062Fd91

Page 38: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

38

Arithmetic Operation for Pointern How does the int pointer work?

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

n

pn0062Fd90

20

ppn00FDFD84

int** ppn

&pn

&ppn

+1

&n

0062FD78

0062FD79

0062FD7A

0062FD7B

00FDFD84

00FDFD85

00FDFD86

00FDFD87

0062Fd90

0062Fd91

0062Fd92

0062Fd93

Page 39: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

39

Array and Pointern 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;

}

int a[6];

5312

00FDFC18

00FDFC1C

00FDFC20

00FDFC24

a[0]

a[1]

a[2]

a[3]

4Bytea

00FDFC28

6a[4]

a[5]00FDFC2C

4

00FDFC30

*(a + 0)

*(a + 1)

*(a + 2)

*(a + 3)

*(a + 4)

*(a + 5)

00FDFC18 pa00FDFC3C

Page 40: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

40

Example: Array and Pointern Is it a correct code?

1234

65

a pi

pend-1440986684

00FDFC18

00FDFC1C

00FDFC20

00FDFC24

00FDFC28

00FDFC2C00FDFC30

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

Page 41: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

41

Example: Array and Pointern 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;

}

32123142

2415

a psmallest

pend-1440986684

00FDFC18

00FDFC1C

00FDFC20

00FDFC24

00FDFC28

00FDFC2C00FDFC30

Page 42: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

42

Passing Array to Functionn Passing a pointer (instead of an array) to a function

#include <stdio.h>void printArray(int* pa, int len);

int main(){

int a[5] = { 5, 3, 2, 1, 4 };printArray(a, 5);return 0;

}

void printArray(int* pa, int len){

int i;for (i = 0; i < len; i++)

printf("%d\n", pa[i]);}

int a[5];

5321

00FDFC18

00FDFC1C

00FDFC20

00FDFC24

a[0]

a[1]

a[2]

a[3]

4Bytea

00FDFC28 a[4]00FDFC2C

4

00FDFC3000FDFC18 pa

Page 43: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

43

Example: Passing Array to Functionn Multiplying 4 for each element in an array

#include <stdio.h>void multiply4(int* pa, int len);

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, int len){int i;for (i = 0; i < len; i++)pa[i] = pa[i] * 4;

}

int a[5];

5321

00FDFC18

00FDFC1C

00FDFC20

00FDFC24

a[0]

a[1]

a[2]

a[3]

4Bytea

00FDFC28 a[4]

00FDFC2C

4

00FDFC3000FDFC18 pa

Page 44: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

44

How to Use Memory in Cn Conceptual view of memory used in C program

Page 45: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

45

Dynamic Memory Allocation

#include <stdio.h>#include <stdlib.h>

int main(){

int size, i;scanf("%d", &size);// Allocate dynamic memoryint* 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;}

Page 46: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

46

Dynamic Memory Allocationn 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(int size){

int i;int *pn = malloc(4 * size);for (i = 0; i < size; i++)

scanf("%d", &pn[i]);

return pn;}

Page 47: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

47

Memory Leakn It occurs when a computer program incorrectly

manages memory allocations.n Memory that is no longer used is not released yet.

Page 48: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

48

Solving Memory Leakn Use a pair of allocation and deallocation together!

#include <stdio.h>#include <stdlib.h>void genNumbers(int* pn, int s);

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;

}

void genNumbers(int* pn, int s){

int i;for (i = 0; i < s; i++)

scanf("%d", &pn[i]);}

Page 49: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

49

What is Structure?n Definition

n A collection of multiple related elementsn A single name including possibly several different types

Page 50: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

50

What is Structure?n How to declare a structure

n All elements in the structure should be related logically.

#include <stdio.h>typedef struct{

char name[10];int scores[3];

} STUDENT;

int main(){

STUDENT s1 = { "Alice", 80, 70, 60 };

printf("%s\n", s1.name);for (int i = 0; i < 3; i++)

printf("%d\n", s1.scores[i]);return 0;

}

Page 51: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

51

-> (Arrow) Operatorn It is used to access the value of a structure pointer.

#include <stdio.h>typedef struct{

char name[10];int scores[3];

} STUDENT;

int main(){

STUDENT s1 = { "Alice", 80, 70, 60 };STUDENT* s2 = &s1;

printf("%s\n", s2->name);for (int i = 0; i < 3; i++)

printf("%d\n", s2->scores[i]);return 0;

}

Page 52: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

52

Example: Structuren Is it a correct code?

int main(){

STUDENT stu[1];for (int i = 0; i < 1; i++) {

scanf("%s", stu[i].name); for (int j = 0; j < 3; j++) {

scanf("%d", &stu[i].scores[j]);stu[i].total += stu[i].scores[j];

}}for (int i = 0; i < 1; i++) {

printf("%s\n", stu[i].name);for (int j = 0; j < 3; j++)

printf("%d\n", stu[i].scores[j]);printf("%d\n", stu[i].total);

}return 0;

}

typedef struct{

char name[10];int scores[3];int total;

} STUDENT;

Page 53: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

53

Example: Structuren Structure with dynamic memory allocation

int main(){

int n;scanf("%d", &n);

STUDENT* s = malloc(sizeof(STUDENT)*n);for (int i = 0; i < n; i++) {

scanf("%s", s[i].name);s[i].total = 0;for (int j = 0; j < 3; j++) {

scanf("%d", &s[i].scores[j]);s[i].total += s[i].scores[j];

}}free(s);return 0;

}

typedef struct{

char name[10];int scores[3];int total;

} STUDENT;

Page 54: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

54

Example: Structuren Multiplying two fractions

#include <stdio.h>

int main(){

FRACTION f1 = { 4, 5 };FRACTION f2 = { 3, 7 };FRACTION f3;

f3.numerator = f1.numerator * f2.numerator;f3.denominator = f1.denominator * f2.denominator;

printf("%d / %d", f3.numerator, f3.denominator);return 0;

}

typedef struct{int numerator;int denominator;} FRACTION;

Page 55: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

55

Example: Structuren Summing two fractions

#include <stdio.h>

int main(){

FRACTION f1 = { 4, 5 };FRACTION f2 = { 3, 7 };FRACTION f3;

f3.numerator = f1.numerator * f2.denominator;f3.numerator += f2.numerator * f1.denominator;f3.denominator = f1.denominator * f2.denominator;

printf("%d / %d", f3.numerator, f3.denominator);return 0;

}

typedef struct{int numerator;int denominator;} FRACTION;

Page 56: Introduction to Data Structurescsl.skku.edu/uploads/SWE2015S19-41/Lecture_Note01.pdf · modification.More precisely, a data structure is a collection ofdata values, the relationships

56

How to Be a Good Programmern Tips for programming

n https://www.oreilly.com/ideas/7-ways-to-be-a-better-programmer-in-2014

n �������Websiten Euler project: http://euler.synap.co.kr/n Backjoon online judge: https://www.acmicpc.net/n Algospot: https://www.algospot.com/judge/problem/list/