20
REVIEW 01205216 FINAL 2/2556 P’Nut EE43 E67 1 Subject 9. Dynamic Memory Allocation 10. Structured Data Type 11. Functions 12. Lists 13. Stacks 14. Queues 15. Trees 2 Dynamic Memory Allocation 3 Memory Allocation Static memory allocation - จองหน่วยความจําอยู่ในส วน Run-time stack - ต้องจองหน่วยความจําที่มีขนาดแน่นอน - ไม่สามารถเปลี่ยนแปลงขนาดได้ขณะรันโปรแกรม Dynamic memory allocation (DMA) - จองหน่วยความจําอยู่ในส วน Heap - ไม่จําเป็ นต้องจองหน่วยความจําที่มีขนาดแน่นอน - สามารถเปลี่ยนแปลงขนาดได้ขณะรันโปรแกรม 4

ติวภาษา C final no animation_4p_complete

Embed Size (px)

DESCRIPTION

สรุปเนื้อหาไฟนอลวิชา 01205216 Computer Programming for Electrical Engineer (C & Data Structure) By P'Nut EE43 E67 KU71

Citation preview

Page 1: ติวภาษา C final no animation_4p_complete

REVIEW 01205216 FINAL 2/2556P’Nut EE43 E67

1 Subject

9. Dynamic Memory Allocation10. Structured Data Type11. Functions12. Lists13. Stacks14. Queues15. Trees

2

Dynamic Memory Allocation3

Memory Allocation

Static memory allocation- จองหน่วยความจําอยูใ่นสว่น Run-time stack- ตอ้งจองหน่วยความจําทีม่ขีนาดแน่นอน- ไมส่ามารถเปลีย่นแปลงขนาดไดข้ณะรันโปรแกรม

Dynamic memory allocation (DMA)- จองหน่วยความจําอยูใ่นสว่น Heap- ไมจ่ําเป็นตอ้งจองหน่วยความจําทีม่ขีนาดแน่นอน- สามารถเปลีย่นแปลงขนาดไดข้ณะรันโปรแกรม

4

Page 2: ติวภาษา C final no animation_4p_complete

Static Memory Allocation

float a[2][3]; 

เป็นการจองหน่วยความจําตดิกนัโดยตอ้งกําหนดจํานวนสมาชกิในแตล่ะมติิใหเ้ป็นคา่คงที่

int m, n;scanf("%dx%d", &m, &n);float a[m][n]; 

ตามมาตรฐาน ANSI-Cไมส่ามารถทําได ้เพราะเป็นการจองหน่วยความจําขณะรันโปรแกรม

5

Dynamic Memory Allocation

float *p;int n;scanf("%d", &n);p = (float*)malloc(n*sizeof(float));

เมือ่ป้อน3

Ex1 : สรา้ง Array 1 มติ ิโดยใชค้ําสัง่ malloc() (อยูใ่น stdlib.h)

AE50h

6

Dynamic Memory Allocation

Ex2 : สรา้ง Array 2 มติ ิโดยใชค้ําสัง่ malloc()

float **ptp;int m, n, i;scanf("%dx%d", &m, &n);ptp = (float**)malloc(m*sizeof(float*));for(i=0; i<n; i++)

ptp[i] = (float*)malloc(n*sizeof(float));

เมือ่ป้อน2x3

AE86h

FB00h

FB40h

7

Dynamic Memory Allocation

จากตวัอยา่งเมือ่สกัครู ่ตอ้งการใหแ้ตล่ะแถว มจีํานวนหลกัไมเ่ทา่กนั ทําอยา่งไรด?ี??

float **ptp;int m, n, i;scanf("%d", &m);ptp = (float**)malloc(m*sizeof(float*));for(i=0; i<n; i++){

scanf("%d", &n);ptp[i] = (float*)malloc(n*sizeof(float));

}

เมือ่ป้อน234

8

Page 3: ติวภาษา C final no animation_4p_complete

Memory Leak

ปัญหาคอื เมือ่จองหน่วยความจําใหม ่ตําแหน่งหน่วยความจําเดมิจะคงสถานะไมว่า่งตอ่ไป

จาก Ex1

/* โค้ดจาก Ex1 */p = (float*)malloc(4*sizeof(float));

CE48h

9

Memory Leak

แกป้ัญหาโดยใชค้ําสัง่ free()

/* โค้ดจาก Ex1 */ free(p);p = (float*)malloc(4*sizeof(float));

