Vous êtes sur la page 1sur 14

LINKED LIST

INTRODUCTION Array, stack and queue are data structures ordered linearly, and this ordering relation was preserved in the corresponding storage structures by using sequential allocation. There was no need for an element to specify where the next element wld be found. Consider a list consisting of elements which vary individually in size. The task of directly computing the address of a particular element becomes much more difficult. But there are many applications which, by their very nature, have data which are continually being updated. Each time a change occurs, significant manipulation of the data is required. Sometimes sequential allocation of data for such application will lead to wasting of memory, wasting of computational power. And for certain applications its totally unacceptable. Another method of referring the elements of the list is pointer. Pointers are variables that store memory addresses. Pointers are of same length and can be manipulated with simple allocation techniques. They allow referencing of structures in a uniform way, regardless of the organization of the structure being referenced. The use of pointers or links to refer to elements of a data structure implies that elements which are logically adjacent need not be physically adjacent in memory. Definition A list can be defined as an order set of elements which may vary in number. A simple way to rep. a number is to expand each node to contain a link or pointer to the next node. This rep. is called a one-way chain or singly linked linear list.

FIRST The variable FIRST contains an address or pointer which gives the location of the first node of the list. Each node is divided into two parts. The first part rep. the inf. of the element, and the second part contains the address of the next node. The last node of the list does not have a successor node, and no actual address is stored in the pointer field. Here the null value is stored as address. Example: The linked list that contains five-nodes, and whose elements are located in memory locations 2000,2010,2002,2012, and 2006, respectively. The link address of NULL in the last node signals the end of the list.

2010

B 2010

2002

C 2002

2012

D 2012

2006

E 2006

FIRST= 2000

It is possible to have no nodes at all. Such a list is called an empty list, and this is denoted by assigning a value of NULL to FIRST.

SLICA

In an n-element sequentially allocated list to insert a new element between the first and second elements, the last n-1 elements of the list must be moved to make room for the new element. In deletion all the elements after the element being deleted must be moved up so as to take up the vacant space caused by the element being removed from the list. For a list that contains many nodes, this is an inefficient way of performing an insertion and deletion. In the linked allocation, if a new element is to be inserted following the first element only pointers are required to be interchanged.

2100
X

2002
2010

2012

2006

FIRST= 2000
A 2100 B 2002

Insertion

2006

FIRST= 2000

Deletion

It is easier to join or split two linked lists than it is in the case of sequential allocation. This can be done just by changing the pointers. In a sequential allocation accessing the particular node is much easy than the linked list becoz in linked list if particular node is required it is necessary to follow the links from the first node onwards until the desired node is found. The pointers or links consume the additional memory, but if only part of the memory word is being used than the pointer can be stored in the remaining part. It is possible to group nodes so as to require only one link per several nodes. The pointers can also be used to specify more complex relations between the nodes, such as tree or a directed graph. Here the sequential allocation can not be utilized. For certain operations linked allocation is more efficient than sequential and for other operations the opposite is true. In many applications, both types of allocations are used. Array Linked-list -Continuous memory allocation -Scattere memory allocation. -Link field is not required. -Link field is required. -Insertion deletion is complex -Comparatively easy. -Static -Dynamic -Less memory is required -More memory. A pool or list of free nodes which we refer to as availability list is maintained by conjunction with linked allocation. Whenever a node is to be inserted in a list, a free node is taken from availability list and linked to former list as required. On

SLICA

the other hand, the deletion of a node from a list causes its return to availability list. So we can use free space in future. TYPES OF LINKED-LIST Singly Linked List Circular Linked List Two-way or doubly linked list Circular doubly linked list. Singly linked list

2010

2005

Null

Circular Linked List It has no beginning and no end. The last item points back to the first item. In this list left link denotes the predecessor of node and right link denotes successor of node. A A 2010 2010 B B 2005 2005 C C 2050 2050

Two-way linked list Doubly linked list uses double set of pointer, one pointing to next item and other pointing to preceding item. It is bi-direction. A 2010 B 2005 C Null

Circular doubly linked list Circular doubly linked list employs both forward pointer and backward pointer in circular form. A 2010 B 2005 C 2050

Operations on Linear Lists Using Singly Linked Storage Structure Insertion and deletion operations can be performed on the elements of singly linked list. A typical element or node consists of two fields, namely, an information field called INFO and a pointer field denoted by LINK. The name of a typical element is denoted by NODE. NODE INFO LINK It is assumed that an available area of storage for this node structure consists of a linked stack of available nodes, where the pointer variable avail contains the address of the top node in the stack.

