C programming and Data Structures...

78
Intelligent Networking Laboratory H.CHOO 1/78 Copyright 2000-2020 Intelligent Networking Laboratory C programming and Data Structures Overview T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo [email protected]

Transcript of C programming and Data Structures...

Page 1: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 1/78Copyright 2000-2020 Intelligent Networking Laboratory

C programming and

Data Structures Overview

T. H. Cormen, C. E. Leiserson and R. L. Rivest

Introduction to Algorithms, 3rd Edition, MIT Press, 2009

Sungkyunkwan University

Hyunseung Choo

[email protected]

Page 2: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 2/78

Contents

Functions

Invocations Function

Function Definitions Return

statements

Function Prototypes

Call by value and call by

reference

Recursions

Recursive Call

Examples

Networking Laboratory 2/78

Structures

Struct Declaration

Structure Tag

Compatible Structure

Memory Allocation

Accessing a Member

Pointers

Pointer Variable

Passing Pointers to Functions

Exercise Problem

Page 3: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 3/78

Networking Laboratory 3/78

Functions

Page 4: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 4/78

Invocations Function (1/3)

Function

C 프로그램은하나이상의 Function들로구성

모든 c-program은반드시 한 개의 main() Function을포함

반복되는 codes의경우 function으로정의하여필요시마다그function을호출하여사용함으로서 simplicity의향상

Function의종류

Library functions : System이 제공하는 Predefined-Functions

User-defined functions : Programmer에 의해작성된 Functions

Function Invocation

Function의 호출 : Function_name() 의 형식으로 사용.

Function의 종료 : Function을 Call한곳으로 제어 권의이동Networking Laboratory 4/78

Page 5: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 5/78

Invocations Function (2/3)

Networking Laboratory 5/78

Page 6: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 6/78

Invocations Function (3/3)

Networking Laboratory 6/78

Page 7: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 7/78

Function Definitions (1/2)

Function이 호출되기 전반드시 해당 Function을 다음형식으로정의해야 한다.

Networking Laboratory 7/78

Page 8: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 8/78

Function Definitions (2/2)

Parameter Type List

Function 호출시전달되는 arguments에대응되는순서와 data

types을지정

Function body내에서 identifier로사용될수있다

Return type

Function 종료될때 return statement에의해 전달되는 value 의type.

Default type : integer type으로, 생략시자동 integer로간주

void type : Return Value가없는경우 void로지정

Networking Laboratory 8/78

Page 9: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 9/78

The Return Statement

Function의 실행 종료, 호출한 곳으로 control을 이전하는statement

생략시는 function body 끝의 ‘}’를 만났을 때자동 return

복수개의 return문 사용 가능

단, 하나의 Function에서두 개의값을동시에 return할수없음

Networking Laboratory 9/78

Page 10: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 10/78

The Return Value

Networking Laboratory 10/78

Page 11: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 11/78

Function Prototypes (1/2)

Function 사용을 위해호출 전에반드시 필요한 Function

선언문

prototype이 생략가능한 경우

모든조건이 default인경우생략가능 : return value의 type과argument의 type이 integer인경우생략가능

function이 main() function전에정의된경우

선언형식

return-type function_name (parameter type list);

Networking Laboratory 11/78

Page 12: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 12/78

Function Prototypes (2/2)

Argument와 parameter의 type이 일치하지 않는경우function prototype에서 지정된 type으로 convert됨.

Networking Laboratory 12/78

Page 13: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 13/78

Function Definition Order

한개의 file로 작성된 program의 일반적 순서

1. #include, #define statements

2. Enumeration types and typedef

3. struct definition

4. Function Prototypes

5. main() Function

6 Function Definitions

Networking Laboratory 13/78

Page 14: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 14/78

main( )안에서의 Function Definition

Networking Laboratory 14/78

Page 15: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 15/78

Developing Large Program (1/2)