CE48h

10

Memory Leak

การใชค้ําสัง่ free() สําหรับ Array 2 มติ ิตอ่จาก Ex2

/* โค้ดจาก Ex2 */ free(ptp); ถกูตอ้งหรอืไม?่??

AE86h

FB40h

FB00h

11

นีค่อืตวัอยา่งทีผ่ดิ หา้มทํา!!!

Memory Leak

/* โค้ดจาก Ex2 */ for(i=0; i<m; i++)

free(ptp[i]);free(ptp);

FB40h

FB00h

การใชค้ําสัง่ free() สําหรับ Array 2 มติ ิตอ่จาก Ex2

12

Page 4: ติวภาษา C final no animation_4p_complete

Structured Data Type13

Introduction

Struct คอื โครงสรา้งขอ้มลูทีเ่ป็นชดุของขอ้มลูมากกวา่ 1 ชนดิ ซึง่เปรยีบเสมอืนชนดิขอ้มลูทีเ่รานยิามขึน้ใหม่

ตวัอยา่งเชน่ จํานวนเชงิซอ้น

jXRZ struct complex{

float real;float imag;

};

struct{float real;float imag;

}complex;

typedef struct{float real;float imag;

}complex;

typedef struct a{float real;float imag;

}complex;

อยา่ลมื “;” เด็ดขาด

14

Declaration

struct complex{float real;float imag;

};int main(){

struct complex z1, z2, z3;...return 0;

}

struct{float real;float imag;

}complex;int main(){

struct complex z1, z2, z3;...return 0;

}

การนยิาม struct ในลกัษณะนี ้เมือ่ประกาศตวัแปรตอ้งมคีําวา่ struct นําหนา้ ซึง่ไมส่ะดวกเทา่ไรนัก

15

Declaration

typedef struct{float real;float imag;

}complex;int main(){

complex z1, z2, z3;...return 0;

}

typedef struct a{float real;float imag;

}complex;int main(){

complex z1, z2, z3;...return 0;

}

เพือ่ความสะดวก เราสามารถละคําวา่ struct ตอนประกาศตวัแปรโดยใช ้typedef ตอนนยิาม struct

หลงัจากนี ้จะขอพดูถงึเฉพาะการนยิาม struct โดยใช ้typedef

16

Page 5: ติวภาษา C final no animation_4p_complete

Accessing

complex z1, z2;z1.real = 2;z1.imag = 3;z2.real = 5;z2.imag = 7;

การเขา้ถงึแตล่ะสว่นของ struct วธิกีารคอื เตมิจดุหลงัชือ่แลว้ตามดว้ยชือ่ของสมาชกิของ struct

คําถามเพิม่เตมิ

complex z3;z3 = z1+z2;z3 = z1;

ถกูตอ้งหรอืไม?่??ถกูตอ้งหรอืไม?่??

17

Initialization

complex z1={2,3}, z2={5,7};

เราสามารถกําหนดคา่เริม่ตน้ใหก้บั struct ไดค้ลา้ยๆ กบั Arrayนั่นคอื ตอ้งกําหนดคา่เริม่ตน้เมือ่ประกาศตวัแปรเทา่นัน้

โดยมวีธิกีารดงันี้

ซ ึง่ผลลพัธท์ีไ่ดเ้หมอืนกบัตวัอยา่งเมือ่สกัครู่

คําถามเพิม่เตมิcomplex z1, z2;z1 = {2,3};z2 = {5,7};

ถกูตอ้งหรอืไม?่??

18

นีค่อืตวัอยา่งทีผ่ดิ หา้มทํา!!!

Array of Struct

เนือ่งจากการใชง้านของ struct มคีวามคลา้ยคลงึกบัตวัแปร ดงันัน้จงึสามารถประกาศ array ของ struct ไดเ้ชน่เดยีวกบัตวัแปร

complex z[2];

z[0].real = 2;z[0].imag = 3;z[1].real = 5;z[2].imag = 7;

การเขา้ถงึแตล่ะสว่นของ struct ทีเ่ป็นสมาชกิของ arrayวธิกีารคอื ใหเ้ตมิจดุหลงัการ index แลว้ตามดว้ยชือ่ของสมาชกิของ struct

19

Pointer to Struct

เราสามารถประกาศ pointer ทีช่ ีไ้ปยัง struct ไดอ้กีดว้ย

complex z;complex *q;q = &z;(*q).real = 2;(*q).imag = 3;