SLICA

Obtaining a node from the avi. stack: Assume that the address of the next avi. node is to be stored in the variable NEW. For practical reason the availability stack contains only a finite number of nodes. If the value of AVAIL is NULL its an underflow condition, n its required to be checked before performing ope. If the node is available, then the new top-most element of the availability stack is denoted by LINK(AVAIL). The fields of the node corresponding to the pointer value of NEW can now filled in and the field LINK(NEW) is set to a value which designates the successor node of this new node.

A similar procedure can be formulated for the return of a discarded node to the availability stack. If the address of this discarded node is given by the variable FREE, then the link field of this node is set to the present value of AVAIL and the value of FREE becomes the new value of AVAIL.

Algorithms to insert a node into a linked linear list in a stack-like manner. Insertion at FRONT. Function INSERT(X, FIRST). Given X, a new element, and FIRST, a pointer to the first element of a linked linear list whose typical node contains INFO and LINK fields, this function inserts X. AVAIL is a pointer to the top element of the 4 SLICA

availability stack; NEW is a temporary pointer variable. It is required that X precedes the node whose address is given by FIRST. X Element to be inserted FIRST Pointer to first node NEW Pointer to new node AVAIL Pointer to first node of availability list 1) [Underflow?] If AVAIL = NULL then WRITE (AVAILABILITY STACK UNDERFLOW) Return(FIRST) 2) [Obtain address of next free node] NEW AVAIL 3) [Remove free node from availability stack] AVAIL LINK(AVAIL) 4) [Initialize fields of new node and its link to the list] INFO(NEW) X LINK(NEW) FIRST 5) [Return address of new node] Return(NEW) Insertion at END Function INSEND(X, FIRST). Given X, a new elements, and FIRST, a pointer to the first element of a linked linear list whose typical node contains INFO and LINK fields, this function inserts X. AVAIL is a pointer to the top element of the availability stack: NEW and SAVE are temporary pointer variables. It is required that X be inserted at the end of the list. X Element to be inserted FIRST Pointer to first node NEW Pointer to new node AVAIL Pointer to first node of availability list SAVE Temporary pointer 1) [Underflow] If AVAIL = NULL then WRITE(AVAILABILITY STACK UNDERFLOW) Return(FIRST) 2) [Obtain address of next free node] NEW AVAIL 3) [Remove node from availability stack] AVAIL LINK(AVAIL) 4) [Initialize fields of new node] INFO(NEW) X LINK(NEW) NULL 5) [Is the list empty?]

SLICA

If FIRST = NULL then Return(NEW) 6) [Initialize search for the last node] SAVE FIRST 7) [Search for end of the list] Repeat while LINK(SAVE) NULL SAVE LINK(SAVE) 8) [Set link for last node to new] LINK(SAVE) NEW 9) [Return first node pointer] Return(FIRST) There are many applications where it is desirable to maintain an ordered linear list. The ordering is in increasing or decreasing order on the INFO field. A general algorithm for inserting a node into an ordered linear list is now presented. 1. Remove a node from the availability stack 2. Set the fields of the new node 3. If the linked list is empty then return the address of the new node 4. If the node precede all others in the list then insert the node at the front of the list and return its address 5. Repeat step 6 while information content of the node in the list is less than the information content of the new node 6. Obtain the next in the linked list 7. Insert the new node in the list and return address of its first node Function INSORD(X, FIRST). Given a new term X, this function inserts it into a linked linear list whose typical node contains INFO and LINK fields as previously described. AVAIL is a pointer to the top element of the availability stack; NEW and SAVE are temporary pointer variables. It is required that X be inserted so that it preserves the ordering of the terms in increasing order of their INFO fields. Y Element to be inserted FIRST Pointer to first node NEW Pointer to new node AVAIL Pointer to first node of availability list SAVE Temporary pointer 1) [Underlow?] If AVAIL = NULL then WRITE (AVAILABILITY STACK UNDERFLOW) Return(FIRST) 2) [Obtain address of next free node] NEW AVAIL 3) [Remove node from availability stack]

SLICA

