Data Structure & Algorithm Basic Lab – week 3

85
Data Structure & Data Structure & Algorithm Basic Lab Algorithm Basic Lab – week 3 – week 3

Transcript of Data Structure & Algorithm Basic Lab – week 3

Page 1: Data Structure & Algorithm Basic Lab – week 3

Data Structure & Data Structure & Algorithm Basic Lab Algorithm Basic Lab

– week 3– week 3

Page 2: Data Structure & Algorithm Basic Lab – week 3

2

Today's topicsToday's topics• Self referential structure in C• Data structure “single linked LIST”

–Implementation of single linked LIST–Algorithm for scanning data–Algorithm for inserting, deleting

• Data structure “double linked LIST”–Implementation of double linked LIST–Algorithm for scanning data–Algorithm for inserting, deleting

Page 3: Data Structure & Algorithm Basic Lab – week 3

3

Self-Referential StructuresSelf-Referential Structures• One or more of its components is a

pointer to itself.struct list {

char data;struct list *link;};list item1, item2, item3;item1.data=‘a’;item2.data=‘b’;item3.data=‘c’;item1.link=item2;

item2.link=item3; item3.link=NULL;

a b c

Page 4: Data Structure & Algorithm Basic Lab – week 3

4

Implemetation of List in CImplemetation of List in C• “LIST” means data structure that keeps the

information of the location of next element generally.

• The elements of “Single linked LIST” have only next location.

• In C, the pointer is used for the location of the next element.

• Array: We can access any data immediately.• Linked List: We can change the number of data in

it.

NULLroot (or head)

Page 5: Data Structure & Algorithm Basic Lab – week 3

5

Declaration of a Linked ListDeclaration of a Linked Listtypedef ... elementtype;

typedef struct node{ elementtype element;

node* next; }; node* root; node* cur;

typedef ... elementtype;

struct node{ elementtype element;

struct node* next; }; struct node* root; struct node* cur;

Page 6: Data Structure & Algorithm Basic Lab – week 3

6

Memory allocation for an Memory allocation for an elementelement

• We need to allocate a memory bloc for each node (element) via a pointer.

struct node * new;new = (struct node*) malloc(sizeof(struct node));new->element = …new->next = null;

• new->addr means (*new).addr.• “pointer variable for record structure” ->

“member name”

Page 7: Data Structure & Algorithm Basic Lab – week 3

7

Question 3-1Question 3-1• We are now designing “address list”

for mobile phones.• You must declare a record structure

that can keep a name, a phone number, and a e-mail address at least.

• And you must make the program which can deals with any number of the data

Page 8: Data Structure & Algorithm Basic Lab – week 3

8

ExerciseExercise• Create a singly linked list to store a list of

phone address.• Write a function to insert to a list a new

element just after the current element and use it to add node to the list

• Write a function for traversing the list to print out all information stored.

• Write a function for the removal of a node in the list.

Page 9: Data Structure & Algorithm Basic Lab – week 3

9

HintHint• you can organize elements and data

structure using following record structure AddressList. Define by your self a structure for storing infomation about an address.

struct AddressList {struct AddressList *next;struct Address addr;};

Page 10: Data Structure & Algorithm Basic Lab – week 3

10

Declaration of record structure

struct AddressList {struct AddressList *next;struct Address addr;};• “next” is the pointer variable which

can express the next element; an element of AddressList.

• “addr” is instance of an address.

Page 11: Data Structure & Algorithm Basic Lab – week 3

11

Important 3 factors of a LIST

• Root: It keeps the head of the list.• NULL: The value of pointer. It means

the tail of the list.• Cur: Pointer variable that keeps the

element just now.

NULLroot (or head)

cur

Page 12: Data Structure & Algorithm Basic Lab – week 3

12

InitialisationInitialisationstruct AddressList {struct AddressList *next;struct Address addr;};struct AddressList * root, *cur;

Page 13: Data Structure & Algorithm Basic Lab – week 3

13

Make new nodeMake new nodestruct AddressList* makeNewNode(){struct AddressList * new = (struct AddressList * ) malloc(sizeof(struct AddressList));

strcpy((new->addr).name, « Tran Van Thanh »);

….new->next =NULL;

return new;}root = makeNewNode();cur = root;