จะพบวา่ การ derefencing แบบดงักลา่ว ไมค่อ่ยสะดวกนักดงันัน้ เพือ่ความสะดวก เราสามารถใชส้ญัลกัษณ ์-> แทนได ้

complex z;complex *q;q = &z;q‐>real = 2;q‐>imag = 3;

20

Page 6: ติวภาษา C final no animation_4p_complete

Pointer to Struct

นอกจากนัน้ เราสามารถใชค้ําสัง่ malloc() กบั pointer ทีช่ ีไ้ปยัง struct ไดอ้กีดว้ย

complex *q;int n;scanf("%d", &n);q = (complex*)malloc(n*sizeof(complex));

แตถ่า้หากเรานยิาม struct โดยไมไ่ดใ้ชค้ําสัง่ typedefตอนทํา type casting ตอ้งมคีําวา่ struct นําหนา้ชนดิของมันดว้ย

struct complex *q;int n;scanf("%d", &n);q = (struct complex*)malloc(n*sizeof(struct complex));

21

Struct with Array & Pointer

การนยิาม struct ทีม่สีมาชกิเป็น array

typedef struct{float item[8];int top;

}stack;

typedef struct{float item[8];int front;int rear;

}queue;

การนยิาม struct ทีม่สีมาชกิเป็น pointer

typedef struct{float *data;int n;

}sth;

22

Nested Struct

typedef struct{unsigned int day;unsigned int month;unsigned int year;

}date;typedef struct{

char title[40];char artist[40];date create;

}record;

typedef struct{char number[5];char street[31];char city[31];

}address;typedef struct{

char ID[5];char name[31];address *addr;

}employee;

การนยิาม struct ทีม่สีมาชกิเป็น struct

23

Nested Struct

typedef struct _node{float data;node *next;

}node;

typedef struct _node{float data;struct _node *next;

}node;

typedef struct _node{float data;_node *next;

}node;

นอกจากนัน้ เราสามารถนยิาม structทีม่สีมาชกิเป็น struct ตวัเองไดอ้กีดว้ย

แบบไหนถกูตอ้ง???

24

Page 7: ติวภาษา C final no animation_4p_complete

Functions25

Function & Pointer

ฟังกช์ัน่ทีค่นืคา่เป็น pointer

float *GetAddr(){float *var;var = (float*)malloc(sizeof(float));return var;

}

เนือ่งจากมกีารใชค้ําสัง่ malloc ในฟังกช์ัน่ เมือ่ออกจากฟังกช์ัน่ GetAddr() แลว้ ตําแหน่งหน่วยความจําทีถ่กูจองจะยังคงสถานะไมว่า่งตอ่ไป

26

Function & Pointer

void swap(int *x, int *y){int temp = *x;*x = *y;*y = temp;

}int main(){

int a=2, b=3;printf("a=%d, b=%d\n", a, b);swap(&a, &b);printf("a=%d, b=%d\n", a, b);return 0;

}

หากมกีารเขา้ถงึขอ้มลูบนตําแหน่งที ่pointer ชีอ้ยู ่อยา่ลมื dereferencing ดว้ย

สว่นนีค้อื actual parameter

สว่นนีค้อื formal parameter

ฟังกช์ัน่ทีม่กีารสง่ผา่นคา่เป็น pointer

27

Passing Parameter

Pass by value- คอืการ copy คา่ของ actual parameter

ใหก้บั formal parameter - คา่ของ actual parameter จะไมเ่ปลีย่นคา่ตาม

formal parameter

Pass by reference- คอืการ copy ตาํแหนง่หนว่ยความจาํของ actual parameter

ใหก้บั formal parameter - ตอ้งกําหนดให ้formal parameter เป็น pointer

ทีช่ ีไ้ปยังตวัแปรชนดินัน้ๆ- คา่ของ actual parameter จะเปลีย่นคา่ตาม

formal parameter

28

Page 8: ติวภาษา C final no animation_4p_complete

Passing Parameter

ตวัอยา่งการ Pass by value ของตวัแปร

void swap(int x, int y){int temp = x;x = y;y = temp;

}int main(){

int a=2, b=3;swap(a, b);return 0;

}

2

3

3

2

2

29

Passing Parameter

ตวัอยา่งการ Pass by reference ของตวัแปร

void swap(int *x, int *y){int temp = *x;*x = *y;*y = temp;

}int main(){

int a=2, b=3;swap(&a, &b);return 0;

}

