LINKED LIST
MRS.SOWMYA JYOTHI, SDMCBM, MANGALORE
Reference: Data Structures with C by Seymour
Lipschutz, Schaum’s Outlines Series.
•The term list refers to a linear collection of items.
•Data processing frequently involves storing and
processing of data organized into lists.
• There are two ways of storing data in lists.
1. One way to store such data is by means of arrays.
• The linear relationship between the data elements of an array is
reflected by the physical relationship of the data in memory, not by
any information contained in the data elements themselves. This
makes easy to compute the address of a particular element in the
array.
• On the other hand, storing data in arrays has certain
disadvantages. Example: It is relatively expensive to insert and
delete elements in arrays.
• Since an array occupies a block of space, its size is fixed. The size of
the array cannot be changed.
2. Another way of storing a list in memory is to have each
element in the list contain certain field, called a link or a
pointer which contains the address of the next element in
the list.
• The successive elements in the list need not occupy adjacent
space in the memory. This will make it easier to insert and
delete elements in the list(Linked List).
• LINKED LISTS
• A linked list or one-way list is a linear collection of data elements
called nodes.
• Each node is divided into two parts:
1. the first part, called the info field contains the information of the
element, and
2. the second part, called the link field or nextpointer field contains the
address of the next node in the list. The pointer of the last node
contains a special value called null pointer, with invalid address. The null
pointer is denoted by X in the diagram and it signals the end of the list.
The linked list also contains a list pointer variable called START which
contains the address of the first node in the list.
REPRESENTATION OF LINKED LIST IN MEMORY
• Let LIST be a linked list.
• Then LIST will be maintained in memory, unless otherwise specified or
implied.
• First of all, LIST requires two linear arrays – INFO and LINK- such that
• INFO[K] and LINK[K] contain, respectively, the information part and the
nextpointer field of a node of LIST.
• LIST also requires a variable name – such as START – which contain the
location of the beginning of the list, and a nextpointer sentinel – denoted
by a NULL – which indicates the end of the list.
• Since the subscripts of the arrays INFO and LINK will usually be positive, we
will choose NULL=0, unless and otherwise stated.
The following diagram pictures a linked list
in memory where each node of the list
contains a single character. We can obtain
the actual list of characters, or as follows:
START=9, so INFO[9]=N is the first
character.
LINK[9]=3, so INFO[3]=O is the second
character.
LINK[3]=6, so INFO[6]= (blank) is the third
character.
LINK[6]=11, so INFO[11]=E is the fourth
character.
LINK[11]=7, so INFO[7]=X is the fifth
character.
LINK[7]=10, so INFO[10]=I is the sixth
character.
LINK[10]=4, so INFO[4]=T is the seventh
character.
LINK[4]=0, the NULL value, so the list has
ended.
TRAVERSING A LINKED LIST i=1
• Let LIST be a linked list in memory. i=i+1=2
• Let INFO be the data in the current node,
• LINK points to the address of the next node,
• START is pointing to the address of the first node and NULL indicating the
end of the list.
• Suppose we want to traverse LIST in order to process
each node exactly once.
• The pointer variable PTR which points to the node that is currently being
processes.
• Accordingly, LINK[PTR] points to the next node to be processed.
• Thus the assignment PTR:=LINK[PTR] moves the pointer to the
next node in the list
pointer variable PTR which points to the node that is currently being
processes.
Accordingly, LINK[PTR] points to the next node to be processed.
Algorithm 5.1: (Traversing a Linked List)
•Let LIST be a linked list in the memory. This algorithm
traverses LIST, by applying an operation PROCESS to each
element of the LIST.
•The variable PTR points to the node that is currently being
processed.
1. Set PTR:=START [Initializes pointer to PTR]
2. Repeat Steps 3 and 4 while PTR ≠ NULL
3. Apply PROCESS to INFO[PTR]
4. Set PTR:=LINK[PTR] [PTR now points to the next node]
[End of Step 2 Loop]
5. Exit.
SEARCHING A LINKED LIST
•Let LIST be a linked list in memory.
•Suppose a specific ITEM of information is given, for
finding the location LOC of the node where ITEM
first appears in the LIST.
•Then, one searches for ITEM in LIST by traversing
through the list using a pointer variable PTR and
comparing ITEM with the contents of INFO[PTR] of
each node, one by one, of LIST.
Searching a Linked List (When LIST is Unsorted)
Algorithm 5.2: SEARCH(INFO, LINK, START, ITEM, LOC)
•LIST is the linked list in memory.
•This algorithm finds the location LOC of the node
where ITEM first appears in the LIST, or sets
LOC=NULL.
1. Set PTR:=START [Initializes pointer to PTR]
2. Repeat Steps 3 while PTR ≠ NULL
3. If ITEM=INFO[PTR] then
Set LOC:=PTR, and Exit.
Else:
Set PTR:=LINK[PTR] [PTR now points to the next node]
[End of If structure]
[End of Step 2 Loop]
4. Set LOC:=NULL. [Search is unsuccessful?]
5. Exit.
MEMORY ALLOCATION
The maintenance of linked lists in memory assumes the possibility of
inserting new nodes into the lists and hence requires some mechanism
which provides unused memory space for the new nodes.
• Together with linked lists in memory, a special list is maintained which
consists of unused memory cells.
• This list, which has its own pointer, is called the list of available space
or the free storage list or free pool.
• The unused memory cells can be linked together as AVAIL. Such a data
structure is usually denoted by writing
• LIST (INFO, LINK, START, AVAIL)
GARBAGE COLLECTION
•Suppose some memory space becomes reusable
because a node is deleted from the linked list, it can
be made available for the future use.
1. One way to do this is to immediately reinsert the
space into the free storage list.
•This method is too time-consuming for the operating
system.
2. Another alternative method can be used known as
garbage collection. It works as follows:
• The operating system may periodically collect all the deleted
space into free storage list. The technique which does this is called
garbage collection.
• Garbage collection usually takes place in two steps.
• First, the computer runs through all cells, tagging those cells
which are currently in use and then the computer runs through
the memory, collecting all untagged space onto the free storage
list.
• The garbage collection may take place when there is only some
minimum amount of space or no space at all left in the free-
storage list, or when CPU is idle and has some time for collection.
• Garbage collection is invisible to the programmer.
OVERFLOW AND UNDERFLOW
1. Sometimes new data are to be inserted into a data
structure but there is no available space, i.e, the free storage
list is empty. This situation is called as overflow.
• The programmer may handle overflow by printing a message
OVERFLOW when AVAIL=NULL.
2. The term underflow refers to some situation where one
wants to delete data from a data structure that is empty.
• The programmer may handle underflow by printing a
message UNDERFLOW when START=NULL and there is a
deletion.
•Avail list empty-Overflow
•Linked list is empty-underfow
INSERTION INTO A LINKED LIST
• Let LIST be a linked list with successive node A and node B.
• Suppose a node N is to be inserted into the list between node
A and node B.
• The node A now points to the new node N, and node N points to
node B, to which node A previously pointed.
•The first node in the AVAIL list will be used for the new
node N.
•The three pointer fields are changed as follows.
1) The link (nextpointer) field of node A now points to the
new node N, to which AVAIL, previously pointed.
2) AVAIL now points to the second node in the free pool, to
which node N previously pointed.
3) The link (nextpointer) field of node N now points to node
B, to which node A previously pointed.
NEW:=AVAIL, AVAIL:=LINK[AVAIL]
INFO[NEW]:=ITEM
INSERTION ALGORITHMS
• Since insertion algorithms will use a node in the AVAIL list, all of
the algorithms will include the following steps:
a) Checking to see if free space is available in the AVAIL list. If not,
that is, if AVAIL=NULL, then the algorithm will print the message
OVERFLOW.
b) Removing the first node from the AVAIL list.
Using the variable NEW to keep track of the location of the new
node, this step can be implemented by the pair of assignments
NEW:=AVAIL, AVAIL:=LINK[AVAIL]
c) Copying new information into the new node.
In other words, INFO[NEW]:=ITEM
INFO[NEW]:=ITEM
2. AVAIL:=LINK[AVAIL]
NEW:=AVAIL, AVAIL:=LINK[AVAIL]
1. NEW:=AVAIL
•Insert at the Beginning of a List
INFO[NEW]:=ITEM LINK [NEW]:=START START:=NEW
1
2
Algorithm 5.4: INSFIRST(INFO, LINK, START, AVAIL, ITEM)
• This algorithm inserts ITEM as the first node in the list.
1. [OVERFLOW?] IF AVAIL=NULL, then Write: OVERFLOW, and Exit.
2. [Remove first node from AVAIL list]
Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]
3. Set INFO[NEW]:=ITEM [Copies new data into the new node]
4. Set LINK [NEW]:=START [New node now points to the original first node]
5. Set START:=NEW [Change START so it points to the new node]
6. Exit
INSERTING AFTER A GIVEN NODE
• Suppose either the value of LOC or the location LOC is given, the
following algorithm will insert ITEM into LIST so that ITEM follows
node A or when LOC is null ITEM is the first node.
Algorithm 5.5: INSLOC (INFO, LINK, START,AVAIL,LOC, ITEM)
• This algorithm inserts ITEM so that ITEM follows the node with location LOC or
inserts ITEM as the first node when LOC =NULL
1. IF AVAIL=NULL, then Write: OVERFLOW, and Exit. [OVERFLOW?]
2. [Remove first node from AVAIL list]
Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]
3. Set INFO[NEW]:=ITEM [Copies new data into the new node]
4. If LOC=NULL, then:
[Insert as the first node.]
Set LINK[NEW]:=START and START:=NEW.
[Insert after the node with location LOC.]
Else:
Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:=NEW.
5. Exit [End of If Structure]
DELETION FROM A LINKED LIST
•Let LIST be a linked list with a node N between node
A and node B (Fig-a).
•Suppose node N is to be deleted from the linked list
(Fig-b).
•The deletion occurs as soon as the link (nextpointer)
field of node A is changed so that it points to node B.
• Suppose linked list is maintained in the form,
• LIST(INFO, LINK, START, AVAIL)
• When node is deleted from the LIST, it is immediately returned
to the memory space at the beginning of the AVAIL list.
The three pointer fields are changed as follows:
1) The link (nextpointer) field of node A now points to node B,
where node N previously pointed.
2) The link (nextpointer) field of N now points to the original
first node in the free pool, where AVAIL previously pointed.
3) AVAIL now points to the deleted node N
DELETION ALGORITHMS
•Algorithms which delete nodes form linked lists come
up in various situations.
1. First one is deleting the node following a given node
and
2. The second one deletes a node with given
information.
•All deletion algorithms will return the memory space of
the deleted node N to the beginning of the AVAIL list.
Deleting the node following a Given Node
•Let LIST be a linked list in the memory.
•Suppose LOC is the location of a node N in LIST, the
following algorithm deletes N from the list.
Algorithm 5.8:
DEL (INFO, LINK, START, AVAIL, LOC, LOCP)
•This algorithm deletes a node N with location LOC.
•LOCP is the location of the node which precedes N, or
when N is the first node LOCP=NULL.
1. If LOCP:=NULL, then:
Set START:=LINK[START] [Deletes the First node.]
Else:
Set LINK[LOCP]:=LINK[LOC] [Deletes node N.]
2. [Return deleted node to the AVAIL list.]
Set Link[LOC]:=AVAIL and AVAIL:=LOC.
3. Exit.
CIRCULARLY LINKED LIST
•In circularly linked list, the link field of the last node
points to the first node of the list.
•This type of list is mainly used in lists that allow access
to nodes in the middle of the list without starting at
the beginning.
•The node does not contain NULL pointers as in a singly
linked list.
•The link field of the last node is connected to the
information part of the first node.
• Insertion and deletion from a circularly linked list follows the
same pattern used in a singly linked list.
1. However, in this case, the last node points to the first node.
Therefore, when inserting or deleting the last node, besides
updating the rear pointer, the last pointer must updated to point
to the first node.
• However, there will be a problem during searching operation.
2. In case of singly linked list, search will be completed if the item
is found or if the end of list is reached.
But in case of circularly linked list, when the last node is
reached, it will point to the beginning. So in case of unsuccessful
search, to avoid infinite loop starting address needs to be saved
and search must be stopped when circled around it.
TWO-WAY LISTS (OR DOUBLY LINKED LISTS)
•Singly linked lists and circularly linked lists are called
one-way lists, since there is only one way that the list
can be traversed, which means, that the lists can be
traversed only in one direction.
•A doubly linked list can be traversed in two directions:
in the usual forward direction from the beginning of
the list to the end, or in the backward direction from
the end of the list to the beginning. Furthermore, from
a given node N, in the list, one can access both the
preceding and the next node.
• A two-way list is a linear collection of data elements, called
nodes, where each node N is divided into three parts:
1. An information field INFO which contains data of Node N.
2. A pointer field FORW which contains the location of the
next node in the list.
3. A pointer field BACK which contains the location of the
preceding node in the list.
• The list also requires two list pointer variables:
1. FIRST, which points to the first node in the list and LAST, which points to
the last node in the list as shown in the figure above.
2. NULL pointer appears in the FORW field of the last node and BACK field
of the first node in the list.
• FIRST and FORW can be used to traverse the list in forward direction and
LAST and BACK can be used to traverse the list in backward direction.
• Suppose LOCA and LOCB are the locations respectively, of Nodes A and
Node B in a two-way list.
• Then the way that the pointers FORW and BACK are defined gives the
following:
•Pointer property: FORW[LOCA]=LOCB if and only if BACK[LOCB]=LOCA.
• In other words, the statement that the Node B follows Node A is equivalent
to the statement that Node A precedes Node B.
Operations on Two-Way Lists
• Traversing: A list can be traversed and each node can be processed
only once.
• Searching: A search for an ITEM in the linked list can be carried out
to find the location LOC of the ITEM in the list.
• Deleting: A node N can be deleted from the two-way list with the
help of FORW and BACK pointers as follows:
• FORW[BACK[LOC]]:=FORW[LOC] and
BACK[FORW[LOC]]:=BACK[LOCK],
• The deleted node N can then be returned to the list of available
nodes AVAIL by the assignments:
• FORW[LOC]:=AVAIL and AVAIL:=LOC.