대규모 Program의경우, 여러 ~.h 파일과 ~.c파일로나누어작성할 수있다.

Team에의한 작업분담이용이해진다.

프로그램이변경될 때마다 변경된 ~.c 파일만을compile함으로서 시간절약 가능

Networking Laboratory 15/78

Page 16: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 16/78

Developing Large Program (2/2)

Networking Laboratory 16/78

Page 17: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 17/78

Call by Value and Call by Reference

Call-by-Value

Function Invocation이일어나면, argument의 value를전달받기위한 parameter를위해메모리영역이새로생기며 argument의값이새영역에 copy된다.

Function Invocation의 parameter를 identifier로사용하여그값을 function내에서변경한경우에도다른 memory영역을사용함으로써실제 argument의값은변경되지않는다.

Call-by-Reference

Function Invocation이일어나면 argument의 address를전달한다.

실제값의 address를변경하기때문에그값을 function내에서변경한경우실제 argument값이변경된다.

Networking Laboratory 17/78

Page 18: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 18/78

Example of Call by Value

Networking Laboratory 18/78

Page 19: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 19/78

Example of Call by Reference

Networking Laboratory 19/78

Page 20: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 20/78

Networking Laboratory 20/78

Recursions

Page 21: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 21/78

Recursive Call

어떤함수가 자기자신을 호출하는것

Networking Laboratory 21/78

Page 22: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 22/78

Factorial을구하는 예제

Networking Laboratory 22/78

Page 23: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 23/78

Array의평균을 구하는 예제

Networking Laboratory 23/78

Page 24: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 24/78

Drawing Patterns on the Screen (1/2)

제곱근을구하는 예제

Networking Laboratory 24/78

Page 25: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 25/78

Drawing Patterns on the Screen (2/2)

역순으로문자를 출력하는 예제

입력받은문장의문자들을역순으로출력한다.

Networking Laboratory 25/78

Page 26: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 26/78

Networking Laboratory 26/78

Structures

Page 27: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 27/78

Array와 structure의차이점

array

Array의모든 element는같은 type이여야한다.

Index를사용하여각 element를 access한다.

structure

다른 type의 element로구성될수있다있다.

각 element는 name을갖는다.

Name에의해각 element를 access한다.

Networking Laboratory 27/78

Page 28: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 28/78

Struct Declaration

Collection of members(/elements)

Networking Laboratory 28/78

Page 29: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 29/78

Structure Tag

정의되는 특정 structure를 지정하기위한 name

한번 structure tag인 part가 정의되면, 이제 tag를사용하여같은 structure type으로 선언할 수있다.

Networking Laboratory 29/78

Page 30: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 30/78

Structure Tag와변수 동시선언

structure tag를 이용하여 선언된변수는 같은 structure

type

Networking Laboratory 30/78

Page 31: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 31/78

Compatible Structure (1/2)

같은 type의 structure variable이면 서로 assign 가능

compatible types의 조건

Structure 정의와동시에선언되는모든 variables

같은 type의 structure 즉같은 tag에의해선언된모든 variables

Networking Laboratory 31/78

Page 32: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 32/78

Compatible Structure (2/2)

compatible type이 아닐경우 =, ==, != 불가능

Networking Laboratory 32/78

Page 33: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 33/78

Memory Allocation

Structure로 선언된데이터 type은

각 member들이 메모리 내에

순차적으로할당된다.

part1의 base address가 200이고,

integer size가 4byte라 가정하면,

오른쪽 그림과 같이 메모리가 할당됨Networking Laboratory 33/78

Page 34: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 34/78

Accessing a Member (1/3)

struct member operator ‘.’

Structure의각 member를 access하기위해 ‘.’를사용한다.

Networking Laboratory 34/78

Page 35: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 35/78

Accessing a Member (2/3)

member operation

Networking Laboratory 35/78

Page 36: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 36/78

Accessing a Member (3/3)

structure pointer

Networking Laboratory 36/78