3

2

2

7400h

7404h

30

Passing Parameter

ตวัอยา่งการ Pass by value ของ pointer

void swap(int *x, int *y){int *temp;temp = x;x = y;y = temp;

}int main(){

int a=2, b=3, *p, *q;p = &a;q = &b;swap(p, q);return 0;

}

2

3

7400h

7404h

7400h

7400h

7404h

31

Passing Parameter

ตวัอยา่งการ Pass by reference ของ pointer

void swap(int **x, int **y){int *temp;temp = *x;*x = *y;*y = temp;

}int main(){

int a=2, b=3, *p, *q;p = &a;q = &b;swap(&p, &q);return 0;

}

2

3

7400h

740Ch

7408h

7404h

7400h

32

Page 9: ติวภาษา C final no animation_4p_complete

Function & Array

void bubblesort(float *arr, int length){int i, j, sw;float temp;for(i=0; i<length‐1; i++){ 

for(sw=1,j=0; j<length‐1‐i; j++)if(arr[j]>arr[j+1]){  

                sw = 0;temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;

}if(sw)

break;}

}

ตวัอยา่งฟังกช์ัน่ทีร่ับคา่เป็น array 1 มติิ

33

Function & Array

void PrintArray(float arr[][3], int rows){int i, j;for(i=0; i<rows; i++){

for(j=0; j<3; j++)printf("%8.2f", arr[i][j]);

printf("\n");}

}int main(){

float a[2][3]={1,2,3,4,5,6};int a_row = sizeof(a)/sizeof(*a);PrintArray(a, a_row);return 0;

}

ตวัอยา่งฟังกช์ัน่ทีร่ับคา่เป็น array 2 มติิ

34

Lists35

Introduction to Abstract Data Types

โครงสรา้งขอ้มลู (Data Structures) คอื วธิกีารจัดเก็บขอ้มลูโดยคํานงึถงึวธิกีารจัดเก็บและเรยีกใชง้านไดอ้ยา่งมปีระสทิธภิาพ

การจัดเก็บขอ้มลูแบง่ไดเ้ป็น 2 ลกัษณะ คอื1. แบบเชงิเสน้ (Linear Data Structure)- Array- List- Stack- Queue

2. แบบไมเ่ชงิเสน้ (Non-Linear Data Structure)- Graph- Tree

36

Page 10: ติวภาษา C final no animation_4p_complete

Introduction to Abstract Data Types

กอ่นหนา้นี ้ไดก้ลา่วถงึเรือ่ง array ซึง่เก็บขอ้มลูชนดิเดยีวกนัโดยตอ้งจองหน่วยความจําของแตล่ะสมาชกิตดิกนัทัง้หมด

ขอ้จํากดัของ array- ผูใ้ชต้อ้งระบขุนาดพืน้ทีห่น่วยความจํากอ่นเรยีกใชง้าน- มขีนาดคงที ่ไมส่ามารถเปลีย่นแปลงไดห้ลงัประกาศใชง้าน- ไมเ่หมาะสําหรับการลบ ยา้ย และแทรกขอ้มลู

เราสามารถแกป้ัญหาเหลา่นีไ้ด ้โดยสรา้งแบบชนดิขอ้มลูนามธรรม (Abstract Data Type : ADT) แบบตา่งๆ เชน่ List, Stack, Queue,Tree ฯลฯ เพือ่ลดขอ้จํากดัเหลา่นัน้ลงได ้

หลงัจากนัน้จงึคอ่ยมากําหนด operations สําหรับ ADT เหลา่นัน้

37

Introduction to Lists

Linked List เป็นโครงสรา้งขอ้มลู ทีข่อ้มลูแตล่ะตวัจะเชือ่มโยงผา่นตวัแปร pointer ทําใหด้เูหมอืนเป็นสายขอ้มลูเรยีงตอ่กนัโดยไมต่อ้งจองหน่วยความจําของแตล่ะสมาชกิตดิกนั

โดยขอ้มลูแตล่ะตวัจะถกูเก็บอยูใ่นโหนด (Node)ซึง่ประกอบดว้ย 2 สว่นคอื- ขอ้มลู (Data)- Pointer ทีช่ ีไ้ปยังโหนดถัดไป (Next)

typedef float datatype;typedef struct _node{

datatype data;struct _node *next;

}node;

node

data next

38

Operations