AVAIL LINK(AVAIL) 4) [Copy information content into new node] INFO(NEW) Y 5) [Is the list empty?] If FIRST = NULL then LINK(NEW) NULL Return(NEW) 6) [Does the new node precede all others in list?] If INFO(NEW) INFO(FIRST) then LINK(NEW) FIRST Return(NEW) 7) [Initialize temporary pointer] SAVE FIRST 8) [Search for predecessor of new node] repeat while LINK(SAVE) NULL and INFO(LINK(SAVE)) INFO(NEW) SAVE LINK(SAVE) 9) [Set links field of new node & its predecessor] LINK(NEW) LINK(SAVE) LINK(SAVE) NEW 10)[Return first node pointer] Return(FIRST) By repeatedly invoking Function INSORD, we can easily obtain an ordered linked linear list. The sequence of statements listed below creates a five-element list. FRONT NULL FRONT INSORD(29,FRONT) FRONT INSORD(10,FRONT) FRONT INSORD(25,FRONT) FRONT INSORD(40,FRONT) FRONT INSORD(37,FRONT)

SLICA

Algorithm for deleting node from linear linked list: The general algorithm for deleting a node from a linked list is: 1. If the linked list is empty then write underflow and return 2. Repeat step 3 while the end of the list has not been reached and the node has not been found 3. Obtain the next node in the list and record its predecessor node 4. If the end of the list has been reached then write node not found and return 5. Delete the node from the list 6. Return the node to the availability area Procedure DELETE(X, FIRST). Given X and FIRST, pointer variables whose values denote the address of a node in a linked list and the address of the first node in the linked list, respectively, this procedure deletes the node whose address is given by X. TEMP is sued to find the desired node, and PRED keeps track of the predecessor of TEMP. FIRST is changed only when X is the first element of the list. X Address of the node to be deleted TEMP, PRED Temporary pointers 1) [Empty list?] If FIRST = NULL then Write(UNDERFLOW) Return 2) [Initialize search for x] TEMP FIRST 3) [Find X] Repeat thru step 5 while TEMP X and LINK(TEMP) NULL 4) [Update predecessor marker] PRED TEMP 5) [Move to the next node] TEMP LINK(TEMP) 6) [End of the list?] If TEMP X then Write(NODE NOT FOUND) Return 7) [Delete X] If X = FIRST (Is X the first node?) then FIRST LINK(FIRST) else LINK(PRED) LINK(X) 8) [Return node to availability stack] LINK(X) AVAIL AVAIL X Return

SLICA

Circularly Linked Linear Lists The null pointer in the last node of the linear linked list is replaced with the address of its first node. Such a list is called circularly linked linear list or circular list.

Circular list have certain adv. over singly linked list. In circular list every node is accessible from a given node. From the given node all nodes can be reached by merely chaining through the list. In singly linked list to delete the element X, along with its address, address of the first node of the list is also required. Becos to delete X its predecessor is req. and to search the predecessor all the node are searched from first node. This is not the case with circular list since the search for the predecessor of node X can be initiated from X itself. Finally, certain operations on circular list, such as concatenation and splitting become more efficient. Disadvantage in using the circular list: without some care in processing, it is possible to get into an infinite loop. In processing a circular list, it is imp. to be able to delete the end of the list. This can be done by placing a special node which can be easily identified in the circular list. This special node is called the list head of the circular list. The adv. of having list head is that the list can never be empty.

Fig represents the circular list with a list head where the variable HEAD denotes the address of the list head. The INFO field in the list head node is not used. An empty list can be represented by LINK(HEAD) = HEAD. The algorithm for inserting a node at the head of a circular list with a list head consists of the following steps: NEW NODE INFO(NEW) Y LINK(NEW) LINK(HEAD) LINK(HEAD) NEW DOUBLY LINKED LINEAR LIST Traversal of linear linked list is in only one direction. In certain applications it is desirable that list shd traverse in either a forward or reverse manner. This implies that each node must contain two link fields instead of the usual one. The links are used to denote the predecessor and successor of the node. The link denoting the predecessor of a node is called the left link, and that denoting the successor its right link. A list containing this type of the node is called doubly linked linear list or two-way chain.

SLICA

Fig. represents doubly linked linear list, where L and R are pointer variables denoting the left-most and right-most nodes in the list, respectively. The left-link of the left most node and the right-link of the right most node are both NULL, indicating the end of the list for each direction. The left and right links are denoted by the variables LPTR and RPTR respectively. Algorithm to insert node in doubly linked linear list A general algorithm for inserting a node to the left of a given node in a doubly linked linear list is: 1. Obtain a new node and set its fields 2. If the list is empty then insert the node in the list update the left and right pointers to the list and return 3. If the node is to be inserted at the front of the list then insert the node update the left pointer to the list and return 4. Insert the node in the middle of the list and return