Page 37: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 37/78

Structures as Argument

call-by-value로 struct가 copy되어 사용 된다.

Networking Laboratory 37/78

Page 38: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 38/78

Struct Pointer사용 (1/2)

call-by-reference로 struct의 address를 전달한다.

Networking Laboratory 38/78

Page 39: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 39/78

Struct Pointer사용 (2/2)

Networking Laboratory 39/78

Page 40: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 40/78

The Use of typedef (1/2)

data type의 name을 재정의 하기위해 사용

readability의 증가

Networking Laboratory 40/78

Page 41: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 41/78

The Use of typedef (2/2)

typedef를 사용, struct type을 새로운 type으로 선언

Networking Laboratory 41/78

Page 42: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 42/78

Networking Laboratory 42/78

Pointers

Page 43: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 43/78

Pointer Variable

포인터변수는 변수명은같으나 변수선언을 할 때 *

연산자를사용한다.

int *a; → int형포인터

포인터변수는 타입에 상관없이 4바이트다.

Networking Laboratory 43/78

Page 44: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 44/78

Passing Pointers to Functions (1/4)

포인터를 argument로 하는 함수예제

Networking Laboratory 44/78

Page 45: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 45/78

Passing Pointers to Functions (2/4)

포인터를 argument로 하는 함수예제

Networking Laboratory 45/78

Page 46: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 46/78

Passing Pointers to Functions (3/4)

포인터를 argument로 하는 함수예제

Networking Laboratory 46/78

Page 47: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 47/78

Passing Pointers to Functions (4/4)

포인터를 argument로 하는 함수사용시 유의점

Networking Laboratory 47/78

Page 48: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 48/78

Exercise Problem

pointer의 주소 할당문제

Networking Laboratory 48/78

Page 49: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 49/78

Approaches

pointer의 예제

Networking Laboratory 49/78

Page 50: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 50/78Copyright 2000-2020 Intelligent Networking Laboratory

Data Structures Overview

Sungkyunkwan University

Hyunseung Choo

[email protected]

Page 51: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 51/78

Contents

Arrays

Arrays

Representation of

Multidimensional Arrays

Stacks and Queues

Stack Abstract Data Type

Queue Abstract Data Type

Circular Queues

Networking Laboratory 51/78

Lists

Singly Linked Lists

Doubly Linked Lists

Page 52: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 52/78

Networking Laboratory 52/78

Arrays

Page 53: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 53/78

Arrays (1/2)

An array is a set of pairs, <index, value>, such that

each index that is defined has a value associated with it

A consecutive set of memory locations in C

Logical order is the same as physical order

int list[5], *plist[5];

/* arrays start at index 0 in C */

- integers: list[0],..., list[4]

- int ptrs: plist[0],..., plist[4]

Networking Laboratory 53/78

Page 54: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 54/78

Arrays (2/2)

Variable Memory Address

list[0] base address = a

list[1] a + sizeof(int)

list[2] a + 2·sizeof(int)

list[3] a + 3·sizeof(int)

list[4] a + 4·sizeof(int)

list[i] in C programs, C interprets it as a pointer to an integer whose

address is the one in the table above

int *list1;

pointer variable to an int

int list2[5];

five memory locations for holding integers are reserved

Networking Laboratory 54/78

Page 55: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 55/78

Representation of Multidimensional Arrays

Internal Representation of Multidimensional Arrays

How to state n-dimensional array into 1-dimensional array?

How to retrieve arbitrary element in

a[upper0][upper1]···[uppern-1]

the number of elements in the array

n-1

P upperii=0

e.g.) a[10][10][10]

→ 10*10*10 = 1000 (units)Networking Laboratory 55/78

Page 56: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 56/78

1-dimensional Array

Starting-address + offset-value

Assume a: starting-address

1-dimensional array a[u0]

a[0] : a

a[1] : a + 1

: :

a[u0-1] : a + (u0 - 1)