Operation ของ linked list ประกอบดว้ย

- GetNode การสรา้งโหนดขึน้หนึง่โหนด- Traverse การทอ่งไปในลสิต์- SearchInfo การคน้หาขอ้มลูทีเ่ก็บไว ้- InsertFirst การแทรกโหนดเขา้ตน้ลสิต์- InsertAfter การแทรกโหนดหลงัโหนดใดๆ- InsertLast การแทรกโหนดเขา้ทา้ยลสิต์- DeleteFirst การลบโหนดออกจากตน้ลสิต์- DeleteAfter การลบโหนดหลงัโหนดใดๆ- DeleteLast การลบโหนดออกจากทา้ยลสิต์

39

node *GetNode(datatype item){node *temp;temp = (node*)malloc(sizeof(node));if(temp!=NULL){

temp‐>data = item;temp‐>next = NULL;

}return temp;

}

GetNode การสรา้งโหนดขึน้หนึง่โหนด

Operations40

Page 11: ติวภาษา C final no animation_4p_complete

void Traverse(node *list){if(list==NULL){

printf("List is empty.\n");return;

}node *p;int i = 1;p = list;while(p!=NULL){

printf("Item #%d is %f with address %p\n", i, p‐>data, p);p = p‐>next;i++;

}}

Traverse การทอ่งไปในลสิต์

Operations41

Operations

node *SearchInfo(node *list, datatype item){if(list==NULL)

return NULL;node *p;p = list;while((p!=NULL)&&(p‐>data!=item))

p = p‐>next;return p;

}

SearchInfo การคน้หาขอ้มลูทีเ่ก็บไว ้

42

Operations

InsertFirst การแทรกโหนดเขา้ตน้ลสิต์void InsertFirst(node **list, datatype item){

node *newnode;newnode = GetNode(item);if(newnode==NULL)

return;newnode‐>next = *list;*list = newnode;

}

43

Operations

void InsertAfter(node *list, datatype item){if(list==NULL)

return;node *newnode;newnode = GetNode(item);if(newnode==NULL)

return;newnode‐>next = list‐>next;list‐>next = newnode;

}

InsertAfter การแทรกโหนดหลงัโหนดใดๆ

44

Page 12: ติวภาษา C final no animation_4p_complete

Operations

void InsertLast(node **list, datatype item){node *newnode, *p;newnode = GetNode(item);if(newnode==NULL)

return;p = *list;if(*list!=NULL){

while(p‐>next!=NULL)p = p‐>next;

p‐>next = newnode;}else

*list = newnode;}

InsertLast การแทรกโหนดเขา้ทา้ยลสิต์

45

Operations

void DeleteFirst(node **list){if(*list==NULL)

return;node *p;p = *list;*list = (*list)‐>next;free(p);

}

DeleteFirst การลบโหนดออกจากตน้ลสิต์

46

Operations

void DeleteAfter(node *list){if(list==NULL)

return;node *p;p = list‐>next;list‐>next = p‐>next;free(p);

}

DeleteAfter การลบโหนดหลงัโหนดใดๆ

47

Operations

void DeleteLast(node **list){if(*list==NULL)

return;node *prenode, *currnode;prenode = *list;currnode = *list;while(currnode‐>next!=NULL){

prenode = currnode;currnode = currnode‐>next;

}if(prenode==currnode)

free(list);else{

prenode‐>next = NULL;free(currnode);

}}

DeleteLast การลบโหนดออกจากทา้ยลสิต์

48

Page 13: ติวภาษา C final no animation_4p_complete

Double Linked List

ขอ้จํากดัของ linked list คอื สามารถเขา้ถงึขอ้มลูไดเ้พยีงทศิทางเดยีวคอืจากโหนดแรกของ linked list เทา่นัน้

ดงันัน้ หากตอ้งการเขา้ถงึขอ้มลูไดท้ัง้สองทศิทาง จงึตอ้งสรา้งโหนดทีม่ ีpointer ชีไ้ปยังโหนดดา้นซา้ยและดา้นขวา

อยา่งไรก็ตาม การเพิม่หรอืลบขอ้มลูก็จะมคีวามซบัซอ้นมากขึน้

typedef struct _dbnode{datatype data;struct _dbnode *rightnode;struct _dbnode *leftnode;

}dbnode;

dbnode

data rightnodeleftnode

49

Stacks50

Introduction to Stacks

