SlideShare a Scribd company logo
1 of 53
DATA STRUCTURES
LINKED LIST
Dr.RAJESWARI.A
9/14/2021 1
Need for Data Structure?
1, AHAMED SAHIB M L, DS, CS, 46, 42, 0, 57, 0 2, ANANTH
N, H, CS, 64, 37, 0, 80, 03, ASHWATH NIVAS M, DS, CS,
52, 52, 0, 69, 04, DHINESH GV, DS, Bio, 28, 25, 0, 36, 05,
DIWAKAR N, H, CS, OD, 57, 0, 74, 0 6, GIRIDHARAN S M,
DS, CS, 24, 33, 0, 25, 07, HAJA SABEER A, DS, CS, 44, 37, 0,
43, 08, KARTHIK BALAJI B, H, Bio, 18, 16, 0, 50, 09,
KAYAAN ROHITH A, DS, Bio, 34, 18, 0, 35, 010, KESHAV J,
DS, Bio, 50, 50, 0, 80, 011, LOKESH KUMAR S, DS, Bio, 50,
16, 0, 38, 012, MARIYA BANATIC J, DS, CS, OD, 55, 0, OD,
013, PON SANKAR G, DS, CS, 38, A, 0, 57, 014, PRABHU D,
H, CS, 62, 52, 0, OD, 015, PREM S J, DS, Bio, A, 19, 0, 34,
9/14/2021 2
Organized data
SL. NO. NAME OF THE STUDENT UT1 CT1 UT2 CT2 MODEL
1 AHAMED SAHIB M L DS CS 46 42 0 57 0
2 ANANTH N H CS 64 37 0 80 0
3 ASHWATH NIVAS M DS CS 52 52 0 69 0
4 DHINESH GV DS Bio 28 25 0 36 0
5 DIWAKAR N H CS OD 57 0 74 0
6 GIRIDHARAN S M DS CS 24 33 0 25 0
7 HAJA SABEER A DS CS 44 37 0 43 0
8 KARTHIK BALAJI B H Bio 18 16 0 50 0
9 KAYAAN ROHITH A DS Bio 34 18 0 35 0
10 KESHAV J DS Bio 50 50 0 80 0
11 LOKESH KUMAR S DS Bio 50 16 0 38 0
12 MARIYA BANATIC J DS CS OD 55 0 OD 0
13 PON SANKAR G DS CS 38 A 0 57 0
14 PRABHU D H CS 62 52 0 OD 0
15 PREM S J DS Bio A 19 0 34 0
9/14/2021 3
Introduction to Data Structures
Data
Values or set of values
of different data types
such as int, float, etc.,
Way of organizing and
storing information so
that it is easy to use
Structure
Data + Structure
Way of organizing and
storing data on a computer
so that it can be used easily
and effectively
9/14/2021 4
Introduction to Data Structures
Definition:
Data Structure is a way of organizing,
storing and retrieving data and their relationships
with each other
9/14/2021 5
Classification of Data Structures
Data Structure
Primitive
int char float boolean
Non - Primitive
Linear
Array Linked List Stack Queue
Non - Linear
Tree Graph
9/14/2021 6
• It is a type for objects whose behavior is defined by set of
values and set of operations from user point of view
• Mentions only what operations are to be performed but
not how they will be implemented
• Thus, ADT is the specification of a data type within some
language, independent of implementation
ADT – Abstract Data Type
9/14/2021 7
Examples – List ADT, Stack ADT, Queue ADT, etc.,
Some operations on these ADT are:
List:
• insert(x)
• delete(x)
• find(x)
Stack:
• Push (x)
• Pop()
• isFull()
• isEmpty()
Queue:
• enqueue()
• dequeue()
• isFull()
• isEmpty()
ADT – Abstract Data Type
9/14/2021 8
• List ADT can be represented in two ways
– Array
– Linked List
List ADT
9/14/2021 9
• Collection of elements of same data type
• Elements in an array is stored in adjacent memory
location
Syntax:
• datatype arrayname [size]
• Eg: int a [50];
Operations:
• insertion()
• deletion()
• search()
Array ADT
9/14/2021 10
• Fixed size: Resizing is expensive
• Requires continuous memory for allocation
– Difficult when array size is larger
• Insertions and deletions are inefficient
– Elements are usually shifted
• Wastage of memory space unless the array is
full
Issues in Array
9/14/2021 11
Array Implementation of List
• Create
• Insert at beginning
• Insert at middle
• Insert at end
• Delete at beginning
• Delete at middle
• Delete at end
• Search
9/14/2021 12
• Linked list is a linear data structure
• Made up of chain of nodes
• Each node contains a data and a pointer to the next
node in the chain
• Last node of the list is pointed to NULL
Linked List ADT
9/14/2021 13
• Each node contains two fields:
– Data field: Contains data element to be stored
– Pointer field: Contains the address of next node
Representation of Node in Linked List
Declaration of a node:
struct node
{
int data;
struct node *next;
};
9/14/2021 14
Statement to create a node:
n= (struct node*) malloc(sizeof(struct node))
nnext =NULL;
Creating a Node
struct node
{
int data;
struct node *next;
} *n;
9/14/2021 15
• Singly linked list
• Doubly linked list
• Circular linked list
Types of Linked List
9/14/2021 16
• Type of linked list
• Each node contains only one pointer field that
contains the address of the next node in the list
Singly Linked List (SLL)
9/14/2021 17
Major operations are:
• Insertion
– At the beginning
– At the middle
– At the end
• Deletion
– At the beginning
– At the middle
– At the end
• Search
Operations on Singly Linked List
Global declaration of a
Node:
struct node
{
int data;
struct node *next;
} *head,*n, *t;
9/14/2021 18
Routine for Insertion at the Beginning
void ins_beg (int num) //Let num=10
{
n=(struct node *) malloc (size of(struct node));
nnext=NULL;
ndata=num;
if (head==NULL) //case 1
{
head=n;
tail=n;
}
else //case 2
{
nnext=head;
head=n;
}
}
10
9/14/2021 19
Routine for Insertion at the End
void ins_end (int num) //Let num=10
{
n=(struct node *) malloc (size of(struct node));
nnext=NULL;
ndata=num;
if (head==NULL) //case 1
{
head=n;
tail=n;
}
else //case 2
{
tailnext=n;
tail=n;
}
}
10
9/14/2021 20
Routine for Insertion at the Middle
void ins_end (int num, int mid_data) //Let num=10, mid_data=9
{
n=(struct node *) malloc (size of(struct node));
nnext=NULL;
ndata=num;
for(t=head; t!=NULL; t=tnext)
{
if(tdata==mid_data)
break;
}
nnext=tnext;
tnext=n;
}
10
83==9 ? NO 9==9 ? Yes
10
300
300
1500
300
9/14/2021 21
Routine for Deletion at the Beginning
void del_beg ()
{
t=head;
head=tnext;
free(t);
}
9/14/2021 22
Routine for Deletion at the End
void del_end ()
{
struct node *tp;
t=head;
while(tnext!=NULL)
{
tp=t;
t=tnext;
}
tail=tp;
tailnext=NULL;
free(t);
}
9/14/2021 23
Routine for Deletion at the Middle
void del_mid (int num) //Let num=70
{
struct node *tp;
t=head;
while(tdata!=num)
{
tp=t;
t=tnext;
}
tpnext=tnext;
free(t);
}
2800
83!=70? Yes 9!=70? Yes 70!=70? No
1500
9/14/2021 24
Routine for Search an Element
struct node* search (int num) //Let num=9
{
t=head;
while(t!=NULL && tdata!=num)
{
t=tnext;
}
return t;
}
t!=NULL? Yes
83!=9? Yes
t!=NULL? Yes
9!=9? No
Element Found!!
9/14/2021 25
Linked List – Advantages & Disadvantages
Advantages:
• No wastage of memory
– Memory allocated and deallocated as per
requirement
• Efficient insertion and deletion operations
Disadvantages:
• Occupies more space than array to store a data
– Due to the need of a pointer to the next node
• Does not support random or direct access
9/14/2021 26
Doubly Linked List (DLL)
• Type of linked list
• Each node contains two pointer fields that contains
the address of the next node and that of previous
node in the list
9/14/2021 27
• Each node contains three fields:
– Data: Contains data element to be stored
– next: Contains the address of next node
– prev: Contains address of previous node
Representation of Node in DLL
Declaration of a node:
struct node
{
int data;
struct node *next, *prev;
};
A Node in DLL
9/14/2021 28
Major operations are:
• Insertion
– At the beginning
– At the middle
– At the end
• Deletion
– At the beginning
– At the middle
– At the end
• Search
Operations on Doubly Linked List
Global declaration of a Node:
struct node
{
int data;
struct node *next, prev;
} *head, *tail, *n, *t;
9/14/2021 29
Routine for Insertion at the Beginning
void ins_beg_dll (int num) //Let num=70
{
n=(struct node *) malloc(size of(struct node));
nnext=NULL;
nprev=NULL;
ndata=num;
if(head==NULL) //case 1
{
head=n;
tail=n;
}
else //case 2
{
nnext=head;
headprev=n;
head=n;
}
}
70
500
1000
9/14/2021 30
void ins_end_dll (int num) //Let num=10
{
n=(struct node *) malloc (size of(struct node));
nnext=NULL;
nprev=NULL;
ndata=num;
if (head==NULL) //case 1
{
head=n;
tail=n;
}
else //case 2
{
tailnext=n;
nprev=tail;
tail=n;
}
}
Routine for Insertion at the End
9/14/2021 31
void ins_end_dll (int num, int mid_data) //Let num=10, mid_data=9
{
n=(struct node *) malloc (size of(struct node));
nnext=NULL;
nprev=NULL;
ndata=num;
for(t=head; t!=NULL; t=tnext)
{
if(tdata==mid_data)
break;
}
nnext=tnext;
nprev=t;
tnextprev=n;
tnext=n;
}
Routine for Insertion at the Middle
9/14/2021 32
void del_beg_dll ()
{
t=head;
head=headnext;
headprev=NULL;
free(t);
}
Routine for Deletion at the Beginning
t is freed
9/14/2021 33
Routine for Deletion at the End
void del_end_dll ()
{
t=head;
while(tnext!=NULL)
{
t=tnext;
}
tail=tailprev;//tail=tprev;
tailnext=NULL;
free(t);
}
9/14/2021 34
Routine for Deletion at the Middle
void del_mid_dll (int num) //Let num=9
{
t=head;
while(tdata!=num)
{
t=tnext;
}
tprevnext=tnext;
tnextprev=tprev;
free(t);
}
9/14/2021 35
Routine for Search an Element
struct node* search_dll (int num) //Let num=9
{
t=head;
while(t!=NULL && tdata!=num)
{
t=tnext;
}
return t;
}
Element Found!!
t is returned
9/14/2021 36
Advantages:
• Can traverse in both directions
• Operations in the middle of the list can be done
efficiently
– No need of extra pointer (tp) to track the previous
node
• Reversing the list is easy
Disadvantage:
• Space required to store a data is even more higher
than SLL
– Due to the use of two pointers
DLL – Advantages & Disadvantages
9/14/2021 37
• Type of linked list
• The last node will be pointing to the first node
• It can be either singly or doubly linked
Circular Linked List (CLL)
Global declaration of a node:
struct node
{
int data;
struct node *next, *prev;
}*head,*n,*t;
100
9/14/2021 38
Routine for Insertion at the Beginning
void ins_beg_cll (int num) //Let num=10
{
n=(struct node *) malloc (size of(struct node));
nnext=NULL;
ndata=num;
if (head==NULL) //case 1
{ head=n;
nnext=head;
}
else //case 2
{ t=head;
while(tnext!=head)
t=tnext;
tnext=n;
nnext=head;
head=n;
}
}
10
10 100
9/14/2021 39
Routine for Insertion at the End
void ins_end_cll (int num) //Let num=10
{
n=(struct node *) malloc (size of(struct node));
nnext=NULL;
ndata=num;
t=head;
while(tnext!=head)
t=tnext;
tnext=n;
nnext=head;
}
10
1000
9/14/2021 40
Routine for Insertion at the Middle
void ins_end_cll (int num, int mid_data) //Let num=10, mid_data=9
{
n=(struct node *) malloc (size of(struct node));
nnext=NULL;
ndata=num;
for(t=head; tnext!=head; t=tnext)
{
if(tdata==mid_data)
break;
}
nnext=tnext;
tnext=n;
}
10
1000
9/14/2021 41
Routine for Deletion at the Beginning
void del_beg_cll ()
{
struct node *t1;
t=head;
t1=head;
while(tnext!=head)
t=tnext;
tnext=headnext;
head=tnext;
free(t1);
} 100
t1 is freed!!
9/14/2021 42
Routine for Deletion at the End
void del_end_cll ()
{
struct node *tp;
t=head;
while(tnext!=head)
{
tp=t;
t=tnext;
}
tpnext=head;
free(t);
}
100
t is freed !!
9/14/2021 43
Routine for Deletion at the Middle
void del_mid_cll (int num) //Let num=9
{
struct node *tp;
t=head;
while(tdata!=num)
{
tp=t;
t=tnext;
}
tpnext=tnext;
free(t);
} t is freed!!
9/14/2021 44
Routine for Search
struct node* search_cll (int num) //Let num=9
{
t=head;
while(tnext!=head && tdata!=num)
{
t=tnext;
}
return t;
}
9/14/2021 45
Advantage:
• Comparing to SLL, moving to any node from a node is
possible
Disadvantages:
• Reversing a list is complex compared to linear linked
list
• If proper care is not taken in managing the pointers,
it leads to infinite loop
• Moving to previous node is difficult, as it is needed
to complete an entire circle
CLL – Advantages & Disadvantages
9/14/2021 46
Applications of List
Lists can be used to
• Sort elements
• Implement stack and queue
• Represent graph (adjacent list representation)
• Implement hash table
• Implement multiprocessing of applications
• Manipulate polynomial equations
9/14/2021 47
Polynomial ADT
struct node
{
int coeff;
int pow;
struct node *next;
}*n;
Examples of polynomial equations:
– 9x5 + 7x3 – 4x2 + 8
7x4 – 17x3 – 8x2
To store the data of polynomial equations in the linked
list, each node contains two data fields, namely
coefficient and power and one next pointer field
9/14/2021 48
Creating a Polynomial List
struct node* createpoly (int c, int p, struct node *t)
{ struct node *head, *tail;
head=t;
n=(struct node*) malloc(size of(struct node));
ncoeff=c;
npow=p;
nnext=NULL;
if (head==NULL)
{ head=n;
tail=n;
}
else
{ tailnext=n;
tail=n;
}
return head;
}
9/14/2021 49
Polynomial Addition
Void addpoly (struct node *poly1, struct node *poly2, struct node *poly)
{
while(ploy1!=NULL && poly2!=NULL)
{
if(poly1pow>poly2pow)
{
polypow=ploy1pow;
polycoeff=poly1coeff;
ploy1=poly1next;
}
else if(poly1pow < poly2pow)
{
polypow=ploy2pow;
polycoeff=poly2coeff;
ploy2=poly2next;
}
9/14/2021 50
else
{ polypow=ploy1pow;
polycoeff=poly1coeff + poly2coeff;
ploy1=poly1next;
ploy2=poly2next;
}
}
while(poly1 != NULL || poly2 != NULL)
{ if (poly1 != NULL)
{ polypow=ploy1pow;
polycoeff=poly1coeff;
ploy1=poly1next;
}
if (poly2 != NULL)
{ polypow=ploy2pow;
polycoeff=poly2coeff;
ploy2=poly2next;
}
}
}
Polynomial Addition Contd.,
9/14/2021 51
Queries ??
9/14/2021 52
Thank You!!!
9/14/2021 53