&a[i] = α + iNetworking Laboratory 56/78

Page 57: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 57/78

2-dimensional Array

2-dimensional array a[u0][u1]

a[i][j] = α + i·u1 + j

Networking Laboratory 57/78

0 1 · · · u 1 - 1

0 a a + 1 a + (u 1-1)

1

·

·

·

u 0 - 1

?i

j

Page 58: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 58/78

Networking Laboratory 58/78

Stacks and Queues

Page 59: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 59/78

Stack Abstract Data Type

ADT stack Last-In-First-Out (LIFO) Ordered list, insertions and deletions are made at one end

called the “top”

Given stack S = (a0, ···, an-1)

a0 : bottom element

an-1 : top element

ai : on top of element ai-1 (0<i<n)

Inserting and deleting elements in stack

Networking Laboratory 59/78

topA

topB

A

topC

B

A

topD

C

B

A

topE

D

C

B

A

topD

C

B

A

Page 60: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 60/78

Implementing a Stack

Using a one-dimensional array stack[MAX_STACK_SIZE]

#define MAX_STACK_SIZE 100

typedef struct {

int key;

} element;

element stack[MAX_STACK_SIZE];

int top = -1;

Structure element consists of only a key field, and we can add

fields to or modify to meet the requirements of the application

Networking Laboratory 60/78

Page 61: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 61/78

Push and Pop

Pushvoid push(int *ptop, element item) {

if (*ptop >= MAX_STACK_SIZE - 1) {

stack_full();

return;

}

stack[++*ptop] = item;

}

Pop

element pop(int *ptop) {

if (*ptop == -1)

return stack_empty();

return stack[(*ptop)--];

}

Networking Laboratory 61/78

push(&top, item)

pop(&top, item)

Page 62: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 62/78

Queue Abstract Data Type

ADT queue First-In-First-Out (FIFO)

Ordered list

All insertions are made at one end called “rear”

All deletions are made at the other end called “front”

Inserting and deleting elements in queue

rearA

rearB

A

rearC

B

A

rearD

C

B

A

rearD

C

B

front

frontfrontfrontfrontfront

& rear

A

Page 63: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 63/78

Implementing a Queue

A one-dimensional array, and two variables: front and

rear

#define MAX_QUEUE_SIZE 100

typedef struct {

int key;

/* other fields */

} element;

element queue[MAX_QUEUE_SIZE];

int rear = -1;

int front = -1;

Networking Laboratory 63/78

Page 64: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 64/78

Add and Delete

Add to a queue

void addq(int *prear, element item) {

if (*prear == MAX_QUEUE_SIZE - 1) {

queue_full();

return;

}

queue[++*prear] = item;

}

Delete from a queueelement deleteq(int *pfront, int rear) {

if (*pfront == rear)

return queue_empty();

return queue[++*front];

}

Networking Laboratory 64/78

In deleteq()

rear is used

to check for

an empty

queue

addq(&rear, item)

deleteq(&front, rear)

Page 65: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 65/78

Circular Queues (1/3)

More efficient queue representation

Regarding the array queue[MAX_QUEUE_SIZE] as circular

Initially front and rear to 0 rather than -1

The front index always points one position counterclockwise

from the first element in the queue

The rear index points to the current end of the queue

Networking Laboratory 65/78

Page 66: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 66/78

Circular Queues (2/3)

Empty queue

Networking Laboratory 66/78

[1]

[2] [3]

[4]

[0] [5]

front = 0

rear = 0

[1]

[2] [3]

[4]

[0] [5]

front = 0

rear = 3

J3

J1

J2

Empty and nonempty circular queues

Page 67: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 67/78

Circular Queues (3/3)

Full queue

Networking Laboratory 67/78

[1]

[2] [3]

[4]

[0] [5]

front = 0

rear = 5

[1]

[2] [3]

[4]

[0] [5]

front = 4

rear = 3

J9

J7

J8

J1

J2 J3

