Data Structures: Hapter
Data Structures: Hapter
Chapter 05
07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 1
Data Structures- Chapter 5
Linked Lists
Start
node node node
Data Next Data Next Data
– The pointer of the last node contains a special value, called the null
pointer which is any invalid address.
– A pointer variable, called START which contains the address of the first
node.
– A special case is the list that has no nodes. Such a list is called the null
list or empty list and is denoted by the null pointer in the variable
START.
Garbage Collection
– Suppose some memory space becomes reusable because a node is
deleted from a list or an entire list is deleted from a program.
Clearly, we want the space to be available for future use. One way
to bring this about is to immediately reinsert the space into the free-
storage list.
Periodic
Collector
Computer
programs Garbage
(Deleted Space)
Takes
space …
avail list Avail List (Free space)
Underflow
– Underflow refers to the situation where one wants to delete data
from a data structure that is empty.
– Underflow will occur in linked lists when START= NULL and
there is a deletion.
Node A Node B
AVAIL
Node N
Free-storage list
07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 14
Data Structures- Chapter 5
Insertion into a linked list
AVAIL
START
Header
Node
x
START
Grounded header list
Header
Node
Circular header lists are frequently used instead of ordinary linked list
because many operations are much easier to state and implement using
header lists. This comes from the following two properties of circular
header lists:
– The null pointer is not used and hence all pointers contain valid
addresses.
– Every (ordinary) node has a predecessor, so the first node may not
require a special case.
• - polygon clipping
• - round robin situations
• - when you need to rotate the list, rather than the reference.
In simple linked lists there is only one way that the list will be
traversed. That is, beginning with the list pointer variable START,
which points to the first node or the header node, and using the next
pointer field LINK to point to the next node in the list, we can traverse
the list in only one direction.
x x
Node N
Two-way list
Discussion:
(a) There is no advantage.
(b) The location of the preceding node is needed. The two-way
list contains this information, whereas with a one-way list we
must traverse the list.
07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 29
Data Structures- Chapter 5
Solved Problem 5.10: Discuss the advantages, if any, of a two-way
list over a one-way list for each of the following operations:
(a) Traversing the list to process each node.
(b) Deleting a node whose location LOC is given.
(c) Searching an unsorted list for a given element ITEM.
(d) Searching a sorted list for a given element ITEM.
(e) Inserting a node before the node with a given location LOC.
(f) Inserting a node after the node with a given location LOC
Discussion:
(c) There is no advantage.
(d) There is no advantage unless we know that ITEM must appear
at the end of the list, in which case we traverse the list
backward.
07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 30
Data Structures- Chapter 5
Solved Problem 5.10: Discuss the advantages, if any, of a two-way
list over a one-way list for each of the following operations:
(a) Traversing the list to process each node.
(b) Deleting a node whose location LOC is given.
(c) Searching an unsorted list for a given element ITEM.
(d) Searching a sorted list for a given element ITEM.
(e) Inserting a node before the node with a given location LOC.
(f) Inserting a node after the node with a given location LOC
Discussion:
(e) The two-way list is more efficient.
(f) There is no advantage.