More Related Content

Similar to Data Structures - Unit 1 linked list

Data structure week y 4
Data structure week y 4Data structure week y 4
Data structure week y 4karmuhtam
 
6 attributed grammars
6  attributed grammars6  attributed grammars
6 attributed grammarsSaeed Parsa
 
Module 3 Dara structure notes
Module 3 Dara structure notesModule 3 Dara structure notes
Module 3 Dara structure notesDreamers6
 
What is Linked List in C.docx
What is Linked List in C.docxWhat is Linked List in C.docx
What is Linked List in C.docxnona800027
 
Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.pptGokiladeepa
 
Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.pptsaivasu4
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationMoniruzzaman _
 
DS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxDS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxVeerannaKotagi1
 
Please write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdfPlease write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdfamarndsons
 
Basic Deep Learning.pptx
Basic Deep Learning.pptxBasic Deep Learning.pptx
Basic Deep Learning.pptxmabog44
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfkostikjaylonshaewe47
 
17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversalIntro C# Book
 

Similar to Data Structures - Unit 1 linked list (20)

Data structure week y 4
Data structure week y 4Data structure week y 4
Data structure week y 4
 
6 attributed grammars
6  attributed grammars6  attributed grammars
6 attributed grammars
 
Module 3 Dara structure notes
Module 3 Dara structure notesModule 3 Dara structure notes
Module 3 Dara structure notes
 