J4

J5 J6 J5

Full circular queues

Page 68: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 68/78

Implementing Insertions and Deletions

Use modulus operator

Circular rotation of the rear

*rear = (*rear + 1) % MAX_QUEUE_SIZE

Circular rotation of the front

*front = (*front + 1) % MAX_QUEUE_SIZE;

Networking Laboratory 68/78

Page 69: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 69/78

Add to a Circular Queue

Add an item

void addq(int front, int *rear, element item)

{

*rear = (*rear + 1) % MAX_QUEUE_SIZE;

if (front == *rear) {

queue_full(rear);

/* reset rear and print error */

return;

}

queue[*rear] = item;

}

rotate rear before we place the item in queue[rear]

Networking Laboratory 69/78

addq(front, &rear, item)

Page 70: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 70/78

Delete from a Circular Queue

Delete an item

element deleteq(int *front, int rear)

{

element item;

if (*front == rear)

return queue_empty();

/* queue_empty returns an error key */

*front = (*front + 1) % MAX_QUEUE_SIZE;

return queue[*front];

}

Networking Laboratory 70/78

deleteq(&front, rear)

Page 71: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 71/78

Networking Laboratory 71/78

Lists

Page 72: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 72/78

Singly Linked Lists

Compose of data part and link part

Link part contains address of the next element in a list

Non-sequential representations

Size of the list is not predefined

Dynamic storage allocation and deallocation

Networking Laboratory 72/78

bat satcat vat NULL

ptr

Page 73: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 73/78

Insertion of Singly Linked Lists

To insert the word mat between cat and sat

1) Get a currently unused node (paddr)

2) Set paddr’s data to mat

3) Set paddr’s link to point to the address found in the link of the

node cat

4) Set the link of the node cat to point to paddr

Networking Laboratory 73/78

bat satcat vat NULL

ptr

mat

Page 74: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 74/78

Deletion of Singly Linked Lists

To delete mat from the lists

1) Find the element that immediately precedes mat, which is cat

2) Set its link to point to mat’s link

- No data movement in insert and delete operation

Networking Laboratory 74/78

bat satcat vat NULL

ptr

mat

Page 75: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 75/78

Doubly Linked Lists (1/2)

Problems of singly linked lists

Move to only one way direction

Hard to find the previous node

Hard to delete the arbitrary node

Doubly linked circular lists

Doubly lists + circular lists

Allow two links

Two way direction

Networking Laboratory 75/78

Page 76: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 76/78

Doubly Linked Lists (2/2)

Doubly linked circular lists with head node

Networking Laboratory 76/78

head node

llink item rlink

Page 77: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 77/78

Insertion of Doubly Linked Lists

Insertion into doubly linked circular lists

Networking Laboratory 77/78

void dinsert(node_ptr node,node_ptr newnode) {

/* insert newnode to the right of node */

newnode->llink = node;

newnode->rlink = node->rlink;

node->rlink->llink = newnode;

node->rlink = newnode;

}

time: O(1)

1.

2.

3.

4.

dinsert(node, newnode)

newnode

node

341 2

node

newnode

1

23

4

newnode

node

Page 78: C programming and Data Structures Overviewmonet.skku.edu/wp-content/uploads/2020/03/Algorithm_00.pdf · 2020-03-08 · Data Structures Overview Sungkyunkwan University Hyunseung Choo

Intelligent Networking Laboratory H.CHOO 78/78

Deletion of Doubly Linked Lists

Deletion from a doubly linked circular lists

Networking Laboratory 78/78

void ddelete(node_ptr node, node_ptr deleted) {

/* delete from the doubly linked list */

if (node == deleted)

printf(“Deletion of head node not permitted.\n”);

else {

deleted->llink->rlink = deleted->rlink;

deleted->rlink->llink = deleted->llink;

free(deleted);

} }

time: O(1)

node

deleted

node

ddelete(node, deleted)

deleted

node