Bca data structures linked list mrs.sowmya jyothi

  • 1.
    LINKED LIST MRS.SOWMYA JYOTHI,SDMCBM, MANGALORE Reference: Data Structures with C by Seymour Lipschutz, Schaum’s Outlines Series.
  • 2.
    •The term listrefers to a linear collection of items. •Data processing frequently involves storing and processing of data organized into lists.
  • 3.
    • There aretwo ways of storing data in lists. 1. One way to store such data is by means of arrays. • The linear relationship between the data elements of an array is reflected by the physical relationship of the data in memory, not by any information contained in the data elements themselves. This makes easy to compute the address of a particular element in the array. • On the other hand, storing data in arrays has certain disadvantages. Example: It is relatively expensive to insert and delete elements in arrays. • Since an array occupies a block of space, its size is fixed. The size of the array cannot be changed.
  • 4.
    2. Another wayof storing a list in memory is to have each element in the list contain certain field, called a link or a pointer which contains the address of the next element in the list. • The successive elements in the list need not occupy adjacent space in the memory. This will make it easier to insert and delete elements in the list(Linked List).
  • 5.
    • LINKED LISTS •A linked list or one-way list is a linear collection of data elements called nodes. • Each node is divided into two parts: 1. the first part, called the info field contains the information of the element, and 2. the second part, called the link field or nextpointer field contains the address of the next node in the list. The pointer of the last node contains a special value called null pointer, with invalid address. The null pointer is denoted by X in the diagram and it signals the end of the list. The linked list also contains a list pointer variable called START which contains the address of the first node in the list.
  • 8.
    REPRESENTATION OF LINKEDLIST IN MEMORY • Let LIST be a linked list. • Then LIST will be maintained in memory, unless otherwise specified or implied. • First of all, LIST requires two linear arrays – INFO and LINK- such that • INFO[K] and LINK[K] contain, respectively, the information part and the nextpointer field of a node of LIST. • LIST also requires a variable name – such as START – which contain the location of the beginning of the list, and a nextpointer sentinel – denoted by a NULL – which indicates the end of the list. • Since the subscripts of the arrays INFO and LINK will usually be positive, we will choose NULL=0, unless and otherwise stated.
  • 9.
    The following diagrampictures a linked list in memory where each node of the list contains a single character. We can obtain the actual list of characters, or as follows: START=9, so INFO[9]=N is the first character. LINK[9]=3, so INFO[3]=O is the second character. LINK[3]=6, so INFO[6]= (blank) is the third character. LINK[6]=11, so INFO[11]=E is the fourth character. LINK[11]=7, so INFO[7]=X is the fifth character. LINK[7]=10, so INFO[10]=I is the sixth character. LINK[10]=4, so INFO[4]=T is the seventh character. LINK[4]=0, the NULL value, so the list has ended.
  • 10.
    TRAVERSING A LINKEDLIST i=1 • Let LIST be a linked list in memory. i=i+1=2 • Let INFO be the data in the current node, • LINK points to the address of the next node, • START is pointing to the address of the first node and NULL indicating the end of the list. • Suppose we want to traverse LIST in order to process each node exactly once. • The pointer variable PTR which points to the node that is currently being processes. • Accordingly, LINK[PTR] points to the next node to be processed. • Thus the assignment PTR:=LINK[PTR] moves the pointer to the next node in the list
  • 11.
    pointer variable PTRwhich points to the node that is currently being processes. Accordingly, LINK[PTR] points to the next node to be processed.
  • 12.
    Algorithm 5.1: (Traversinga Linked List) •Let LIST be a linked list in the memory. This algorithm traverses LIST, by applying an operation PROCESS to each element of the LIST. •The variable PTR points to the node that is currently being processed. 1. Set PTR:=START [Initializes pointer to PTR] 2. Repeat Steps 3 and 4 while PTR ≠ NULL 3. Apply PROCESS to INFO[PTR] 4. Set PTR:=LINK[PTR] [PTR now points to the next node] [End of Step 2 Loop] 5. Exit.
  • 13.
    SEARCHING A LINKEDLIST •Let LIST be a linked list in memory. •Suppose a specific ITEM of information is given, for finding the location LOC of the node where ITEM first appears in the LIST. •Then, one searches for ITEM in LIST by traversing through the list using a pointer variable PTR and comparing ITEM with the contents of INFO[PTR] of each node, one by one, of LIST.
  • 14.
    Searching a LinkedList (When LIST is Unsorted) Algorithm 5.2: SEARCH(INFO, LINK, START, ITEM, LOC) •LIST is the linked list in memory. •This algorithm finds the location LOC of the node where ITEM first appears in the LIST, or sets LOC=NULL.
  • 15.
    1. Set PTR:=START[Initializes pointer to PTR] 2. Repeat Steps 3 while PTR ≠ NULL 3. If ITEM=INFO[PTR] then Set LOC:=PTR, and Exit. Else: Set PTR:=LINK[PTR] [PTR now points to the next node] [End of If structure] [End of Step 2 Loop] 4. Set LOC:=NULL. [Search is unsuccessful?] 5. Exit.
  • 16.
    MEMORY ALLOCATION The maintenanceof linked lists in memory assumes the possibility of inserting new nodes into the lists and hence requires some mechanism which provides unused memory space for the new nodes. • Together with linked lists in memory, a special list is maintained which consists of unused memory cells. • This list, which has its own pointer, is called the list of available space or the free storage list or free pool. • The unused memory cells can be linked together as AVAIL. Such a data structure is usually denoted by writing • LIST (INFO, LINK, START, AVAIL)
  • 17.
    GARBAGE COLLECTION •Suppose somememory space becomes reusable because a node is deleted from the linked list, it can be made available for the future use. 1. One way to do this is to immediately reinsert the space into the free storage list. •This method is too time-consuming for the operating system. 2. Another alternative method can be used known as garbage collection. It works as follows:
  • 18.
    • The operatingsystem may periodically collect all the deleted space into free storage list. The technique which does this is called garbage collection. • Garbage collection usually takes place in two steps. • First, the computer runs through all cells, tagging those cells which are currently in use and then the computer runs through the memory, collecting all untagged space onto the free storage list. • The garbage collection may take place when there is only some minimum amount of space or no space at all left in the free- storage list, or when CPU is idle and has some time for collection. • Garbage collection is invisible to the programmer.
  • 19.
    OVERFLOW AND UNDERFLOW 1.Sometimes new data are to be inserted into a data structure but there is no available space, i.e, the free storage list is empty. This situation is called as overflow. • The programmer may handle overflow by printing a message OVERFLOW when AVAIL=NULL. 2. The term underflow refers to some situation where one wants to delete data from a data structure that is empty. • The programmer may handle underflow by printing a message UNDERFLOW when START=NULL and there is a deletion.
  • 20.
  • 21.
    INSERTION INTO ALINKED LIST • Let LIST be a linked list with successive node A and node B. • Suppose a node N is to be inserted into the list between node A and node B. • The node A now points to the new node N, and node N points to node B, to which node A previously pointed.
  • 22.
    •The first nodein the AVAIL list will be used for the new node N. •The three pointer fields are changed as follows. 1) The link (nextpointer) field of node A now points to the new node N, to which AVAIL, previously pointed. 2) AVAIL now points to the second node in the free pool, to which node N previously pointed. 3) The link (nextpointer) field of node N now points to node B, to which node A previously pointed.
  • 23.
  • 24.
    INSERTION ALGORITHMS • Sinceinsertion algorithms will use a node in the AVAIL list, all of the algorithms will include the following steps: a) Checking to see if free space is available in the AVAIL list. If not, that is, if AVAIL=NULL, then the algorithm will print the message OVERFLOW. b) Removing the first node from the AVAIL list. Using the variable NEW to keep track of the location of the new node, this step can be implemented by the pair of assignments NEW:=AVAIL, AVAIL:=LINK[AVAIL] c) Copying new information into the new node. In other words, INFO[NEW]:=ITEM
  • 25.
  • 26.
    •Insert at theBeginning of a List INFO[NEW]:=ITEM LINK [NEW]:=START START:=NEW 1 2
  • 27.
    Algorithm 5.4: INSFIRST(INFO,LINK, START, AVAIL, ITEM) • This algorithm inserts ITEM as the first node in the list. 1. [OVERFLOW?] IF AVAIL=NULL, then Write: OVERFLOW, and Exit. 2. [Remove first node from AVAIL list] Set NEW:=AVAIL and AVAIL:=LINK[AVAIL] 3. Set INFO[NEW]:=ITEM [Copies new data into the new node] 4. Set LINK [NEW]:=START [New node now points to the original first node] 5. Set START:=NEW [Change START so it points to the new node] 6. Exit
  • 28.
    INSERTING AFTER AGIVEN NODE • Suppose either the value of LOC or the location LOC is given, the following algorithm will insert ITEM into LIST so that ITEM follows node A or when LOC is null ITEM is the first node.
  • 29.
    Algorithm 5.5: INSLOC(INFO, LINK, START,AVAIL,LOC, ITEM) • This algorithm inserts ITEM so that ITEM follows the node with location LOC or inserts ITEM as the first node when LOC =NULL 1. IF AVAIL=NULL, then Write: OVERFLOW, and Exit. [OVERFLOW?] 2. [Remove first node from AVAIL list] Set NEW:=AVAIL and AVAIL:=LINK[AVAIL] 3. Set INFO[NEW]:=ITEM [Copies new data into the new node] 4. If LOC=NULL, then: [Insert as the first node.] Set LINK[NEW]:=START and START:=NEW. [Insert after the node with location LOC.] Else: Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:=NEW. 5. Exit [End of If Structure]
  • 30.
    DELETION FROM ALINKED LIST •Let LIST be a linked list with a node N between node A and node B (Fig-a). •Suppose node N is to be deleted from the linked list (Fig-b). •The deletion occurs as soon as the link (nextpointer) field of node A is changed so that it points to node B.
  • 32.
    • Suppose linkedlist is maintained in the form, • LIST(INFO, LINK, START, AVAIL) • When node is deleted from the LIST, it is immediately returned to the memory space at the beginning of the AVAIL list. The three pointer fields are changed as follows: 1) The link (nextpointer) field of node A now points to node B, where node N previously pointed. 2) The link (nextpointer) field of N now points to the original first node in the free pool, where AVAIL previously pointed. 3) AVAIL now points to the deleted node N
  • 34.
    DELETION ALGORITHMS •Algorithms whichdelete nodes form linked lists come up in various situations. 1. First one is deleting the node following a given node and 2. The second one deletes a node with given information. •All deletion algorithms will return the memory space of the deleted node N to the beginning of the AVAIL list.
  • 36.
    Deleting the nodefollowing a Given Node •Let LIST be a linked list in the memory. •Suppose LOC is the location of a node N in LIST, the following algorithm deletes N from the list. Algorithm 5.8: DEL (INFO, LINK, START, AVAIL, LOC, LOCP) •This algorithm deletes a node N with location LOC. •LOCP is the location of the node which precedes N, or when N is the first node LOCP=NULL.
  • 37.
    1. If LOCP:=NULL,then: Set START:=LINK[START] [Deletes the First node.] Else: Set LINK[LOCP]:=LINK[LOC] [Deletes node N.] 2. [Return deleted node to the AVAIL list.] Set Link[LOC]:=AVAIL and AVAIL:=LOC. 3. Exit.
  • 39.
    CIRCULARLY LINKED LIST •Incircularly linked list, the link field of the last node points to the first node of the list. •This type of list is mainly used in lists that allow access to nodes in the middle of the list without starting at the beginning. •The node does not contain NULL pointers as in a singly linked list. •The link field of the last node is connected to the information part of the first node.
  • 41.
    • Insertion anddeletion from a circularly linked list follows the same pattern used in a singly linked list. 1. However, in this case, the last node points to the first node. Therefore, when inserting or deleting the last node, besides updating the rear pointer, the last pointer must updated to point to the first node. • However, there will be a problem during searching operation. 2. In case of singly linked list, search will be completed if the item is found or if the end of list is reached. But in case of circularly linked list, when the last node is reached, it will point to the beginning. So in case of unsuccessful search, to avoid infinite loop starting address needs to be saved and search must be stopped when circled around it.
  • 42.
    TWO-WAY LISTS (ORDOUBLY LINKED LISTS) •Singly linked lists and circularly linked lists are called one-way lists, since there is only one way that the list can be traversed, which means, that the lists can be traversed only in one direction. •A doubly linked list can be traversed in two directions: in the usual forward direction from the beginning of the list to the end, or in the backward direction from the end of the list to the beginning. Furthermore, from a given node N, in the list, one can access both the preceding and the next node.
  • 43.
    • A two-waylist is a linear collection of data elements, called nodes, where each node N is divided into three parts: 1. An information field INFO which contains data of Node N. 2. A pointer field FORW which contains the location of the next node in the list. 3. A pointer field BACK which contains the location of the preceding node in the list.
  • 44.
    • The listalso requires two list pointer variables: 1. FIRST, which points to the first node in the list and LAST, which points to the last node in the list as shown in the figure above. 2. NULL pointer appears in the FORW field of the last node and BACK field of the first node in the list. • FIRST and FORW can be used to traverse the list in forward direction and LAST and BACK can be used to traverse the list in backward direction. • Suppose LOCA and LOCB are the locations respectively, of Nodes A and Node B in a two-way list. • Then the way that the pointers FORW and BACK are defined gives the following: •Pointer property: FORW[LOCA]=LOCB if and only if BACK[LOCB]=LOCA. • In other words, the statement that the Node B follows Node A is equivalent to the statement that Node A precedes Node B.
  • 45.
    Operations on Two-WayLists • Traversing: A list can be traversed and each node can be processed only once. • Searching: A search for an ITEM in the linked list can be carried out to find the location LOC of the ITEM in the list. • Deleting: A node N can be deleted from the two-way list with the help of FORW and BACK pointers as follows: • FORW[BACK[LOC]]:=FORW[LOC] and BACK[FORW[LOC]]:=BACK[LOCK], • The deleted node N can then be returned to the list of available nodes AVAIL by the assignments: • FORW[LOC]:=AVAIL and AVAIL:=LOC.