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 listThe entire linked list is accessed from an external pointer that points tothe 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 infonextinfonextinfonextinfonextlistNullnodenodenodenode“A List is a dynamic data structure. The number of nodes on the list mayvary dramatically as elements are inserted and removed.”Operation for Linked List- Inserting- Removing
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 nodethat follows node(p) in the list-getnode() refers creating a new nodefreenode(p) refers destroying node p
8 Inserting an element to the front of a list สมมติว่า กำหนดให้มี List of integers และต้องการเพิ่ม integer 6เข้าไปไว้ส่วนหน้าของ Listinfonextinfonextinfonextlist538Null
9 1. Obtain an empty node to be added onto the list. p = getnode() Steps for inserting1. 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) = 6infonextpinfonextp6
10 3. Set the next portion of that node Steps for inserting3. Set the next portion of that node(Since node(p) is to be inserted at the front of the list,the node thatfollows should be the current first node on the list.next(p) = listinfonextinfonextinfonextinfonextp6list538Null
11 be modified to the address of the new first node of the list. list = p Steps for inserting4. Since list is the external pointer to the desired list, its value mustbe modified to the address of the new first node of the list.list = pinfonextinfonextinfonextinfonextp6538Nulllistlist6538Null
12 Algorithm for adding the integer 6 to the front of the list p = getnode()info(p) = 6next(p) = listlist = p“The algorithm can be generalized so that it adds any object X to thefront 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 ของโหนดนั้นไว้ในตัวแปร Xinfonextinfonextinfonextlist759Null
14 Algorithm 1. p = list info next info next info next list p 759Nullp2. list = next(p)infonextinfonextinfonextp759Nulllist
15 Algorithm 3. x = info(p) info next info next info next X=7 p list 59Nulllist4. freenode(p)infonextinfonextX=7p59Nulllist
16 Algorithm for removing a node from the front of a list p = listlist = next(p)x = info(p)freenode(p)
17 Linked Implementation of Stacks - The operation of adding an element to the front of a linked listis similar to that of pushing an element onto a stack.- A stack can be accessed only through its top element, and a listcan be accessed only from the pointer to its first element.- The operation of removing the first element from a linked list isanalogous to popping a stack.- A stack may be represented by a linear linked list. The first nodeof 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 byp = getnode()info(p) = xnext(p) = ss = ps538null7s6538null7
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’exitelsep = ss = 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 ofthe 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 beset to null, since in an empty queue both q.front and q.rear must be null.
21 Algorithm for x = remove(q) If(empty(q)print “queue underflow”exit()p = q.frontx = info(p)q.front = next(p)if(q.front == null)q.rear = nullfreenode(p)return(x)
22 Algorithm for insert(q) p = getnode()info(p) = xnext(p) = nullif(q.rear == null)q.front = pelsenext(q.rear) = pq.rear = p
23 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.
24 Let insafter(p,x) denote the operation of inserting an item x into a list after a node pointed to by p.Algorithmq = getnode()info(q) = xnext(q) = next(p)next(p) = qplistnullx0x1x2x3qx
25 Let delafter(p,x) denote the operation of deleting the node following node(p) and assigning its contents to variable x.Algorithmq = next(p)x = info(q)next(p) = next(q)freenode(q)
26 doubleLinked ListsEach node in such a list contains two pointers, one to its predecessorand another to its successornullnullMay 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
27 Operation on doublelinked lists - Delete a given nodeDeletes the node pointed to by p from a double linked list andstores 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 a
28 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 nodeleft(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)
29 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) = rleft(r) = qfreenode(p)
30 Insert a node to the right of node(p) สมมติว่า กำหนดให้ Double linked list เป็น List of integers และต้องการแทรกโหนดทางขวามือจากโหนดที่ p ชี้อยู่ และกำหนด q และ r เป็นตัวชี้ไปยังโหนดใด ๆq = getnode()info(q) = xr = right(p)left(r) = qright(q) = rleft(q) = pright(p) = q
31 Insert a node to the left of node(p) สมมติว่า กำหนดให้ Double linked list เป็น List of integers และต้องการแทรกโหนดทางซ้ายมือจากโหนดที่ p ชี้อยู่ และกำหนด q และ r เป็นตัวชี้ไปยังโหนดใด ๆq = getnode()info(q) = xr = left(p)right(r) = qleft(q) = rright(q) = pleft(p) = q