Stack เป็นโครงสรา้งขอ้มลูทีน่ําขอ้มลูนํามาวางซอ้นกนัซึง่เพิม่และลบขอ้มลูทีต่ําแหน่งบนสดุเทา่นัน้

Stack เป็น ADT ทีม่ลีกัษณะ LIFO (Last-In-First-Out) คอืขอ้มลูทีถ่กูเพิม่เขา้มาทหีลงัจะถกูเรยีกออกไปใชง้านกอ่น

51

Operations

Operation ของ stack ประกอบดว้ย

- InitializeStack กําหนดใหส้แต็กวา่งเปลา่ไมม่ขีอ้มลู- IsStackEmpty ใชต้รวจสอบสแต็กวา่งหรอืไม่- IsStackFull ใชต้รวจสอบสแต็กเต็มหรอืไม่- Push ใชใ้นการนําขอ้มลูเก็บลงในสแต็ก- Pop ใชใ้นการนําขอ้มลูออกจากสแต็ก- Top ใชใ้นการสอบถามขอ้มลูทีอ่ยูบ่นสดุของสแต็ก

52

Page 14: ติวภาษา C final no animation_4p_complete

Stack from Array

การสรา้ง stack ดว้ย array

#define MAXSTACK 16typedef float datatype;typedef struct _Stack{

datatype data[MAXSTACK];int top;

}Stack;int main(){

Stack *st = (Stack*)malloc(sizeof(Stack));......return 0;

}

53

Stack from Array - Operations

InitializeStack กําหนดใหส้แต็กวา่งเปลา่ไมม่ขีอ้มลูvoid InitializeStack(Stack *st){

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

st‐>data[i] = 0;st‐>top = ‐1;

}

IsStackEmpty ใชต้รวจสอบสแต็กวา่งหรอืไม่int IsStackEmpty(Stack *st){

return st‐>top == ‐1;}

IsStackFull ใชต้รวจสอบสแต็กเต็มหรอืไม่int IsStackFull(Stack *st){

return st‐>top == MAXSTACK‐1;}

54

Stack from Array - Operations

Push ใชใ้นการนําขอ้มลูเก็บลงในสแต็กint Push(Stack *st, datatype item){

if(IsStackFull(st))return 0; // Stack overflow

st‐>data[++(st‐>top)] = item;return 1;

}

Pop ใชใ้นการนําขอ้มลูออกจากสแต็กint Pop(Stack *st, datatype *item){

if(IsStackEmpty(st))return 0; // Stack underflow (empty stack)

*item = st‐>data[(st‐>top)‐‐];return 1;

}

55

Stack from Array - Operations

Top ใชใ้นการสอบถามขอ้มลูทีอ่ยูบ่นสดุของสแต็กint Top(Stack *st, datatype *item){

if(IsStackEmpty(st))return 0; // Stack underflow (empty stack)

*item = st‐>data[st‐>top];return 1;

}

56

Page 15: ติวภาษา C final no animation_4p_complete

Stack from Pointer

ขอ้จํากดัของการสรา้ง stack ดว้ย array คอื มโีอกาสเกดิ stack overflow ได ้

ดงันัน้ เราจงึสรา้ง stack ดว้ย linked list แบบมหีวัเพือ่หลกีเลีย่งปัญหาเหลา่นัน้

typedef float datatype;typedef struct _stack{

int count;node *top;

}stack;

57

Stack from Pointer - Operations

CreateStack ใชใ้นการสรา้งสแต็กจากพอยนเ์ตอร์stack *CreateStack(){

stack *st;st = (stack*)malloc(sizeof(stack));if(st!=NULL){

st‐>count = 0;st‐>top = NULL;

}return st;

}

IsStackEmpty ใชต้รวจสอบสแต็กวา่งหรอืไม่int IsStackEmpty(stack *st){

return st‐>count <= 0;}

58

Stack from Pointer - Operations

Push ใชใ้นการนําขอ้มลูเก็บลงในสแต็กint Push(stack *st, datatype item){

if(st==NULL)return 0;

node *newnode;newnode = GetNode(item);if(newnode==NULL)

return 0;newnode‐>next = st‐>top;st‐>top = newnode;(st‐>count)++;return 1;

}

59

Stack from Pointer - Operations

Pop ใชใ้นการนําขอ้มลูออกจากสแต็กint Pop(stack *st, datatype *item){

if((st==NULL)||IsStackEmpty(st))return 0;

node *temp;*item = st‐>top‐>data;temp = st‐>top;st‐>top = temp‐>next;free(temp);(st‐>count)‐‐;return 1;

}