Page 14: Data Structure & Algorithm Basic Lab – week 3

14

AttentionAttention• You can modify the makeNewNode function to

receive the data field as parameter:struct AddressList* makeNewNode(Address addr){

struct AddressList * new = (struct AddressList * ) malloc(sizeof(struct AddressList));

new->addr=addr;new->next =NULL;return new;

}

Page 15: Data Structure & Algorithm Basic Lab – week 3

15

Link list: insertion Link list: insertion • Just after the current position

create new_item

new->next = cur->next;cur->next = new;cur= cur->next;

root

cur

new_item

Page 16: Data Structure & Algorithm Basic Lab – week 3

16

Link list: insertion Link list: insertion • Just after the current position

new = ( struct AddressList * ) malloc( sizeof( struct AddressList ) );

new->addr = addr;new->next = NULL;// new = makeNewNode();if ( root == NULL ) {

/* if there is no element */root = new;cur = root;

} else { new->next=cur->next;

cur->next = new;// prev=cur;cur = cur->next;

}}

Page 17: Data Structure & Algorithm Basic Lab – week 3

17

Linked lists – insertion Linked lists – insertion

• Another case: before the current position

root

Insert new item:

prev cur

Page 18: Data Structure & Algorithm Basic Lab – week 3

18

Traversing a listTraversing a listfor ( cur = root; cur != NULL; cur = cur->next ) { showAddress( cur->addr, stdout );}

NULLroot

cur

Page 19: Data Structure & Algorithm Basic Lab – week 3

19

Traversing a listTraversing a list• Changing the value of pointer variable cur in

sequence.• These variables are called “iterator.”• The traversing is finished if the value is NULL

NULLroot

cur

Page 20: Data Structure & Algorithm Basic Lab – week 3

20

DeletionDeletion• When we remove the first element

root = del->next; free(del);• When we remove the first element,

change the value of “root” into the value of “next” which is pointed by “del.”

NULLroot

del

Page 21: Data Structure & Algorithm Basic Lab – week 3

21

Deletion from the middleDeletion from the middle• We want to remove the node pointed by cur• Determine prev which point to the node just before the

node to deleteprev->next = cur->next;free(cur);

rootprev cur

Page 22: Data Structure & Algorithm Basic Lab – week 3

22

Bài tập I (Tuần 3)Bài tập I (Tuần 3)• Đọc dữ liệu từ file phone.dat (đã tạo

từ các bài tập trước) và nạp vào danh sách liên kết đơn.

• Phải sử dụng các hàm đã xây dựng ở lớp

• In ra nội dung danh sách các số điện thoại liên lạc.

• Yêu cầu người dùng nhập thêm các số mới và cập nhật vao file.

Page 23: Data Structure & Algorithm Basic Lab – week 3

23

ExerciseExercise• Implement function insert, delete with a

parameter n (integer) indicating the position of node to be affected.– The head position means 0th.– 1st means that we want to add the element into the

next place of the first element.– 2nd means the next place of the second element.

struct AddressList *insert (struct AddressList *root, struct Address ad, int n);

struct AddressList *delete(struct AddressList *root, int n);

Page 24: Data Structure & Algorithm Basic Lab – week 3

24

Bài tập IIBài tập II• Bổ sung vào bài tập I hai chức năng cập

nhật và xóa theo vị trí phần tử.• Sử dụng hai hàm đã viết ở lớp• Viết hàm và cài đặt sử dụng vào menu

chương trình chức năng tìm kiếm dữ liệu trong danh sách– Theo số DT– Theo tên

• Bắt tất cả các lỗi có thể xảy ra.• Bổ sung chức năng đảo ngược danh sách

Page 25: Data Structure & Algorithm Basic Lab – week 3

25

Bài tập IIIBài tập III• a) Viết hàm chia tách danh sách

– Chia làm hai danh sách con. Tham số là vị trí n. (Chỉ số tính bắt đầu từ 0)

– Tách – tham số n1 n2: Tách ra 1 danh sách con dài n2 nút từ vị trị n1.

