โครงสร้างข้อมูลแบบรายการโยง (linked lists)

40
โโโโโโโโโโโโโโโโโโโโโโโโโโโ (Linked Lists) 290210 Data Structures and Algorithms โ.โโโโโโโโโ โโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโ [email protected]

Upload: samantha-koch

Post on 30-Dec-2015

30 views

Category:

Documents


0 download

DESCRIPTION

290210 Data Structures and Algorithms. โครงสร้างข้อมูลแบบรายการโยง (Linked Lists). อ.ธารารัตน์ พวงสุวรรณ คณะวิทยาศาสตร์และศิลปศาสตร์ มหาวิทยาลัยบูรพา วิทยาเขตสารสนเทศจันทบุรี [email protected]. เนื้อหา. โครงสร้างข้อมูลแบบ Linked Lists Single Linked List - PowerPoint PPT Presentation

TRANSCRIPT

โครงสร�างข้�อมู�ลแบบรายการโยง

(Linked Lists)

290210 Data Structures and Algorithms

อ. ธาราร�ตน์� พวงส�วรรณคณะว�ทยาศาสตร�และศ�ลปศาสตร�

มูหาว�ทยาล�ยบ�รพา ว�ทยาเข้ตสารสน์เทศจั�น์ทบ�ร"[email protected]

เนื้��อหา• โครงสร�างข้�อมู�ลแบบ Linked Lists• Single Linked List• การดำ%าเน์�น์การก�บ Single Linked Lists• Double Linked Lists• การดำ%าเน์�น์การก�บ Double Linked

Lists

ล�กษณะข้องล�งค�ล�สต�• ล�งค�ล�สต�เป'น์การน์%าข้�อมู�ลแต(ละโหน์ดำมูาจั�ดำเร"ยงต(อก�น์เป'น์ล�สต�ท")มู"การ

