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 ListinfonextinfonextinfonextinfonextlistNullnodenodenodenode“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 = pSteps 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 listp = 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 ”

16 Algorithm for removing a node from the front of a listp = 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.

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.

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

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)