งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

โครงสร้างข้อมูลแบบรายการโยง (Linked Lists) 290214 Data Structures and Algorithms อ. ธารารัตน์ พวงสุวรรณ คณะวิทยาศาสตร์และศิลปศาสตร์ มหาวิทยาลัยบูรพา วิทยาเขตสารสนเทศจันทบุรี

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "โครงสร้างข้อมูลแบบรายการโยง (Linked Lists) 290214 Data Structures and Algorithms อ. ธารารัตน์ พวงสุวรรณ คณะวิทยาศาสตร์และศิลปศาสตร์ มหาวิทยาลัยบูรพา วิทยาเขตสารสนเทศจันทบุรี"— ใบสำเนางานนำเสนอ:

1 โครงสร้างข้อมูลแบบรายการโยง (Linked Lists) 290214 Data Structures and Algorithms อ. ธารารัตน์ พวงสุวรรณ คณะวิทยาศาสตร์และศิลปศาสตร์ มหาวิทยาลัยบูรพา วิทยาเขตสารสนเทศจันทบุรี thararat@buu.ac.th

2 เนื้อหา โครงสร้างข้อมูลแบบ Linked Lists Single Linked List การดำเนินการกับ Single Linked Lists Double Linked Lists การดำเนินการกับ Double Linked Lists

3 ลักษณะของลิงค์ลิสต์ ลิงค์ลิสต์เป็นการนำข้อมูลแต่ละโหนดมาจัดเรียงต่อ กันเป็นลิสต์ที่มีการเชื่อมโยงกัน ภายในแต่ละโหนดแบ่งส่วนประกอบได้เป็น 2 ฟิลด์ คือ ข้อมูล และแอดเดรสของโหนดถัดไป (Link) โดยฟิลด์ข้อมูลสามารถเก็บข้อมูลได้มากกว่าหนึ่ง ค่า ลิงค์ลิสต์เป็นการนำข้อมุลแต่ละโหนดมาเรียงต่อกัน เป็นลิสต์ สิ่งที่เป็นตัวกำหนดลำดับ คือ ฟิลด์ แอดเดรสของแต่ละโหนด การเข้าถึงลิงค์ลิสต์เริ่มต้นจะต้องมีพอยน์เตอร์ชี้ไป ยังโหนดแรกของลิงค์ลิสต์ (Head) ฟิลด์แอดเดรสของโหนดสุดท้ายต้องกำหนดให้เป็น null ซึ่งเป็นการกำหนดให้จบลิงค์ลิสต์ ลิงค์ลิสต์ที่ไม่มีโหนดอยู่ภายในจะเรียกว่า ลิสต์ว่าง (empty list หรือ null list)

4 ประเภทของลิงค์ลิสต์ ลิงค์ลิสต์เดี่ยว (Single Linked List) คือ ลิงค์ลิสต์ที่มีฟิลด์แอดเดรสเพียงฟิลด์เดียว สำหรับชี้ตำแหน่งของโหนดถัดไป ลิงค์ลิสต์คู่ (Double Linked List) คือ ลิงค์ลิสต์ที่มีฟิลด์แอดเดรสจำนวน 2 ฟิลด์ สำหรับชี้ตำแหน่งของโหนถัดไปและ ตำแหน่งของโหนดที่อยู่ก่อนหน้า DogUSACop12 Head 1378 DogUSACop Head

5 Single Linked List Each item in the list is called a node. It contains two field - An information field : holds the actual element on the list - A next address field : contains the address of the next node in the list The entire linked list is accessed from an external pointer that points to the first node in the list. The next address field of the last node in the list contains a special value, known as null. The list with no nodes on it is called the empty list or null list.

6 ลักษณะของ Single Linked List infonext infonextinfonextinfonext list Null node Operation for Linked List - Inserting - Removing “A List is a dynamic data structure. The number of nodes on the list may vary dramatically as elements are inserted and removed.”

7 Notation for use in algorithm (Not in C program) If p is a pointer to a node - node(p) refers to the node pointed to by p - info(p) refers to the information portion of that node - next(p) refers to the next address portion - info(next(p)) refers to the information portion of the node that follows node(p) in the list - getnode() refers creating a new node freenode(p) refers destroying node p

8 Inserting an element to the front of a list สมมติว่า กำหนดให้มี List of integers และต้องการเพิ่ม integer 6 เข้าไปไว้ส่วนหน้าของ List infonext infonextinfonext list Null538