เชื่+)อมูโยงก�น์• ภายใน์แต(ละโหน์ดำแบ(งส(วน์ประกอบไดำ�เป'น์ 2 ฟิ0ลดำ� ค+อ ข้�อมู�ล และ

แอดำเดำรสข้องโหน์ดำถั�ดำไป (Link) โดำยฟิ0ลดำ�ข้�อมู�ลสามูารถัเก2บข้�อมู�ลไดำ�มูากกว(าหน์3)งค(า

• ล�งค�ล�สต�เป'น์การน์%าข้�อมู�ลแต(ละโหน์ดำมูาเร"ยงต(อก�น์เป'น์ล�สต� ส�)งท")เป'น์ ต�วก%าหน์ดำล%าดำ�บ ค+อ ฟิ0ลดำ�แอดำเดำรสข้องแต(ละโหน์ดำ

• การเข้�าถั3งล�งค�ล�สต�เร�)มูต�น์จัะต�องมู"พอยน์�เตอร�ชื่"4ไปย�งโหน์ดำแรกข้อง ล�งค�ล�สต� (Head)

• ฟิ0ลดำ�แอดำเดำรสข้องโหน์ดำส�ดำท�ายต�องก%าหน์ดำให�เป'น์ null ซึ่3)งเป'น์การก%าหน์ดำให�จับล�งค�ล�สต�

• ล�งค�ล�สต�ท")ไมู(มู"โหน์ดำอย�(ภายใน์จัะเร"ยกว(า ล�สต�ว(าง (empty list หร+อnull list)

ประเภทข้องล�งค�ล�สต�• ลิงค์�ลิสต์�เดี่��ยว (Single Linked List) ค+อ

ล�งค�ล�สต�ท")มู"ฟิ0ลดำ�แอดำเดำรสเพ"ยงฟิ0ลดำ�เดำ"ยวส%าหร�บชื่"4ต%าแหน์(งข้องโหน์ดำถั�ดำไป

• ลิงค์�ลิสต์�ค์�� (Double Linked List) ค+อ ล�งค� ล�สต�ท")มู"ฟิ0ลดำ�แอดำเดำรสจั%าน์วน์ 2 ฟิ0ลดำ� ส%าหร�บชื่"4

ต%าแหน์(งข้องโหน์ถั�ดำไปและต%าแหน์(งข้องโหน์ดำท")อย�(ก(อน์หน์�า

Dog USA Cop12Head 13 78

Dog USA CopHead

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.

ลิ�กษณะของ Single Linked Listinfonext infonext infonext infonext

list Null

node node node 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.”

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 por tion of the node

that follows node(p) in the list- -

getnode() refers creating a new nodefreenode(p) refers destroying node p

Inserting an element to the front of a list

สมูมูต�ว(า ก%าหน์ดำให�มู" List of integers และต�องการเพ�)มู integer 6

เข้�าไปไว�ส(วน์หน์�าข้อง List

infonext infonext infonext

list Null5 3 8

Steps for inserting

1 . Obtain an empty node to be added onto the list. p = getnode()

2 6. Insert the integer into info portion of the newly allocated node. info(p) = 6

infonextp

infonextp

6

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

ssssss ss sss sssssss sssss ssss ss sss sssss. next(p) = list

infonext infonext infonext

list Null5 3 8

infonextp

6

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 infonext infonext

listNull5 3 8

infonextp

6

list Null5 3 86

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 o bject X to the front of a list by replacing the operation info(p) = 6 to info(p) = X ”

Removing the first node of a nonempty list

สมูมูต�ว(า ก%าหน์ดำให�มู" List of integers และต�องการลบโหน์ดำแรก

ข้อง List และเก2บค(า info ข้องโหน์ดำน์�4น์ไว�ใน์ต�วแปร X

infonext infonext infonext

list Null7 5 9

Algorithm1 . p = list

2. list = next(p)

infonext infonext infonextlist Null7 5 9

p

infonext infonext infonext

listNull

75 9

p

Algorithm 3. x = info(p)

infonext infonext infonext

listNull

75 9

pX=7

4. freenode(p)

infonext infonext

listNull5 9

pX=7

Al gor i t hmf or r emovi ng a no de f r omt he f r ont of a l i st

p = list list = next(p)

x = info(p)freenode(p)

Linked Implementation ofStacks

- 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 ss ssssssss ssss ssss sss sssssss ss sss sssss ssss ssss.

- 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.

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

5 3 8 7 nulls

5 3 8 7 null6s

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”

Linked Implementation ofQueues

- 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.

Notation for use in algorithm (Not in C program)

q.front represents a pointer to the first element of a listq.rear represents a pointer to the last element of the listp แลิะ q represents a pointer to node in the listgetnode() ค+อ การสร�าง node ใหมู(ข้34น์ใน์ listfreenode(p) ค+อ การลบโหน์ดำ p ใน์ list

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 = nullfreenode(p)return(x)

Algorithm for insert(q)

p = getnode() info(p) = x next(p) = null if(q.rear == null)

q.front = pelse

next(q.rear) = p q.rear = p

Inserting a new element after node

การเพ�)มูโหน์ดำใหมู(หล�งโหน์ดำใดำ ๆ จัะเก")ยวข้�องก�บข้�4น์ตอน์ต(าง ๆ ดำ�งน์"4- Allocating a node- Inserting the information- adjusting two pointer

** The amount of work is independent of the size of the list.

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

list0x 1x 2x 3null

p

sq

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)

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 5 0 0

struct nodetype { int info, next;

}; struct nodetype node[NUMNODES];

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.

Operation ท")เก")ยวข้�องซึ่3)งสามูารถั implement ใน์ C ไดำ� ประกอบดำ�วย- getnode : is a function that removes a node from the available list

and return pointer to it.int getnode(void){ int p;

if(avail == -1) {printf(“Overflow\n”);exit(1);

}p = avail;avail = node[avail].next;return(p);

}

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

}

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;

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

Function freenode(p) should return the node whose address is at p to available storage.

void freenode(NODEPTR p){

free(p);}

double Linked Lists Each node in such a list contains two pointers, one to its predecessor

and another to its successor

null null

May consider the nodes on a double linked 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

We may declare a set of such nodes using either the array or dynamic implementation, by

Array Implementation Dynamic Implementation

struct nodetype { struct node { int info; int info; int left, right; struct node *left, *right;}; };

struct nodetype node[NUMNODES]; typedef struct node *NODEPTR

Operation on double linkedlists

- 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 adouble 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 adouble linked list.

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)

Delete a given nodeสมูมูต�ว(า ก%าหน์ดำให� Double linked list เป'น์ List of integers และต�องการลบโหน์ดำท") p ชื่"4อย�( และเก2บค(า info ข้องโหน์ดำน์�4น์ไว�ใน์ต�วแปร Xก%าหน์ดำ q และ r เป'น์ต�วชื่"4ไปย�งโหน์ดำใดำ ๆ

x = info(p) q = left(p) r = right(p)

right(q) = rleft(r) = qfreenode(p)

Insert a node to the right of node(p)

q = getnode()info(q) = x r = right(p)

left(r) = qright(q) = rleft(q) = pright(p) = q

สมูมูต�ว(า ก%าหน์ดำให� Double linked list เป'น์ List of integers และต�องการแทรกโหน์ดำทางข้วามู+อจัากโหน์ดำท") p ชื่"4อย�( และก%าหน์ดำ q และ r เป'น์ต�วชื่"4ไปย�งโหน์ดำใดำ ๆ

Insert a node to the left of node(p)

q = getnode()info(q) = x r = left(p)

right(r) = qleft(q) = rright(q) = pleft(p) = q

สมูมูต�ว(า ก%าหน์ดำให� Double linked list เป'น์ List of integers และต�องการแทรกโหน์ดำทางซึ่�ายมู+อจัากโหน์ดำท") p ชื่"4อย�( และก%าหน์ดำ q และ r เป'น์ต�วชื่"4ไปย�งโหน์ดำใดำ ๆ