SlideShare a Scribd company logo
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.
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
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.
Bca data structures linked list mrs.sowmya jyothi
• 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
Bca data structures linked list mrs.sowmya jyothi
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.
Bca data structures linked list mrs.sowmya jyothi
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.
Bca data structures linked list mrs.sowmya jyothi
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.
Bca data structures linked list mrs.sowmya jyothi
• 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.

More Related Content

PDF
Arrays in python
PPTX
Pivot table
PPTX
Stacks IN DATA STRUCTURES
PDF
3. Reviewing the literature
PDF
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
PDF
Unit 1-Introduction to Data Structures-BCA.pdf
PPT
Chapter 1( intro & overview)
PDF
Asma ul husna with Urdu Translation
Arrays in python
Pivot table
Stacks IN DATA STRUCTURES
3. Reviewing the literature
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Unit 1-Introduction to Data Structures-BCA.pdf
Chapter 1( intro & overview)
Asma ul husna with Urdu Translation

What's hot (20)

PPTX
Threaded Binary Tree.pptx
PPT
Circular linked list
PPTX
Heap tree
PPTX
Linked List
PPTX
linked list in data structure
PDF
Binary search tree operations
PPTX
Binary Tree in Data Structure
PPTX
Data structures
PPTX
Link_List.pptx
PPTX
Binary Search Tree
PPTX
Tree in data structure
PPTX
Data Structures (CS8391)
PPTX
Trees in data structures
PPTX
Threaded Binary Tree
PPTX
Doubly Linked List
PPTX
B+ tree intro,uses,insertion and deletion
PPT
Data Structure and Algorithms Binary Search Tree
PPT
Singly link list
PPTX
Threaded binary tree
PPT
358 33 powerpoint-slides_10-trees_chapter-10
Threaded Binary Tree.pptx
Circular linked list
Heap tree
Linked List
linked list in data structure
Binary search tree operations
Binary Tree in Data Structure
Data structures
Link_List.pptx
Binary Search Tree
Tree in data structure
Data Structures (CS8391)
Trees in data structures
Threaded Binary Tree
Doubly Linked List
B+ tree intro,uses,insertion and deletion
Data Structure and Algorithms Binary Search Tree
Singly link list
Threaded binary tree
358 33 powerpoint-slides_10-trees_chapter-10
Ad

Similar to Bca data structures linked list mrs.sowmya jyothi (20)

PPT
linked list1.ppt linked list ppts and notes
PPTX
linked list in dsa python (presentation)
PDF
unit 2- linked list- PPT for btech students.pdf
PDF
unit 2- PPT.pdf
PPTX
DSModule2.pptx
PPTX
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
PPTX
RPT_03_A_Linked List presentation for FE
PPT
Data structure lecture 5
PPTX
Engineering.CSE.DataStructure.Linkedlist.notes
PPTX
Dounly linked list
PPTX
mbit_Unit-2_Linked List.pptx
PPTX
Linked lists 1
PPT
linked list2.ppt linked list part 2 ppt
PDF
Linkedlist in C lanuguage explained in detail
PPTX
Linked List Representation of a Linked List.pptx
PPTX
link list.pptx complete notes detailed ans
PPTX
Linked list in Data Structure and Algorithm
PPTX
Implementation of linked list unit 2.pptx
PDF
DS Module 03.pdf
PPTX
Linked list
linked list1.ppt linked list ppts and notes
linked list in dsa python (presentation)
unit 2- linked list- PPT for btech students.pdf
unit 2- PPT.pdf
DSModule2.pptx
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
RPT_03_A_Linked List presentation for FE
Data structure lecture 5
Engineering.CSE.DataStructure.Linkedlist.notes
Dounly linked list
mbit_Unit-2_Linked List.pptx
Linked lists 1
linked list2.ppt linked list part 2 ppt
Linkedlist in C lanuguage explained in detail
Linked List Representation of a Linked List.pptx
link list.pptx complete notes detailed ans
Linked list in Data Structure and Algorithm
Implementation of linked list unit 2.pptx
DS Module 03.pdf
Linked list
Ad

More from Sowmya Jyothi (19)

PDF
Functions in c mrs.sowmya jyothi
PDF
Strings in c mrs.sowmya jyothi
PDF
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
PDF
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
PDF
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
PDF
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
PDF
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
PDF
Unit II chapter 4 Loops in C
PDF
Unit ii chapter 2 Decision making and Branching in C
PDF
Unit ii chapter 1 operator and expressions in c
PPTX
Overview of C Mrs Sowmya Jyothi
PPT
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
PPT
Introduction to computers MRS. SOWMYA JYOTHI
PPT
Introduction to graphics
PDF
Inter Process Communication PPT
PDF
Internal representation of file chapter 4 Sowmya Jyothi
PDF
Buffer cache unix ppt Mrs.Sowmya Jyothi
PDF
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
PPT
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Functions in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
Unit II chapter 4 Loops in C
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 1 operator and expressions in c
Overview of C Mrs Sowmya Jyothi
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHI
Introduction to graphics
Inter Process Communication PPT
Internal representation of file chapter 4 Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya Jyothi
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi

Recently uploaded (20)

PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PDF
Empowerment Technology for Senior High School Guide
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PPTX
Virtual and Augmented Reality in Current Scenario
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PPTX
History, Philosophy and sociology of education (1).pptx
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Hazard Identification & Risk Assessment .pdf
Unit 4 Computer Architecture Multicore Processor.pptx
FORM 1 BIOLOGY MIND MAPS and their schemes
Environmental Education MCQ BD2EE - Share Source.pdf
AI-driven educational solutions for real-life interventions in the Philippine...
A powerpoint presentation on the Revised K-10 Science Shaping Paper
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
Empowerment Technology for Senior High School Guide
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Virtual and Augmented Reality in Current Scenario
Share_Module_2_Power_conflict_and_negotiation.pptx
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Cambridge-Practice-Tests-for-IELTS-12.docx
Introduction to pro and eukaryotes and differences.pptx
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
History, Philosophy and sociology of education (1).pptx
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Hazard Identification & Risk Assessment .pdf

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 list refers to a linear collection of items. •Data processing frequently involves storing and processing of data organized into lists.
  • 3. • 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.
  • 4. 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).
  • 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 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.
  • 9. 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.
  • 10. 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
  • 11. pointer variable PTR which points to the node that is currently being processes. Accordingly, LINK[PTR] points to the next node to be processed.
  • 12. 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.
  • 13. 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.
  • 14. 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.
  • 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 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)
  • 17. 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:
  • 18. • 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.
  • 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. •Avail list empty-Overflow •Linked list is empty-underfow
  • 21. 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.
  • 22. •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.
  • 24. 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
  • 26. •Insert at the Beginning 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 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.
  • 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 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.
  • 32. • 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
  • 34. 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.
  • 36. 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.
  • 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 •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.
  • 41. • 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.
  • 42. 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.
  • 43. • 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.
  • 44. • 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.
  • 45. 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.