9 Steps for inserting 1. Obtain an empty node to be added onto the list. p = getnode() 2. Insert the integer 6 into info portion of the newly allocated node. info(p) = 6 infonext p infonext p 6

10 Steps for inserting 3. Set the next portion of that node (Since node(p) is to be inserted at the front of the list,the node that follows should be the current first node on the list. next(p) = list infonext infonextinfonext list Null538 infonext p 6

11 Steps for inserting 4. Since list is the external pointer to the desired list, its value must be modified to the address of the new first node of the list. list = p infonext infonextinfonext list Null538 infonext p 6 list Null538 6

12 Algorithm for adding the integer 6 to the front of the list p = getnode() info(p) = 6 next(p) = list list = p “The algorithm can be generalized so that it adds any object X to the front of a list by replacing the operation info(p) = 6 to info(p) = X ”

13 Removing the first node of a nonempty list สมมติว่า กำหนดให้มี List of integers และต้องการลบโหนดแรก ของ List และเก็บค่า info ของโหนดนั้นไว้ในตัวแปร X infonext infonextinfonext list Null759

14 Algorithm 1. p = list 2. list = next(p) infonext infonextinfonext list Null759 p infonext infonextinfonext list Null 7 59 p

15 Algorithm 3. x = info(p) infonext infonextinfonext list Null 7 59 p X=7 4. freenode(p) infonextinfonext list Null59 p X=7

16 Algorithm for removing a node from the front of a list p = list list = next(p) x = info(p) freenode(p)

17 Linked Implementation of Stacks - The operation of adding an element to the front of a linked list is similar to that of pushing an element onto a stack. - A stack can be accessed only through its top element, and a list can be accessed only from the pointer to its first element. - The operation of removing the first element from a linked list is analogous to popping a stack. - A stack may be represented by a linear linked list. The first node of the list is the top of the stack.

18 If an external pointer s points to such a linked list, the operation push(s,x) may be implemented by p = getnode() info(p) = x next(p) = s s = p 538 7 null s 538 7 6 s

19 The operation x = pop(s) removes the first node from a nonempty list and signals underflow if the list is empty : If (empty(s)) print ‘stack underflow’ exit else p = s s = next(p) x = info(p) freenode(p) “The operation empty(s) is merely a test of whether s equals null”

20 Linked Implementation of Queues - The items are deleted from of a queue and inserted at the rear. - A pointer to the first element of a list represent the front of the queue - Another pointer to the last element of the list represents the rear of the queue. - A queue q consists of a list and two pointers, q.front and q.rear - The operations empty(q) and x = remove(q) are analogous to empty(s) and x = pop(s), with the pointer q.front replacing s. - When the last element is removed from a queue, q.rear must also be set to null, since in an empty queue both q.front and q.rear must be null.

21 Notation for use in algorithm (Not in C program) q.front represents a pointer to the first element of a list q.rear represents a pointer to the last element of the list p และ q represents a pointer to node in the list getnode() คือ การสร้าง node ใหม่ขึ้นใน list freenode(p) คือ การลบโหนด p ใน list

22 Algorithm for x = remove(q) If(empty(q) print “queue underflow” exit() p = q.front x = info(p) q.front = next(p) if(q.front == null) q.rear = null freenode(p) return(x)

23 Algorithm for insert(q) p = getnode() info(p) = x next(p) = null if(q.rear == null) q.front = p else next(q.rear) = p q.rear = p

24 Inserting a new element after node การเพิ่มโหนดใหม่หลังโหนดใด ๆ จะเกี่ยวข้องกับขั้นตอนต่าง ๆ ดังนี้ - Allocating a node - Inserting the information - adjusting two pointer ** The amount of work is independent of the size of the list.

25 Let insafter(p,x) denote the operation of inserting an item x into a list after a node pointed to by p. Algorithm q = getnode() info(q) = x next(q) = next(p) next(p) = q list x0 x1x2x3 null p x q

26 Let delafter(p,x) denote the operation of deleting the node following node(p) and assigning its contents to variable x. Algorithm q = next(p) x = info(q) next(p) = next(q) freenode(q)

27 Implementation of List - A list is a collection of nodes - The nodes can not be ordered by the array ordering. - Each node must contain within itself a pointer to its successor until the last node in the list - For last node,next field contain -1, which is the null pointer Example of declaration as an array node #define NUMNODES 500 struct nodetype { int info, next; }; struct nodetype node[NUMNODES];

28 Initially, all nodes are unused, since no list have yet been formed. Therefore they must all be placed on the available list. If the variable avail is used to point to the available list, may organize that list follows: avail = 0; for(i=0; i = NUMNODES-1; i++) node[i].next = i+1; node[NUMNODES-1].next = -1; “Assume that variables node and avail are global and can be used by any routine” When a node is needed for use in a particular list, it is obtained from the available list.And When a node is no longer necessary,it is returned to the available list.

29 Operation ที่เกี่ยวข้องซึ่งสามารถ implement ใน C ได้ ประกอบด้วย - getnode : is a function that removes a node from the available list and return pointer to it. i nt getnode(void) {int p; if(avail == -1) { printf(“Overflow\n”); exit(1); } p = avail; avail = node[avail].next; return(p); }

30 - freenode : is a function to accepts a pointer to a node and returns that node to the available list. void freenode(int p) { node[p].next = avail; avail = p; return; }

31 Linked Lists Using Dynamic Variables A linked list consists of a set of nodes, each of which has two fields : - an information field - a pointer to the next node in the list - an external pointer points to the first node in the list struct node { int info; struct node *next; } typedef struct node *NODEPTR;

32 Instead of declaring an array to represent an aggregate collection of nodes, nodes are allocated and freed as necessary. NODEPTR p; p = getnode(); should place the address of an available node into p. Function getnode : NODEPTR getnode(void) { NODEPTR p; p = (NODEPTR) malloc(sizeof(struct node)); return(p); }

33 Function freenode(p) should return the node whose address is at p to available storage. void freenode(NODEPTR p) { free(p); }

34 doubleLinked Lists Each node in such a list contains two pointers, one to its predecessor and another to its successor null May consider the nodes on a doublelinked list to consist of 3 field : - An info field : contains the information stored in the node - Left field : contains pointer to the left node - Right field : contain pointer to the right node

35 We may declare a set of such nodes using either the array or dynamic implementation, by Array ImplementationDynamic Implementation struct nodetype { struct node { int info; int info; int left, right; struct node *left, *right; }; struct nodetype node[NUMNODES]; typedef struct node *NODEPTR

36 Operation on double linked lists - Delete a given node Deletes the node pointed to by p from a double linked list and stores its contents in x - Insert a node to the right of node(p) Insert a node with information field x to the right of node(p) in a double linked list. - Insert a node to the left of node(p) Insert a node with information field x to the left of node(p) in a double linked list.

37 Notation for use in algorithm (Not in program) If p,q,r is a pointer to a node - node(p) refers to the node pointed to by p - info(p) refers to the information portion of that node - left(p) refers to the left address portion (address of prior node for node p) - right(p) refers to the right address portion (address of next node for node p) - left(r) refers to the left address portion (address of prior node for node r) - right(r) refers to the right address portion (address of next node for node r) - left(q) refers to the left address portion (address of prior node for node q) - right(q) refers to the right address portion (address of next node for node q)

38 Delete a given node สมมติว่า กำหนดให้ Double linked list เป็น List of integers และต้องการลบโหนดที่ p ชี้อยู่ และเก็บค่า info ของโหนดนั้นไว้ในตัวแปร X กำหนด q และ r เป็นตัวชี้ไปยังโหนดใด ๆ x = info(p) q = left(p) r = right(p) right(q) = r left(r) = q freenode(p)

39 Insert a node to the right of node(p) q = getnode() info(q) = x r = right(p) left(r) = q right(q) = r left(q) = p right(p) = q สมมติว่า กำหนดให้ Double linked list เป็น List of integers และต้องการแทรกโหนดทางขวามือ จากโหนดที่ p ชี้อยู่ และกำหนด q และ r เป็นตัว ชี้ไปยังโหนดใด ๆ

40 Insert a node to the left of node(p) q = getnode() info(q) = x r = left(p) right(r) = q left(q) = r right(q) = p left(p) = q สมมติว่า กำหนดให้ Double linked list เป็น List of integers และต้องการแทรกโหนดทางซ้ายมือ จากโหนดที่ p ชี้อยู่ และกำหนด q และ r เป็นตัว ชี้ไปยังโหนดใด ๆ


ดาวน์โหลด ppt โครงสร้างข้อมูลแบบรายการโยง (Linked Lists) 290214 Data Structures and Algorithms อ. ธารารัตน์ พวงสุวรรณ คณะวิทยาศาสตร์และศิลปศาสตร์ มหาวิทยาลัยบูรพา วิทยาเขตสารสนเทศจันทบุรี

งานนำเสนอที่คล้ายกัน


Ads by Google