– Tích hợp vào menu chương trình.• b) Viết hàm ghi nội dung một danh sách

vào một file văn bản. Tham số là con trỏ đầu danh sách và đường dẫn. Sử dụng hàm này để lưu kết quả của việc tách danh sách.

• c) Dữ liệu danh sách – Bài tập NokiaDB

Page 26: Data Structure & Algorithm Basic Lab – week 3

26

Freeing a listFreeing a list

rootto_freeto_free = root ;while (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 27: Data Structure & Algorithm Basic Lab – week 3

27

Freeing all nodes of a listFreeing all nodes of a list

rootto_freeto_free = root ;while (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 28: Data Structure & Algorithm Basic Lab – week 3

28

Freeing all nodes of a listFreeing all nodes of a list

rootto_freeto_free = root ;

while (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 29: Data Structure & Algorithm Basic Lab – week 3

29

Freeing all nodes of a listFreeing all nodes of a list

rootto_freewhile (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 30: Data Structure & Algorithm Basic Lab – week 3

30

Freeing all nodes of a listFreeing all nodes of a list

rootto_freewhile (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 31: Data Structure & Algorithm Basic Lab – week 3

31

Freeing all nodes of a listFreeing all nodes of a list

rootto_freewhile (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 32: Data Structure & Algorithm Basic Lab – week 3

32

Freeing all nodes of a listFreeing all nodes of a list

rootto_freewhile (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 33: Data Structure & Algorithm Basic Lab – week 3

33

Freeing all nodes of a listFreeing all nodes of a list

rootto_freewhile (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 34: Data Structure & Algorithm Basic Lab – week 3

34

Freeing all nodes of a listFreeing all nodes of a list

rootto_freewhile (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 35: Data Structure & Algorithm Basic Lab – week 3

35

Freeing all nodes of a listFreeing all nodes of a list

rootto_freewhile (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 36: Data Structure & Algorithm Basic Lab – week 3

36

Freeing all nodes of a listFreeing all nodes of a list

rootto_freewhile (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 37: Data Structure & Algorithm Basic Lab – week 3

37

Freeing all nodes of a listFreeing all nodes of a list

rootto_freewhile (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 38: Data Structure & Algorithm Basic Lab – week 3

38

Freeing all nodes of a listFreeing all nodes of a list

rootto_freewhile (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 39: Data Structure & Algorithm Basic Lab – week 3

39

Freeing all nodes of a listFreeing all nodes of a list

rootto_freewhile (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 40: Data Structure & Algorithm Basic Lab – week 3

40

Freeing all nodes of a listFreeing all nodes of a list

rootto_freewhile (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 41: Data Structure & Algorithm Basic Lab – week 3

41

Freeing all nodes of a listFreeing all nodes of a list

to_freewhile (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

root

Page 42: Data Structure & Algorithm Basic Lab – week 3

42

Freeing all nodes of a listFreeing all nodes of a list

NULL

rootto_freewhile (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 43: Data Structure & Algorithm Basic Lab – week 3

43

Freeing all nodes of a listFreeing all nodes of a list

NULL

rootto_freewhile (to_free != NULL) { root = root->next; free(to_free); to_free = root;}

Page 44: Data Structure & Algorithm Basic Lab – week 3

44

Reverse a listReverse a list• Given a list defined as:struct list_int {

int val;

struct list_int *next;

};

...

struct list_int *head=NULL;

• Write a function that reverse a list of this type.

Page 45: Data Structure & Algorithm Basic Lab – week 3

45

SolutionSolutionstruct list_int *list_reverse (struct list_int* li){

struct list_int *cur, *prev;cur = prev = NULL;while (li != NULL) {

cur = li;li = li->next;cur->next = prev;prev = cur;

}return prev;

}

Page 46: Data Structure & Algorithm Basic Lab – week 3

46

Bài tập 3 (Chương List)Bài tập 3 (Chương List)• Bổ sung vào Menu chương trình

(Nokia DB List) chức năng đảo ngược danh sách

• Kiểm tra bằng chức năng Duyệt.

Page 47: Data Structure & Algorithm Basic Lab – week 3

47

Tổng kết các chức năng của Tổng kết các chức năng của Bài tập Bài tập

• 1. Import from NokiaDB.dat (insertafter)• 2. Display (traverse)• 3. Add new phone (insertbefore)• 4. Insert at Position• 5. Delete at Position• 6. Delete current• 7. Delete first• 8. Reverse List• 9. Save to File• 10. Quit(Free)

Page 48: Data Structure & Algorithm Basic Lab – week 3

48

Exercise 3-3Exercise 3-3• Develop a simple student management program

using linked list composed of node like this:

typedef struct Student_t {char id[ID_LENGTH];char name[NAME_LENGTH];int grade;

struct Student_t *next; } Student;

Page 49: Data Structure & Algorithm Basic Lab – week 3

49

Exercise 3-3Exercise 3-3so that:- The list is sorted in descending order of student's grades.

- Program provide the functionality of:- Insert new student (when you insert a new student into this list, first find the right position)

- searching a student by ID: return to a pointer - delete a student with a given ID

- ;

Page 50: Data Structure & Algorithm Basic Lab – week 3

50

Adding a student - beginingAdding a student - begining

rootNext

root

Page 51: Data Structure & Algorithm Basic Lab – week 3

51

Adding a student – mid/endAdding a student – mid/end

root

Insert new item:

Previous Next

Page 52: Data Structure & Algorithm Basic Lab – week 3

52

Student *add_student(Student *root, Student *to_add){ Student *curr_std, *prev_std = NULL;

if (root == NULL) return to_add;

if (to_add->grade > root->grade) { to_add->next = root; return to_add; }

curr_std = root; while (curr_std != NULL && to_add->grade < curr_std->grade) { prev_std = curr_std; curr_std = curr_std->next; }

prev_std->next = to_add; to_add->next = curr_std;

return root;}

handle empty list

handle beginning

the rest

Page 53: Data Structure & Algorithm Basic Lab – week 3

53

Adding a student – Adding a student – beginningbeginning

if (root == NULL) return to_add;

if (to_add->grade > root->grade){ to_add->next = root; return to_add;}

95 80 70 …

to_add 100

root

Page 54: Data Structure & Algorithm Basic Lab – week 3

54

Adding a student – mid / Adding a student – mid / endend

curr_std = root;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return root;

95 80 70 60root

to_add 75

curr_std

Page 55: Data Structure & Algorithm Basic Lab – week 3

55

Adding a student – mid / Adding a student – mid / endend

curr_std = root;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return root;

95 80 70 60root

to_add 75

curr_stdprev_std

Page 56: Data Structure & Algorithm Basic Lab – week 3

56

Adding a student – mid / Adding a student – mid / endend

curr_std = root;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return root;

95 80 70 60root

to_add 75

curr_stdprev_std

Page 57: Data Structure & Algorithm Basic Lab – week 3

57

Adding a student – mid / Adding a student – mid / endend

curr_std = root;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return root;

95 80 70 60root

to_add 75

curr_stdprev_std

Page 58: Data Structure & Algorithm Basic Lab – week 3

58

Adding a student – mid / Adding a student – mid / endend

curr_std = root;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return root;

95 80 70 60root

to_add 75

curr_stdprev_std

Page 59: Data Structure & Algorithm Basic Lab – week 3

59

Adding a student – mid / Adding a student – mid / endend

curr_std = root;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return root;

95 80 70 60root

to_add 75

curr_stdprev_std

Page 60: Data Structure & Algorithm Basic Lab – week 3

60

Adding a student – mid / Adding a student – mid / endend

curr_std = root;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return root;

95 80 70 60root

to_add 75

curr_stdprev_std

Page 61: Data Structure & Algorithm Basic Lab – week 3

61

Adding a student – mid / Adding a student – mid / endend

curr_std = root;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return root;

95 80 70 60root

to_add 75

curr_stdprev_std

Page 62: Data Structure & Algorithm Basic Lab – week 3

62

Adding a student – mid / Adding a student – mid / endend

curr_std = root;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return root;

95 80 70 60root

to_add 75

curr_stdprev_std

Page 63: Data Structure & Algorithm Basic Lab – week 3

63

Adding a student – mid / Adding a student – mid / endend

curr_std = root;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return root;

95 80 70 60root

to_add 75

curr_stdprev_std

Page 64: Data Structure & Algorithm Basic Lab – week 3

64

Exercise Exercise • Implement find_student, which receives

a list and an ID number and returns a pointer to a student whose ID matches or NULL if no such student is found.

Student *find_student(Student *root, char* id);

Hint: Use strcmp(s1, s2) which compares s1 and s2 and returns 0 if they are equal

Page 65: Data Structure & Algorithm Basic Lab – week 3

65

SolutionSolution

/* find a student whose id matches the given id number */Student *find_student(Student *root, char* id){

Student *to_search = root; /* Start from root of list */while (to_search != NULL) /* go over all the list */{

if (strcmp(to_search->id, id) == 0) /* same id */return to_search;

to_search = to_search->next;}/* If we're here, we didn't find */return NULL;

}

Page 66: Data Structure & Algorithm Basic Lab – week 3

66

Removing a studentRemoving a student• We would like to be able to remove a

student by her/his ID.• The function that performs this is

remove_student

Page 67: Data Structure & Algorithm Basic Lab – week 3

67

rootPrevious Current

Removing a student - Removing a student - reminderreminder

Page 68: Data Structure & Algorithm Basic Lab – week 3

68

Removing a student – Removing a student – beginningbeginning

if (root == NULL) return root;

cur = root;

if (strcmp(cur->id, id) == 0){ root = root->next; free(cur); return root;}

74823 53621 25773root

cur

14525

last

ID

14525

Page 69: Data Structure & Algorithm Basic Lab – week 3

69

Removing a student – mid Removing a student – mid listlist

14525 74823 53621 25773root

53621

ID

cur

while (cur != NULL && strcmp(cur->id, id) != 0){ prev = cur; cur = cur->next;}

if (cur != NULL){ prev->next = cur->next; free(cur);}

return root;

Page 70: Data Structure & Algorithm Basic Lab – week 3

70

Removing a student – mid Removing a student – mid listlist

14525 74823 53621 25773root

53621

ID

prev cur

while (cur != NULL && strcmp(cur->id, id) != 0){ prev = cur; cur = cur->next;}

if (cur != NULL){ prev->next = cur->next; free(cur);}

return root;

Page 71: Data Structure & Algorithm Basic Lab – week 3

71

Removing a student – mid Removing a student – mid listlist

14525 74823 53621 25773root

53621

ID

prev cur

while (cur != NULL && strcmp(cur->id, id) != 0){ prev = cur; cur = cur->next;}

if (cur != NULL){ prev->next = cur->next; free(cur);}

return root;

Page 72: Data Structure & Algorithm Basic Lab – week 3

72

Removing a student – mid Removing a student – mid listlist

14525 74823 53621 25773root

53621

ID

prev cur

while (cur != NULL && strcmp(cur->id, id) != 0){ prev = cur; cur = cur->next;}

if (cur != NULL){ prev->next = cur->next; free(cur);}

return root;

Page 73: Data Structure & Algorithm Basic Lab – week 3

73

Removing a student – mid Removing a student – mid listlist

while (cur != NULL && strcmp(cur->id, id) != 0){ prev = cur; cur = cur->next;}

if (cur != NULL){ prev->next = cur->next; free(cur);}

return root;

14525 74823 25773root

53621

ID

prev cur

Page 74: Data Structure & Algorithm Basic Lab – week 3

74

Exercise Exercise • Add a change_grade function. The

function should take as parameters the root of the list, the ID whose grade we’d like to change, and the new grade

• Hint – Create a new student with the same name, ID as the old one, with the new grade. Then remove the old student from the list and add the new one using the existing functions

Page 75: Data Structure & Algorithm Basic Lab – week 3

75

solutionsolutionStudent *find_student(Student* root, char* id){ Student* curr = root;

while (curr != NULL && strcmp(curr->id, id) != 0) { curr = curr->next; }

return curr;}

Student *change_grade(Student *root, char* id, int new_grade){ Student* std = find_student(root, id); std = create_student(std->name, id, new_grade);

root = remove_student(root, id); return add_student(root, std);}

Page 76: Data Structure & Algorithm Basic Lab – week 3

76

QuestionQuestion• We are now designing “address list” for mobile phones.• You must declare a record structure that can keep a name,

a phone number, and a e-mail address at least. And you must make the program which can deals with any number of the data.

• Hint: you can organize elements and data structure using following record structure AddressList

struct AddressList {struct AddressList *prev;struct AddressList *next;struct Address addr;

};

Page 77: Data Structure & Algorithm Basic Lab – week 3

77

Double link listDouble link list• An element has 2 pointer fields, we can

follow front and back.

5

/

12 5

/

head

tail

Page 78: Data Structure & Algorithm Basic Lab – week 3

78

DeclarationDeclarationtypedef ... ElementType;

typedef struct Node{ ElementType Element; Node* Prev; Node* Next;

}; typedef Node* Position; typedef Position DoubleList;

Page 79: Data Structure & Algorithm Basic Lab – week 3

79

Initialisation and check for Initialisation and check for emptinessemptiness

void MakeNull_List (DoubleList *DL){

(*DL)= NULL;

}

int Empty (DoubleList DL){

return (DL==NULL);

}

Page 80: Data Structure & Algorithm Basic Lab – week 3

80

Delete a node Delete a node pointed by ppointed by pvoid Delete_List (Position p, DoubleList *DL){

if (*DL == NULL) printf(”Empty list”);

else {

if (p==*DL) (*DL)=(*DL)->Next;

//Delete first element

else p->Previous->Next=p->Next;

if (p->Next!=NULL) p->Next->Previous=p->Previous;

free(p);

}

}

5 12 5

/

8DL

p

Page 81: Data Structure & Algorithm Basic Lab – week 3

81

Delete a node Delete a node pointed by ppointed by pvoid Delete_List (Position p, DoubleList *DL){

if (*DL == NULL) printf(”Empty list”);

else {

if (p==*DL) (*DL)=(*DL)->Next;

//Delete first element

else p->Previous->Next=p->Next;

if (p->Next!=NULL) p->Next->Previous=p->Previous;

free(p);

}

}

5 12 5

/

8DL

p

Page 82: Data Structure & Algorithm Basic Lab – week 3

82

Delete a node Delete a node pointed by ppointed by pvoid Delete_List (Position p, DoubleList *DL){

if (*DL == NULL) printf(”Empty list”);

else {

if (p==*DL) (*DL)=(*DL)->Next;

//Delete first element

else p->Previous->Next=p->Next;

if (p->Next!=NULL) p->Next->Previous=p->Previous;

free(p);

}

}

5 5

/

8DL

p

Page 83: Data Structure & Algorithm Basic Lab – week 3

83

InsertionInsertionvoid Insert_List (ElementType X,Position p, DoubleList *DL){

if (*DL == NULL){ // List is empty(*DL)=(Node*)malloc(sizeof(Node)); (*DL)->Element = X; (*DL)->Previous =NULL; (*DL)->Next =NULL;

} else{

Position temp; temp=(Node*)malloc(sizeof(Node)); temp->Element=X; temp->Next=p; temp->Previous=p->Previous; if (p->Previous!=NULL) p->Previous->Next=temp; p->Previous=temp;

} }

Page 84: Data Structure & Algorithm Basic Lab – week 3

84

Create a libraryCreate a library• Create lib.h

–Type declaration–Function prototype

• Create lib.c–#include “lib.h”–Function Implementation

• Main Program: pro.c–#include “lib.h”

• Compile• gcc – o ex pro.c lib.c

Another way:gcc –c lib.cgcc –c pro.cgcc –o lib.o pro.oex

Page 85: Data Structure & Algorithm Basic Lab – week 3

85

Double linked listDouble linked list• Using the library doubly linked list

and the data you have created in the PhoneBook Exercise. Write a function that: –Count the max number of identical

phone number elements in a list.–Split these elements from the list.

Display the memory address of these element before and after the split action. (Bài tập)