What is Linked List in C.docx
What is Linked List in C.docxWhat is Linked List in C.docx
What is Linked List in C.docx
 
Linked List.ppt
Linked List.pptLinked List.ppt
Linked List.ppt
 
Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.ppt
 
Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.ppt
 
Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.ppt
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
ELAVARASAN.pdf
ELAVARASAN.pdfELAVARASAN.pdf
ELAVARASAN.pdf
 
Chtp412
Chtp412Chtp412
Chtp412
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
DS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxDS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docx
 
Please write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdfPlease write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdf
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
Basic Deep Learning.pptx
Basic Deep Learning.pptxBasic Deep Learning.pptx
Basic Deep Learning.pptx
 
Op ps
Op psOp ps
Op ps
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
 
NMEC RD_UNIT 1.ppt
NMEC RD_UNIT 1.pptNMEC RD_UNIT 1.ppt
NMEC RD_UNIT 1.ppt
 
17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal
 

Recently uploaded

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 

Recently uploaded (20)

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

Data Structures - Unit 1 linked list

  • 2. Need for Data Structure? 1, AHAMED SAHIB M L, DS, CS, 46, 42, 0, 57, 0 2, ANANTH N, H, CS, 64, 37, 0, 80, 03, ASHWATH NIVAS M, DS, CS, 52, 52, 0, 69, 04, DHINESH GV, DS, Bio, 28, 25, 0, 36, 05, DIWAKAR N, H, CS, OD, 57, 0, 74, 0 6, GIRIDHARAN S M, DS, CS, 24, 33, 0, 25, 07, HAJA SABEER A, DS, CS, 44, 37, 0, 43, 08, KARTHIK BALAJI B, H, Bio, 18, 16, 0, 50, 09, KAYAAN ROHITH A, DS, Bio, 34, 18, 0, 35, 010, KESHAV J, DS, Bio, 50, 50, 0, 80, 011, LOKESH KUMAR S, DS, Bio, 50, 16, 0, 38, 012, MARIYA BANATIC J, DS, CS, OD, 55, 0, OD, 013, PON SANKAR G, DS, CS, 38, A, 0, 57, 014, PRABHU D, H, CS, 62, 52, 0, OD, 015, PREM S J, DS, Bio, A, 19, 0, 34, 9/14/2021 2
  • 3. Organized data SL. NO. NAME OF THE STUDENT UT1 CT1 UT2 CT2 MODEL 1 AHAMED SAHIB M L DS CS 46 42 0 57 0 2 ANANTH N H CS 64 37 0 80 0 3 ASHWATH NIVAS M DS CS 52 52 0 69 0 4 DHINESH GV DS Bio 28 25 0 36 0 5 DIWAKAR N H CS OD 57 0 74 0 6 GIRIDHARAN S M DS CS 24 33 0 25 0 7 HAJA SABEER A DS CS 44 37 0 43 0 8 KARTHIK BALAJI B H Bio 18 16 0 50 0 9 KAYAAN ROHITH A DS Bio 34 18 0 35 0 10 KESHAV J DS Bio 50 50 0 80 0 11 LOKESH KUMAR S DS Bio 50 16 0 38 0 12 MARIYA BANATIC J DS CS OD 55 0 OD 0 13 PON SANKAR G DS CS 38 A 0 57 0 14 PRABHU D H CS 62 52 0 OD 0 15 PREM S J DS Bio A 19 0 34 0 9/14/2021 3
  • 4. Introduction to Data Structures Data Values or set of values of different data types such as int, float, etc., Way of organizing and storing information so that it is easy to use Structure Data + Structure Way of organizing and storing data on a computer so that it can be used easily and effectively 9/14/2021 4
  • 5. Introduction to Data Structures Definition: Data Structure is a way of organizing, storing and retrieving data and their relationships with each other 9/14/2021 5
  • 6. Classification of Data Structures Data Structure Primitive int char float boolean Non - Primitive Linear Array Linked List Stack Queue Non - Linear Tree Graph 9/14/2021 6
  • 7. • It is a type for objects whose behavior is defined by set of values and set of operations from user point of view • Mentions only what operations are to be performed but not how they will be implemented • Thus, ADT is the specification of a data type within some language, independent of implementation ADT – Abstract Data Type 9/14/2021 7
  • 8. Examples – List ADT, Stack ADT, Queue ADT, etc., Some operations on these ADT are: List: • insert(x) • delete(x) • find(x) Stack: • Push (x) • Pop() • isFull() • isEmpty() Queue: • enqueue() • dequeue() • isFull() • isEmpty() ADT – Abstract Data Type 9/14/2021 8
  • 9. • List ADT can be represented in two ways – Array – Linked List List ADT 9/14/2021 9
  • 10. • Collection of elements of same data type • Elements in an array is stored in adjacent memory location Syntax: • datatype arrayname [size] • Eg: int a [50]; Operations: • insertion() • deletion() • search() Array ADT 9/14/2021 10
  • 11. • Fixed size: Resizing is expensive • Requires continuous memory for allocation – Difficult when array size is larger • Insertions and deletions are inefficient – Elements are usually shifted • Wastage of memory space unless the array is full Issues in Array 9/14/2021 11
  • 12. Array Implementation of List • Create • Insert at beginning • Insert at middle • Insert at end • Delete at beginning • Delete at middle • Delete at end • Search 9/14/2021 12
  • 13. • Linked list is a linear data structure • Made up of chain of nodes • Each node contains a data and a pointer to the next node in the chain • Last node of the list is pointed to NULL Linked List ADT 9/14/2021 13
  • 14. • Each node contains two fields: – Data field: Contains data element to be stored – Pointer field: Contains the address of next node Representation of Node in Linked List Declaration of a node: struct node { int data; struct node *next; }; 9/14/2021 14
  • 15. Statement to create a node: n= (struct node*) malloc(sizeof(struct node)) nnext =NULL; Creating a Node struct node { int data; struct node *next; } *n; 9/14/2021 15
  • 16. • Singly linked list • Doubly linked list • Circular linked list Types of Linked List 9/14/2021 16
  • 17. • Type of linked list • Each node contains only one pointer field that contains the address of the next node in the list Singly Linked List (SLL) 9/14/2021 17
  • 18. Major operations are: • Insertion – At the beginning – At the middle – At the end • Deletion – At the beginning – At the middle – At the end • Search Operations on Singly Linked List Global declaration of a Node: struct node { int data; struct node *next; } *head,*n, *t; 9/14/2021 18
  • 19. Routine for Insertion at the Beginning void ins_beg (int num) //Let num=10 { n=(struct node *) malloc (size of(struct node)); nnext=NULL; ndata=num; if (head==NULL) //case 1 { head=n; tail=n; } else //case 2 { nnext=head; head=n; } } 10 9/14/2021 19
  • 20. Routine for Insertion at the End void ins_end (int num) //Let num=10 { n=(struct node *) malloc (size of(struct node)); nnext=NULL; ndata=num; if (head==NULL) //case 1 { head=n; tail=n; } else //case 2 { tailnext=n; tail=n; } } 10 9/14/2021 20
  • 21. Routine for Insertion at the Middle void ins_end (int num, int mid_data) //Let num=10, mid_data=9 { n=(struct node *) malloc (size of(struct node)); nnext=NULL; ndata=num; for(t=head; t!=NULL; t=tnext) { if(tdata==mid_data) break; } nnext=tnext; tnext=n; } 10 83==9 ? NO 9==9 ? Yes 10 300 300 1500 300 9/14/2021 21
  • 22. Routine for Deletion at the Beginning void del_beg () { t=head; head=tnext; free(t); } 9/14/2021 22
  • 23. Routine for Deletion at the End void del_end () { struct node *tp; t=head; while(tnext!=NULL) { tp=t; t=tnext; } tail=tp; tailnext=NULL; free(t); } 9/14/2021 23
  • 24. Routine for Deletion at the Middle void del_mid (int num) //Let num=70 { struct node *tp; t=head; while(tdata!=num) { tp=t; t=tnext; } tpnext=tnext; free(t); } 2800 83!=70? Yes 9!=70? Yes 70!=70? No 1500 9/14/2021 24
  • 25. Routine for Search an Element struct node* search (int num) //Let num=9 { t=head; while(t!=NULL && tdata!=num) { t=tnext; } return t; } t!=NULL? Yes 83!=9? Yes t!=NULL? Yes 9!=9? No Element Found!! 9/14/2021 25
  • 26. Linked List – Advantages & Disadvantages Advantages: • No wastage of memory – Memory allocated and deallocated as per requirement • Efficient insertion and deletion operations Disadvantages: • Occupies more space than array to store a data – Due to the need of a pointer to the next node • Does not support random or direct access 9/14/2021 26
  • 27. Doubly Linked List (DLL) • Type of linked list • Each node contains two pointer fields that contains the address of the next node and that of previous node in the list 9/14/2021 27
  • 28. • Each node contains three fields: – Data: Contains data element to be stored – next: Contains the address of next node – prev: Contains address of previous node Representation of Node in DLL Declaration of a node: struct node { int data; struct node *next, *prev; }; A Node in DLL 9/14/2021 28
  • 29. Major operations are: • Insertion – At the beginning – At the middle – At the end • Deletion – At the beginning – At the middle – At the end • Search Operations on Doubly Linked List Global declaration of a Node: struct node { int data; struct node *next, prev; } *head, *tail, *n, *t; 9/14/2021 29
  • 30. Routine for Insertion at the Beginning void ins_beg_dll (int num) //Let num=70 { n=(struct node *) malloc(size of(struct node)); nnext=NULL; nprev=NULL; ndata=num; if(head==NULL) //case 1 { head=n; tail=n; } else //case 2 { nnext=head; headprev=n; head=n; } } 70 500 1000 9/14/2021 30
  • 31. void ins_end_dll (int num) //Let num=10 { n=(struct node *) malloc (size of(struct node)); nnext=NULL; nprev=NULL; ndata=num; if (head==NULL) //case 1 { head=n; tail=n; } else //case 2 { tailnext=n; nprev=tail; tail=n; } } Routine for Insertion at the End 9/14/2021 31
  • 32. void ins_end_dll (int num, int mid_data) //Let num=10, mid_data=9 { n=(struct node *) malloc (size of(struct node)); nnext=NULL; nprev=NULL; ndata=num; for(t=head; t!=NULL; t=tnext) { if(tdata==mid_data) break; } nnext=tnext; nprev=t; tnextprev=n; tnext=n; } Routine for Insertion at the Middle 9/14/2021 32
  • 33. void del_beg_dll () { t=head; head=headnext; headprev=NULL; free(t); } Routine for Deletion at the Beginning t is freed 9/14/2021 33
  • 34. Routine for Deletion at the End void del_end_dll () { t=head; while(tnext!=NULL) { t=tnext; } tail=tailprev;//tail=tprev; tailnext=NULL; free(t); } 9/14/2021 34
  • 35. Routine for Deletion at the Middle void del_mid_dll (int num) //Let num=9 { t=head; while(tdata!=num) { t=tnext; } tprevnext=tnext; tnextprev=tprev; free(t); } 9/14/2021 35
  • 36. Routine for Search an Element struct node* search_dll (int num) //Let num=9 { t=head; while(t!=NULL && tdata!=num) { t=tnext; } return t; } Element Found!! t is returned 9/14/2021 36
  • 37. Advantages: • Can traverse in both directions • Operations in the middle of the list can be done efficiently – No need of extra pointer (tp) to track the previous node • Reversing the list is easy Disadvantage: • Space required to store a data is even more higher than SLL – Due to the use of two pointers DLL – Advantages & Disadvantages 9/14/2021 37
  • 38. • Type of linked list • The last node will be pointing to the first node • It can be either singly or doubly linked Circular Linked List (CLL) Global declaration of a node: struct node { int data; struct node *next, *prev; }*head,*n,*t; 100 9/14/2021 38
  • 39. Routine for Insertion at the Beginning void ins_beg_cll (int num) //Let num=10 { n=(struct node *) malloc (size of(struct node)); nnext=NULL; ndata=num; if (head==NULL) //case 1 { head=n; nnext=head; } else //case 2 { t=head; while(tnext!=head) t=tnext; tnext=n; nnext=head; head=n; } } 10 10 100 9/14/2021 39
  • 40. Routine for Insertion at the End void ins_end_cll (int num) //Let num=10 { n=(struct node *) malloc (size of(struct node)); nnext=NULL; ndata=num; t=head; while(tnext!=head) t=tnext; tnext=n; nnext=head; } 10 1000 9/14/2021 40
  • 41. Routine for Insertion at the Middle void ins_end_cll (int num, int mid_data) //Let num=10, mid_data=9 { n=(struct node *) malloc (size of(struct node)); nnext=NULL; ndata=num; for(t=head; tnext!=head; t=tnext) { if(tdata==mid_data) break; } nnext=tnext; tnext=n; } 10 1000 9/14/2021 41
  • 42. Routine for Deletion at the Beginning void del_beg_cll () { struct node *t1; t=head; t1=head; while(tnext!=head) t=tnext; tnext=headnext; head=tnext; free(t1); } 100 t1 is freed!! 9/14/2021 42
  • 43. Routine for Deletion at the End void del_end_cll () { struct node *tp; t=head; while(tnext!=head) { tp=t; t=tnext; } tpnext=head; free(t); } 100 t is freed !! 9/14/2021 43
  • 44. Routine for Deletion at the Middle void del_mid_cll (int num) //Let num=9 { struct node *tp; t=head; while(tdata!=num) { tp=t; t=tnext; } tpnext=tnext; free(t); } t is freed!! 9/14/2021 44
  • 45. Routine for Search struct node* search_cll (int num) //Let num=9 { t=head; while(tnext!=head && tdata!=num) { t=tnext; } return t; } 9/14/2021 45
  • 46. Advantage: • Comparing to SLL, moving to any node from a node is possible Disadvantages: • Reversing a list is complex compared to linear linked list • If proper care is not taken in managing the pointers, it leads to infinite loop • Moving to previous node is difficult, as it is needed to complete an entire circle CLL – Advantages & Disadvantages 9/14/2021 46
  • 47. Applications of List Lists can be used to • Sort elements • Implement stack and queue • Represent graph (adjacent list representation) • Implement hash table • Implement multiprocessing of applications • Manipulate polynomial equations 9/14/2021 47
  • 48. Polynomial ADT struct node { int coeff; int pow; struct node *next; }*n; Examples of polynomial equations: – 9x5 + 7x3 – 4x2 + 8 7x4 – 17x3 – 8x2 To store the data of polynomial equations in the linked list, each node contains two data fields, namely coefficient and power and one next pointer field 9/14/2021 48
  • 49. Creating a Polynomial List struct node* createpoly (int c, int p, struct node *t) { struct node *head, *tail; head=t; n=(struct node*) malloc(size of(struct node)); ncoeff=c; npow=p; nnext=NULL; if (head==NULL) { head=n; tail=n; } else { tailnext=n; tail=n; } return head; } 9/14/2021 49
  • 50. Polynomial Addition Void addpoly (struct node *poly1, struct node *poly2, struct node *poly) { while(ploy1!=NULL && poly2!=NULL) { if(poly1pow>poly2pow) { polypow=ploy1pow; polycoeff=poly1coeff; ploy1=poly1next; } else if(poly1pow < poly2pow) { polypow=ploy2pow; polycoeff=poly2coeff; ploy2=poly2next; } 9/14/2021 50
  • 51. else { polypow=ploy1pow; polycoeff=poly1coeff + poly2coeff; ploy1=poly1next; ploy2=poly2next; } } while(poly1 != NULL || poly2 != NULL) { if (poly1 != NULL) { polypow=ploy1pow; polycoeff=poly1coeff; ploy1=poly1next; } if (poly2 != NULL) { polypow=ploy2pow; polycoeff=poly2coeff; ploy2=poly2next; } } } Polynomial Addition Contd., 9/14/2021 51

Editor's Notes

  1. If element not found, t=NULL, so NULL will be returned
  2. In CLL, ins at begin and ins at end are almost same with a difference that n will not be assigned as head in ins at end
  3. Multiprocessing of applications – all the applications will be stored in nodes of the list. OS has a fixed time for running each application