60

Page 16: ติวภาษา C final no animation_4p_complete

Stack from Pointer - Operations

Top ใชใ้นการสอบถามขอ้มลูทีอ่ยูบ่นสดุของสแต็กint Top(stack *st, datatype *item){

if((st==NULL)||IsStackEmpty(st))return 0;

*item = st‐>top‐>data;return 1;

}

61

Queues62

Introduction to Queues

Queue เป็นโครงสรา้งขอ้มลูทีน่ําขอ้มลูนํามาวางตอ่กนัซึง่เพิม่ขอ้มลูทีป่ลายดา้นหนึง่ทีเ่รยีกวา่ทา้ยควิ (rear)และลบขอ้มลูทีป่ลายดา้นหนึง่ทีเ่รยีกวา่หวัควิ (front)

Queue เป็น ADT ทีม่ลีกัษณะ FIFO (First-In-First-Out) คอืขอ้มลูทีถ่กูเพิม่เขา้มากอ่นจะถกูเรยีกออกไปใชง้านกอ่น

63

Operations

Operation ของ queue ประกอบดว้ย

- InitializeQueue กําหนดใหค้วิวา่งเปลา่ไมม่ขีอ้มลู- IsQueueEmpty ใชต้รวจสอบควิวา่งหรอืไม่- IsQueueFull ใชต้รวจสอบควิเต็มหรอืไม่- Enqueue ใชใ้นการนําขอ้มลูเก็บลงในควิ- Dequeue ใชใ้นการนําขอ้มลูออกจากควิ- Front ใชใ้นการสอบถามขอ้มลูทีอ่ยูห่นา้สดุของควิ- Rear ใชใ้นการสอบถามขอ้มลูทีอ่ยูท่า้ยสดุของควิ

64

Page 17: ติวภาษา C final no animation_4p_complete

Queue from Array

การสรา้ง queue ดว้ย array

#define MAXQUEUE 16typedef float datatype;typedef struct _Queue{

datatype data[MAXQUEUE];int front;int rear;

}Queue;int main(){

Queue *q = (Queue*)malloc(sizeof(Queue));......return 0;

}

65

Queue from Array - Operations

InitializeQueue กําหนดใหค้วิวา่งเปลา่ไมม่ขีอ้มลูvoid InitializeQueue(Queue *q){

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

q‐>data[i] = 0;q‐>front = 0;q‐>rear = 0;

}

IsQueueEmpty ใชต้รวจสอบควิวา่งหรอืไม่int IsQueueEmpty(Queue *q){

return q‐>front == q‐>rear;}

IsQueueFull ใชต้รวจสอบควิเต็มหรอืไม่int IsQueueFull(Queue *q){

return q‐>front == (q‐>rear+1)%MAXQUEUE;}

66

Queue from Array - Operations

Enqueue ใชใ้นการนําขอ้มลูเก็บลงในควิint Enqueue(Queue *q, datatype item){

if(IsQueueFull(q))return 0; // Queue overflow

q‐>data[q‐>rear] = item;q‐>rear = (q‐>rear+1)%MAXQUEUE;return 1;

}

Dequeue ใชใ้นการนําขอ้มลูออกจากควิint Dequeue(Queue *q, datatype *item){

if(IsQueueEmpty(q))return 0; // Queue underflow (empty queue)

*item = q‐>data[q‐>front];q‐>front = (q‐>front+1)%MAXQUEUE;return 1;

}

67

Queue from Array - Operations

Front ใชใ้นการสอบถามขอ้มลูทีอ่ยูห่นา้สดุของควิint Front(Queue *q, datatype *item){

if(IsQueueEmpty(q))return 0; // Queue underflow (empty queue)

*item = q‐>data[q‐>front];return 1;

}

Rear ใชใ้นการสอบถามขอ้มลูทีอ่ยูท่า้ยสดุของควิint Rear(Queue *q, datatype *item){

if(IsQueueEmpty(q))return 0; // Queue underflow (empty queue)

*item = q‐>data[(q‐>rear+MAXQUEUE‐1)%MAXQUEUE];return 1;

}

68

Page 18: ติวภาษา C final no animation_4p_complete

Queue from Pointer

