0% found this document useful (0 votes)
13 views31 pages

Data Structures: Hapter

Uploaded by

munshijubair7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views31 pages

Data Structures: Hapter

Uploaded by

munshijubair7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Data Structures

Chapter 05
07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 1
Data Structures- Chapter 5
 Linked Lists

– A link list or one-way list is a linear collection of data elements, called


nodes, where the linear order is given by means of pointers.
– That is, each node is divided into two parts: the first part contains the
information of the elements, and the second part, called the link field or
next pointer field, contains the address of the next node in the list.
– The following figure is a schematic diagram of a linked list with 3
nodes.

Start
node node node
Data Next Data Next Data

Linked list with 3 nodes


07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 2
Data Structures- Chapter 5
 Linked Lists

– 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.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 3


Data Structures- Chapter 5
 Memory Representation of the Linked List

– Let LIST be a linked list. Then LIST will be maintained in


memory as follows.
– 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 next pointer field
of a node of the LIST.
– LIST also requires a variable name such as START which
contains the location of the beginning of the list and a next
pointer sentinel denoted by NULL which indicates the end
of the list.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 4


Data Structures- Chapter 5
 Distinguish between Array and Link List
– Both are linear data structures.
– Array size is fixed (dense list or static data structure) but linked list
size is not fixed (dynamic data structure).
– Array requires less space as only information is stored. Linked list
requires more space as pointers are also stored along with
information.
– Elements are stored in consecutive memory locations. In linked list
Elements may or may not be stored in consecutive memory locations.
– Array needs movements of elements for insertion and deletion.
Linked list does not need movement of nodes for insertion and
deletion.
– It can’t be extended or reduced according to requirements. Linked list
can be extended or reduced according to requirements.
– Space is wasted in Array. Space is not wasted in Linked List.
07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 5
Data Structures- Chapter 5
 Algorithm 5.1: Write an algorithm that traverses a linked list.
Let LIST be a linked list in memory. This algorithm traverses LIST,
applying an operation PROCESS to each element of LIST. The variable
PTR points to the node currently being processed.

Step 1. Set PTR = START


Step 2. Repeat Step 3 and 4 while PTR ≠ NULL
Step 3. Apply PROCESS to INFO[PTR]
Step 4. Set PTR = LINK[PTR]
[End of Step 2 loop.]
Step 5. Exit.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 6


Data Structures- Chapter 5
 Procedure: Write a procedure that prints the information at each
node of a linked list.

PRINT (INFO, LINK, START)


This procedure prints the information at each node of a linked list.

Step 1. Set PTR = START


Step 2. Repeat Step 3 and 4 while PTR ≠ NULL
Step 3. Write: INFO[PTR]
Step 4. Set PTR = LINK[PTR]
[End of Step 2 loop.]
Step 5. Return.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 7


Data Structures- Chapter 5
 Procedure: Write a procedure that finds the number of elements in a
linked list.

COUNT (INFO, LINK, START, NUM)


This procedure finds the number of elements in a linked list.

Step 1. Set NUM = 0


Step 2. Set PTR = START
Step 3. Repeat Step 4 and 5 while PTR ≠ NULL
Step 4. NUM = NUM + 1
Step 5. Set PTR = LINK [PTR]
[End of Step 3 loop.]
Step 6. Return.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 8


Data Structures- Chapter 5
 Algorithm 5.2: Write an algorithm that searches an item in a linked
list.
SEARCH (INFO, LINK, START, ITEM, LOC)
LIST is a linked list in memory. This algorithm finds the location LOC of
the node where ITEM first appears in LIST, or sets LOC=NULL.
Step 1. Set PTR = START
Step 2. Repeat Step 3 while PTR ≠ NULL
Step 3. If ITEM=INFO[PTR], then
Set LOC = PTR and exit.
Else
Set PTR = LINK [PTR]
[End of If structure]
[End of Step 2 loop]
Step 4. Set LOC = NULL
Step 5. Exit.
07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 9
Data Structures- Chapter 5
 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.

 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.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 10


Data Structures- Chapter 5
 Garbage Collection
– The operating system may periodically collect all the deleted space
onto the free-storage list. Any technique which does this collection
is called garbage collection. Garbage collection usually takes place
in two steps. First the computer runs through all lists, tagging those
cells which are currently in use, and then 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 the CPU is idle and has time to do the collection. The
garbage collection is invisible to the programmer.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 11


Data Structures- Chapter 5
 Garbage Collection

Periodic
Collector
Computer
programs Garbage
(Deleted Space)

Takes
space …
avail list Avail List (Free space)

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 12


Data Structures- Chapter 5
 Overflow
– Sometimes new data are to be inserted into a data structure but
there is no available space. This situation is usually called overflow.
– Overflow will occur in linked list when AVAIL = NULL and there
is an insertion.

 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.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 13


Data Structures- Chapter 5
 Insertion into a linked list
– Let LIST be a linked list with successive nodes A and B as pictured
below. Suppose a node N is to be inserted into the list between
START
nodes A and B. The schematic diagram of such an insertion appears
below.
Node A Node B

START (a) Before insertion

Node A Node B

(b) After insertion

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

Three pointer fields are changed as follows:


– The next pointer field of node A now points to the new node N, to
which AVAIL previously pointed.
– AVAIL now point to the second node in the free pool, to which
node N previously pointed.
– The next pointer field of node N now points to node B, to which
node A previously pointed.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 15


Data Structures- Chapter 5
 Algorithm 5.4: Write an algorithm that inserts an item as the first
node in a linked list.

INSFIRST(INFO, LINK, START, AVAIL, ITEM)


This algorithm inserts ITEM as the first node in the list.

Step 1. If AVAIL= NULL, then Write: OVERFLOW, and Exit.


Step 2. Set NEW = AVAIL and AVAIL = LINK [AVAIL].
Step 3. Set INFO [NEW] = ITEM.
Step 4. Set LINK [NEW] = START.
Step 5. Set START = NEW.
Step 6. Exit.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 16


Data Structures- Chapter 5
 Algorithm 5.5: Write an algorithm that inserts an item after a given
node in a linked list.
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.

Step 1. If AVAIL = NULL, then Write: OVERFLOW, and Exit.


Step 2. Set NEW = AVAIL and AVAIL = LINK [AVAIL].
Step 3. Set INFO [NEW] = ITEM.
Step 4. If LOC =NULL, then
Set LINK [NEW] = START and START = NEW.
Else
Set LINK [NEW] = LINK [LOC] and LINK [LOC] = NEW.
[End of If structure.]
Step 5. Exit.
07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 17
Data Structures- Chapter 5
 Deletion from a linked list
– Let LIST be a linked list with a node N between nodes A and B, as
pictured below. Suppose node N is to be deleted from the linked
list. The schematic diagram of such a deletion appears below.
START
Node A Node N Node B

START (a) Before deletion

Node A Node N Node B

(b) After deletion

AVAIL

Free storage list

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 18


Data Structures- Chapter 5
 Deletion from a linked list

Three pointer fields are changed as follows:


– The next pointer field of node A now points to node B, where node
N previously pointed.
– The next pointer field of N now points to the original first node in
the free pool, where previously pointed.
– AVAIL now points to the deleted node N.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 19


Data Structures- Chapter 5
 Algorithm 5.8: Write an algorithm that deletes the node following a
given node.
DEL (INFO, LINK, START, AVAIL, LOC, LOCP)
This algorithm deletes the node N with location LOC. LOCP is the
location of the node which precedes N or, when N is the first node,
LOCP = NULL.
Step 1. If LOCP = NULL, then
Set START =LINK [START].
Else
Set LINK [LOCP] = LINK [LOC]
[End of If structure.]
Step 2. Set LINK [LOC] = AVAIL and AVAIL = LOC.
Step 3. Exit.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 20


Data Structures- Chapter 5
 HEADER LINKED LISTS
A header linked list is a linked list which always contains a special
node, called the header node at beginning of the list. The following are
two kinds of widely used header lists:

– A grounded header list is a header list where the last node


contains the null pointer.
– A circular header list is a header list where the last node points
back to the header node.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 21


Data Structures- Chapter 5
 HEADER LINKED LISTS

START

Header
Node
x
START
Grounded header list
Header
Node

Circular header list

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 22


Data Structures- Chapter 5
 HEADER LINKED LISTS

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.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 23


Data Structures- Chapter 5
Example of application of circular linked list

• A good example of an application where circular linked list should be used


is a timesharing problem solved by the operating system.
• In a timesharing environment, the operating system must maintain a list
of present users and must alternately allow each user to use a small slice of
CPU time, one user at a time.
• The operating system will pick a user, let him/her use a small amount of
CPU time and then move on to the next user, etc.
• For this application, there should be no NIL pointers unless there is
absolutely no one requesting CPU time.

• - polygon clipping
• - round robin situations
• - when you need to rotate the list, rather than the reference.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 24


Data Structures- Chapter 5
 Algorithm 5.11: Write an algorithm that traverses a circular header
list.

Let LIST be a circular header list in memory. This algorithm traverses


LIST, applying an operation PROCESS to each node of LIST.

Step 1. Set PTR = LINK[START]


Step 2. Repeat Step 3 and 4 while PTR ≠ START
Step 3. Apply PROCESS to INFO[PTR]
Step 4. Set PTR = LINK[PTR]
[End of Step 2 loop.]
Step 5. Exit.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 25


Data Structures- Chapter 5
 Two-way lists

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.

A two-way list is a new list structure which can be traversed in two


directions:
- in the usual forward direction from the beginning of the list to
the end
- in the backward direction from the end of the list to the
beginning.
07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 26
Data Structures- Chapter 5
 Two-way lists

INFO field of node N


BACK pointer field of node N
FIRST LAST
FORW pointer field of node N

x x

Node N

Two-way list

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 27


Data Structures- Chapter 5
 Two-way lists
A two-way list is a linear collection of data element, called nodes,
where each node N is divided into three parts:

– An information field INFO which contains the data of N


– A pointer field FORW which contains the location of the next node
in the list
– A pointer field BACK which contains the location of the preceding
node in the list.

The list requires two pointer variables:


– FIRST which point to the first node in the list and
– LAST which points to the last node in the list.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 28


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:
(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.

07-Oct-24 Md. Golam Moazzam, Dept. of CSE, JU 31

You might also like