UpdateLinked Lists Week 6 7
UpdateLinked Lists Week 6 7
(Data Structure)
Lecture on
Chapter-4: LINKED LISTS
2 MOHIUDDIN 1
3 THAMINA 7
JALAL 5
4
5 JIHAD 2
6 RAIHAN 3
TOKIA 0
7
4
LINKED LISTS: Representation in Memory
Let LIST be a linked list which will be maintained in the memory
LIST requires…..
Two linear array,
INFO[K]-Information part, and
LINK[K]-nextpointer field of a node of LIST
A variable name-START, indicating the beginning of the LIST.
A nextpointer senitel-NULL, indicates the end of the LIST
Since, the subscripts of the array INFO and LINK will usually be
positive, NULL=0, unless otherwise stated
5
LINKED LISTS: Example
START=9, so INFO[9]= ? N
LINK[9]=3, so, INFO[3]=? 0
LINK[3]=6, so, INFO[6]=? ⎕
LINK[6]=11, so, INFO[11]=? E
LINK[11]=7, so, INFO[7]= ? X
LINK[7]=10, so, INFO[10]= ? I
LINK[10]=4, so, INFO[4]= ? T
LINK[4]=0, so, INFO[0]= ? NULL
6
LINKED LISTS: Example
7
LINKED LISTS: Example
8
LINKED LISTS: Example
In general, the information part of a node may be a record with more
than one data item.
In such a case, the data must be stored in some type of record
structure or in a collection of parallel arrays.
9
LINKED LISTS: Types
The following are the types of linked list:
• Singly Linked list
• Doubly Linked list
• Circular Linked list
• Doubly Circular Linked list
10
LINKED LISTS: Types
1. Singly Linked List
It is the simplest type of linked list in which every node contains some
data and a pointer to the next node of the same data type.
The node contains a pointer to the next node means that the node
stores the address of the next node in the sequence.
A single linked list allows the traversal of data only in one way. Below is
the image for the same:
11
LINKED LISTS: Types
2. Doubly Linked list
A doubly linked list or a two-way linked list is a more complex type of
linked list that contains a pointer to the next as well as the previous
node in sequence.
Therefore, it contains three parts of data, a pointer to the next node,
and a pointer to the previous node. This would enable us to traverse the
list in the backward direction as well. Below is the image for the same:
12
LINKED LISTS: Types
3. Circular Linked List
A circular linked list is that in which the last node contains the pointer to
the first node of the list.
While traversing a circular linked list, we can begin at any node and
traverse the list in any direction forward and backward until we reach
the same node we started. Thus, a circular linked list has no beginning
and no end. Below is the image for the same:
13
LINKED LISTS: Types
4. Doubly Circular linked list
A Doubly Circular linked list or a circular two-way linked list is a more
complex type of linked list that contains a pointer to the next as well as
the previous node in the sequence. The difference between the doubly
linked and circular doubly list is the same as that between a singly
linked list and a circular linked list. The circular doubly linked list does
not contain null in the previous field of the first node. Below is the
image for the same:
14
LINKED LISTS: Traversing
Let LIST be a linked list in memory stored in linear arrays INFO and LINK with START
pointing to the first element and NULL indicating the end of LIST.
15
LINKED LISTS: Traversing Algorithm
Let LIST be a linked list in memory.
LINKED_LIST_Traversing(INFO, LINK, START)
Step1. Set PTR:=START.[Initialize pointer PTR.]
Step2. Repeat Steps 3 and 4 while PTR NULL
Step3. Apply PROCESS to INFO[PTR]
Step4. Set PTR:=LINK[PTR]. [PTR now point to the next node.]
[End of step 2 loop]
Step5. Exit.
16
LINKED LISTS: Practice…
17
LINKED LISTS: Searching
Given:
• LIST- a linked list in memory (Unknown/Unseen)
• ITEM- a specific information.
Objective: Finding the location LOC of the node where ITEM first
appears in LIST.
18
LINKED LISTS: Searching Algorithm-1
SEARCH (INFO, LINK, START, ITEM, LOC) [when list is unsorted]
Step1. Set PTR:=START
Step2. Repeat Step 3 while PTR NULL
Step3. If ITEM=INFO [PTR], then:
Set LOC:=PTR, and Exit.
Else:
Set LOC:=LINK[PTR]. [PTR now points to the next node.]
[End of If structure.]
[End of Step 2 loop.]
Step4. [Search is unsuccessful.] Set LOC:=NULL.
Step5. Exit. 19
LINKED LISTS: Searching Algorithm-2
SEARCH (INFO, LINK, START, ITEM, LOC) [when list is sorted]
Step1. Set PTR:=START
Step2. Repeat Step 3 while PTR NULL
Step3. If ITEM < INFO [PTR], then:
Set PTR:=LINK[PTR] [PTR now points to next node.]
Else if ITEM=INFO[PTR], then:
Set: LOC=PTR. and Exit [Search is successful]
Else:
Set LOC:= NULL, and Exit. [ITEM now exceeds INFO[PTR]]
[End of If structure.]
[End of Step 2 loop.]
Step4. Set LOC:=NULL.
Step5. Exit. Any change in
Descending
algorithm for
Order Sorted
Ascending Order
LIST 20
Sorted LIST?
LINKED LISTS: Searching Algorithm
Limitations:
NB. A binary search algorithm cannot be applied to a sorted linked list.
Since, there is no way of indexing the middle element in the list.
The main
drawbacks in
using a linked list
as data structure
21
LINKED LISTS: Memory Allocation
25
LINKED LISTS: Example 5.11(a)
26
LINKED LISTS: Example 5.11(b)
27
LINKED LISTS: Example 5.11(c)
28
LINKED LISTS: Example 5.12
Suppose LIST(INFO, LINK, START, AVAIL) has memory space for n=5
nodes,
Furthermore, suppose LIST is initially empty.
Your INSTATNT Task
Show the LINKED LIST which consists of START, AVAIL, INFO, LINK
29
LINKED LISTS: Garbage Collection
Time Consuming Method for OS:
Time Efficient Method for OS:
31
LINKED LISTS: Overflow
Sometimes new data are to be inserted into a data structure
but there is no available space (the free-storage list is empty).
This situation is usually called Overflow.
Usually, underflow will occur with our linked lists when START=NULL
and there is a deletion.
33
LINKED LISTS: Insertion
Let LIST be a linked list with successive nodes A and B. Suppose, a node
N is to be inserted into the list between nodes A and B.
Here, it does not take into account the memory space for the new node
N will come from the AVAIL list.
But…..for easier processing, the first node in the AVAIL will be used for
34
the new node N.
LINKED LISTS: Insertion
More exact Illustration of such an insertion
36
LINKED LISTS: Insertion Algorithms
Algorithms which inserts nodes into linked lists come up in various
situations :
Inserts a node at the beginning of the list,
Inserts a node after the node with a given location, and
Inserts a node into a sorted list.
All the algorithms:
assume that linked list is in memory in the form
LIST(INFO, LINK, START, AVAIL)
Have a variable ITEM which contains the new information to be
added to the list.
37
LINKED LISTS: Insertion Algorithms
Since all the insertion algorithm will use a node in the AVAIL list, all of
the algorithm will include the following steps:
Checking to see if space is available in the AVAIL list. If not, that is,
If AVAIL=NULL, then the algorithm will print the message
OVERFLOW.
Removing the first node from the AVAIL list. Using the variable
NEW to keep track of the location of the new node.
NEW:=AVAIL, AVAIL:=LINK[AVAIL]
Copying new information into the new node. i.e.,
INFO[NEW]=ITEM
38
LINKED LISTS: Insertion Algorithms
39
LINKED LISTS: Inserting at the Beginning of a list
40
LINKED LISTS: Inserting at the Beginning ….ALG
41
LINKED LISTS: Inserting at the Beginning
….Exmp
42
LINKED LISTS: Inserting after a given node
Here we don’t care about the future of the deleted nodes of the
linked list 44
LINKED LISTS: Deletion from a Linked List
More Exact procedure:
46
LINKED LISTS: Deletion Algorithms
Algorithms which delete nodes from linked lists come up in various
situations:
The first one deletes the node following a given node, and
The second one deletes the node with a given ITEM of information
47
LINKED LISTS:
48
LINKED LISTS: Deleting the node following a Given Node
49
LINKED LISTS: Deleting the node with a given ITEM
Let LIST be a linked list in memory.
Suppose we are given an ITEM of Information and we want to
delete from the LIST the first node which contain ITEM.
The algorithm has TWO PARTS
51