Procedure DOUBINS(L, R, M, X). Given a doubly linked linear list whose leftmost and right-most node addresses are given by the pointer variables L and R, respectively, it is required to insert a node whose address is given by the pointer variable NEW. The left and right links of a node are denoted by LPTR and RPTR, respectively. The information field of a node is denoted by the variable

SLICA

10

INFO. The insertion is to be performed to the left of a specified node with its address given by the pointer variable M. The information to be entered in the node is contained in X. L Pointer to left most node R Pointer to right most node M Address of the node before which the new node has to be inserted X Info of the new node 1) [Obtain new node from availability stack] NEW NODE ( stands for absolute assignment, no need for check availability stack) 2) [Copy information field] INFO(NEW) X 3) [Insertion into an empty list?] If R = NULL then LPTR(NEW) RPTR(NEW) NULL L R NULL Return 4) [Left-most insertion?] If M = L then LPTR(NEW) NULL RPTR(NEW) M LPTR(L) NEW L NEW Return 5) [Insertion in middle] LPTR(NEW) LPTR(M) RPTR(NEW) M LPTR(M) NEW RPTR(LPTR(NEW)) NEW Return
[

Deleting a node from the doubly linked list Doubly linked list are much more efficient with respect to deletions than singly linked list. In singly linked list predecessor of the discarded node is required to be searched. This search is necessary in order to change the link of the predecessor node to a value that wld point to the successor of the node being deleted. And it can be time consuming. Such search is not required in doubly linked list becoz from the address of the node which is to be deleted its predecessor and successor can be easily find out. A general algorithm for deleting a node from a doubly linked list is as follows: 1. If the list is empty then write underflow and return 2. If a single node exits in the list then set the left and right pointers of the list to null else if the leftmost node in the list is being deleted

SLICA

11

then delete node n update left pointer to the list else if rightmost node in the list is being deleted then delete node and update right pointer else delete the node from the middle of list 3. Restore the deleted node to the availability area and return Procedure DOUBDEL(L, R, OLD). Given a doubly linked list with the addresses of the left-most and right-most nodes given by the pointer variables L and R, respectively, it is required to delete the node whose address is contained in the variable OLD. Nodes contain left and right links with names LPTR and RPTR, respectively. L Leftmost node of the list R Rightmost node of the list OLD Address of the node to be deleted 1) [Underflow?] If R = NULL then Write( UNDERFLOW) 2) [Delete node] If L = R (Single node in list) then L R NULL else If OLD = L (Left-most node being deleted) then L RPTR(L) LPTR(L) NULL else If OLD = R (Right-most node being deleted) then R LPTR(R) RPTR(R) NULL else RPTR(LPTR(OLD)) RPTR(OLD) LPTR(RPTR(OLD)) LPTR(OLD) 3) [Return deleted node] Restore(OLD) Return Doubly linked linear lists can be easily used to represent a queue whose number of elements is very volatile.

Here R and F are pointer variables which denote the rear and front of the queue, respectively. The insertion of a node whose address is NEW at the rear of the queue is shown in fig. Here R denotes the address of the rear of the queue after update.

SLICA

12

Steps: RPTR(R) NEW RPTR(NEW) NULL LPTR(NEW) R R NEW Deletion from a doubly linked queue can be represented as follows where F denotes the address of the front node of the updated queue.

Steps: F RPTR(F) LPTR(F) NULL The insertion and deletion algorithms associated with doubly liked linear list can be simplified. A special node is used that will be in the list always, so list can never be empty. N it will be the only node in the empty list. The special node is called the head node of the list. Fig shows doubly linked circular list with head node.

The right link of the right-most node contains the address of the head node and the left link of the head node points to the right-most node. The empty list is represented as follows, where both left and right links of the head node point to itself.

The algorithm for inserting a node to the left of a specified node M now reduces to the following seq. of steps: RPTR(NEW) M LPTR(NEW) LPTR(M) RPTR(LPTR(M)) NEW LPTR(M) NEW The insertion of the node into an empty list can be represented before and after the insertion as:

SLICA

13

The deletion algorithm for a node with an address given by the variable OLD consists of the following steps: RPTR(LPTR(OLD)) RPTR(OLD) LPTR(RPTR(OLD)) LPTR(OLD)

SLICA

14

Vous aimerez peut-être aussi