ขอ้จํากดัของการสรา้ง queue ดว้ย array คอื มโีอกาสเกดิ queue overflow ได ้และตอ้งเสยีพืน้ทีห่น่วยความจํา 1 ชอ่งโดยเปลา่ประโยชน์

ดงันัน้ เราจงึสรา้ง queue ดว้ย linked list แบบมหีวัเพือ่หลกีเลีย่งปัญหาเหลา่นัน้

typedef float datatype;typedef struct _queue{

int count;node *front;node *rear;

}queue;

69

Queue from Pointer - Operations

CreateQueue ใชใ้นการสรา้งควิจากพอยนเ์ตอร์queue *CreateQueue(){

queue *q;q = (queue*)malloc(sizeof(queue));if(q!=NULL){

q‐>count = 0;q‐>front = NULL;q‐>rear = NULL;

}return q;

}

IsQueueEmpty ใชต้รวจสอบควิวา่งหรอืไม่int IsQueueEmpty(queue *q){

return q‐>count <= 0;}

70

Queue from Pointer - Operations

Enqueue ใชใ้นการนําขอ้มลูเก็บลงในควิint Enqueue(queue *q, datatype item){

if(q==NULL)return 0;

node *newnode = GetNode(item);if(newnode==NULL)

return 0;if(IsQueueEmpty(q)){

q‐>front = q‐>rear = newnode;(q‐>count)++;

}else{

q‐>rear‐>next = newnode;q‐>rear = newnode;(q‐>count)++;

}return 1;

}

71

Queue from Pointer - Operations

Dequeue ใชใ้นการนําขอ้มลูออกจากควิint Dequeue(queue *q, datatype *item){

if(q==NULL||IsQueueEmpty(q))return 0;

node *temp;*item = q‐>front‐>data;temp = q‐>front;q‐>front = q‐>front‐>next;(q‐>count)‐‐;free(temp);return 1;

}

72

Page 19: ติวภาษา C final no animation_4p_complete

Queue from Pointer - Operations

Front ใชใ้นการสอบถามขอ้มลูทีอ่ยูห่นา้สดุของควิint Front(queue *q, datatype *item){

if(q==NULL||IsQueueEmpty(q))return 0;

*item = q‐>front‐>data;return 1;

}

Rear ใชใ้นการสอบถามขอ้มลูทีอ่ยูท่า้ยสดุของควิint Rear(queue *q, datatype *item){

if(q==NULL||IsQueueEmpty(q))return 0;

*item = q‐>rear‐>data;return 1;

}

73

Trees74

Introduction to Trees

คณุสมบตัขิอง Tree คอื- โหนดทีอ่ยูบ่นสดุจะเรยีกวา่ root- Tree จะประกอบดว้ยการเชือ่มกนัของโหนดโดยทกุโหนดในทรสีามารถเดนิทางจาก root โหนด

- โหนดแตล่ะตวัจะมเีพยีง 1 parent ยกเวน้ root โหนดทีจ่ะไมม่ ีparent

- Tree จะไมม่ ีcycle- เสน้ทางจาก root ไปยัง node อืน่ ๆ มไีดเ้พยีงเสน้ทางเดยีว

75

Binary Tree

Binary Tree คอื tree ทีแ่ตล่ะโหนด มลีกูไดไ้มเ่กนิ 2 โหนด

Tree Traversal ม ี3 วธิี- Pre-Order- In-Order- Post-Order

76

Page 20: ติวภาษา C final no animation_4p_complete

Pre-Order- ดําเนนิการที ่root- ดําเนนิการที ่left subtrees แบบ recursive- ดําเนนิการที ่right subtrees แบบ recursive

A B

Binary Tree

C D F G E

77

In-Order- ดําเนนิการที ่left subtrees แบบ recursive- ดําเนนิการที ่root- ดําเนนิการที ่right subtrees แบบ recursive

B A

Binary Tree

F D G C E

78

Post-Order- ดําเนนิการที ่left subtrees แบบ recursive- ดําเนนิการที ่right subtrees แบบ recursive- ดําเนนิการที ่root

B F

Binary Tree

G D E C A

79

Binary Search Tree

Binary Search Tree มคีณุสมบตัดิงันี้- คา่คยีท์กุคา่ ไมซ่ํา้กนั- คา่คยีท์กุคา่ใน left subtree มคีา่นอ้ยกวา่คา่คา่คยีใ์น root- คา่คยีท์กุคา่ใน right subtree มคีา่มากกวา่คา่คา่คยีใ์น root

80