SlideShare a Scribd company logo
1 of 40
DATA STRUCTURES
Dr. P. Subathra
Professor
Dept. of Information Technology
KAMARAJ College of Engineering & Technology
Madurai
Tamil Nadu
India
subathrakishore@yahoo.com
CS8391 – DATA STRUCTURES
ONLINE CLASSES – CLASS NO. 2
13.08.2020
(10:00 AM – 11:00 AM
&
11:30 AM – 12:30 PM)
UNIT 1
SINGLY LINKED LIST
WHAT IS WRONG WITH ARRAY…??
• STATIC MEMORY ALLOCATION
• BUT… IS IT NOT DYNAMIC…??
• malloc ( )
WHAT IS WRONG WITH ARRAY…??
• malloc ( ) …………NO..!!!
• HOW ABOUT realloc( ) …???
• realloc ( )…. NO….??!!!
WHAT IS WRONG WITH ARRAY…??
HEAP
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
LOOK CLOSER……
HEAP
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
LOOK CLOSER……
int * a = (int *) malloc (4);
HEAP
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
int * a = (int *) malloc (4);
HEAP
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
int * a = (int *) realloc (8);
HEAP
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
int * a = (int *) realloc (4);
HEAP
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
int * a = (int *) realloc (4);
HEAP
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
int * a = (int *) realloc (4);
HEAP
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
LINKED LIST….!!!
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
Linked List
• NODE
– Data Field
– Link / Pointer Field
• HEADER
• NULL Pointer
TYPES OF LINKED LISTS
1. SINGLY LINKED LIST
2. CIRCULAR SINGLY LINKED LIST
TYPES OF LINKED LISTS
1. DOUBLY LINKED LIST
2. CIRCULAR DOUBLY LINKED LIST
Singly Linked List
Singly Linked List
• Linked list or list is an ordered collection of
elements.
• Each element in the list is referred as a node.
• Each node contains two fields namely.
– Data field.
– Link field.
(or) Address field
Operations on a Singly Linked List
• Creating a new list
• Insertion
- at the beginning
- at the end
- after and element
- before an element
• Deletion
- at the beginning
- at the end
- a given element
- at a position
• Display
• Search Element
• Reverse List
• Sort List
• Find Duplicates
• ………………
CREATING A LIST
• Template for a node
struct node
{
int element ;
struct node * next ;
};
• Creating an Empty List
struct node * head = NULL;
Operations on a Singly Linked List
NULL
head
Data field Link field
2 B 4 B
4 B
head
CREATING A LIST
• Creating a New Node and Attaching it to the
List
struct node * temp = (node *) malloc (sizeof (struct node));
head = temp;
tempnext = NULL
Operations on a Singly Linked List
NULL
head
Data field Link field
temp1005
CREATING A LIST
• Creating a New Node and Attaching it to the
List
struct node * temp = (node *) malloc (sizeof (struct node));
tempnext = NULL
tempdata =222;
head = temp;
Operations on a Singly Linked List
1005
head
Data field Link field
NULL
temp
1005
222
INSERTION – FIRST
• Algorithm
STEP 1: Dynamically create a new node.
STEP 2: Read the element to the
element field of the new node.
STEP 3: Copy the address value
available in the Head pointer (also
called as Header) into the next field of
the new node.
STEP 4: Make the Header to pointer to
this new node.
• Case 1
– Insertion into an Empty
List
• Case 2
– Insertion into a Non
Empty List
Operations on a Singly Linked List
INSERTION – FIRST
• Algorithm
STEP 1: Dynamically create a new node.
STEP 2: Read the element to the
element field of the new node.
STEP 3: Copy the address value
available in the Head pointer (also
called as Header) into the next field of
the new node.
STEP 4: Make the Header to pointer to
this new node.
Case 1 – Empty List
Operations on a Singly Linked List
INSERTION – FIRST
• Algorithm
Case 2 : Non Empty List
STEP 1: Dynamically create a new node.
STEP 2: Read the element to the
element field of the new node.
STEP 3: Copy the address value
available in the Head pointer (also called
as Header) into the next field of the new
node.
STEP 4: Make the Header to pointer to
this new node.
Operations on a Singly Linked List
INSERTION – FIRST
Pseudo code
void InsertFirst (int x )
{
creat new node temp,
temp  element = x;
tempnext = header; // copy header value into T next
header = temp; // make header to point to new node T
}
Operations on a Singly Linked List
INSERTION – FIRST
Code Snippet in C
void InsertFirst (int x )
{
struct node * temp = (node*) malloc(sizeof(struct node*));
temp  data = x;
tempnext = head; // copy header value into T next
head = temp; // make header to point to new node T
}
Operations on a Singly Linked List
INSERTION – LAST
• Algorithm
STEP 1: Dynamically create a new node.
STEP 2: Read the element to the element
field of the new node and make the next field
to be NULL
STEP 3: If the list is empty insert this new
node as we did for insert first module
ie, make the header to point to this node.
STEP 4: If a non empty list already exist,
traverse the list till the last node is reached.
STEP 5: Make the last node to point to this
new node.
• Case 1
– Insertion into an Empty List
• Case 2
– Insertion into a Non Empty
List
Operations on a Singly Linked List
INSERTION – LAST
Algorithm : Case 1 - Empty
List
STEP 1: Dynamically create a new
node.
STEP 2: Read the element to the
element field of the new node and
make the next field to be NULL
STEP 3: If the list is empty insert this
new node as we did for insert first
module
ie, make the header to point to this
node.
Operations on a Singly Linked List
INSERTION – LAST
Algorithm :
Case 1 – Non Empty List
STEP 1: Dynamically create a new node.
STEP 2: Read the element to the element
field of the new node and make the next field
to be NULL
STEP 3: If the list is empty insert this new
node as we did for insert first module
ie, make the header to point to this node.
STEP 4: If a non empty list already exist,
traverse the list till the last node is reached.
STEP 5: Make the last node to point to this
new node.
Operations on a Singly Linked List
Insert Last – Non empty list
ALGORITHM Insert Last
void InsertLast (int x )
{
create a new node temp
temp  data = x;
tempnext = NULL;
if (head== NULL) // List is empty
{
head = temp;
}
else
{ / * List Traversal * /
node * current = head;
while (current next! = NULL) // Traverse to the last node.
current = current next;
current  next = temp ; // make last node to point to temp
}
}
Operations on a Singly Linked List

More Related Content

What's hot (20)

stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
Stack project
Stack projectStack project
Stack project
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Stacks in Data Structure
Stacks in Data StructureStacks in Data Structure
Stacks in Data Structure
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Stack & queue
Stack & queueStack & queue
Stack & queue
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applications
 
Stacks
StacksStacks
Stacks
 

Similar to 1. 2 Singly Linked List

Data Structure Presentation
Data Structure PresentationData Structure Presentation
Data Structure Presentationpinturam2
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)Durga Devi
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked ListsAfaq Mansoor Khan
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queueRai University
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queueRai University
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queuessuser7319f8
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Getachew Ganfur
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocationkiran Patel
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in pythonLifna C.S
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.pptSeethaDinesh
 

Similar to 1. 2 Singly Linked List (20)

Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Data Structure Presentation
Data Structure PresentationData Structure Presentation
Data Structure Presentation
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
 
sorting_part1.ppt
sorting_part1.pptsorting_part1.ppt
sorting_part1.ppt
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
5.Linked list
5.Linked list 5.Linked list
5.Linked list
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
module 3-.pptx
module 3-.pptxmodule 3-.pptx
module 3-.pptx
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
 
DSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdfDSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdf
 

More from P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai

More from P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai (20)

TestFile
TestFileTestFile
TestFile
 
3.1 Trees ( Introduction, Binary Trees & Binary Search Trees)
3.1 Trees ( Introduction, Binary Trees & Binary Search Trees)3.1 Trees ( Introduction, Binary Trees & Binary Search Trees)
3.1 Trees ( Introduction, Binary Trees & Binary Search Trees)
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
 
1. C Basics for Data Structures Bridge Course
1. C Basics for Data Structures   Bridge Course1. C Basics for Data Structures   Bridge Course
1. C Basics for Data Structures Bridge Course
 
Approximation Algorithms TSP
Approximation Algorithms   TSPApproximation Algorithms   TSP
Approximation Algorithms TSP
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
 
The stable marriage problem iterative improvement method
The stable marriage problem iterative improvement methodThe stable marriage problem iterative improvement method
The stable marriage problem iterative improvement method
 
Maximum matching in bipartite graphs iterative improvement method
Maximum matching in bipartite graphs   iterative improvement methodMaximum matching in bipartite graphs   iterative improvement method
Maximum matching in bipartite graphs iterative improvement method
 
Knapsack dynamic programming formula top down (1)
Knapsack dynamic programming formula top down (1)Knapsack dynamic programming formula top down (1)
Knapsack dynamic programming formula top down (1)
 
Knapsack dynamic programming formula bottom up
Knapsack dynamic programming formula bottom upKnapsack dynamic programming formula bottom up
Knapsack dynamic programming formula bottom up
 
Huffman tree coding greedy approach
Huffman tree coding  greedy approachHuffman tree coding  greedy approach
Huffman tree coding greedy approach
 
Simplex method
Simplex methodSimplex method
Simplex method
 
Simplex method
Simplex methodSimplex method
Simplex method
 
Multiplication of integers & strassens matrix multiplication subi notes
Multiplication of integers & strassens matrix multiplication   subi notesMultiplication of integers & strassens matrix multiplication   subi notes
Multiplication of integers & strassens matrix multiplication subi notes
 
Multiplication of large integers problem subi notes
Multiplication of large integers  problem  subi notesMultiplication of large integers  problem  subi notes
Multiplication of large integers problem subi notes
 
Huffman tree coding
Huffman tree codingHuffman tree coding
Huffman tree coding
 
Final maximum matching in bipartite graphs
Final maximum matching in bipartite graphsFinal maximum matching in bipartite graphs
Final maximum matching in bipartite graphs
 
The Stable Marriage Problem
The Stable Marriage ProblemThe Stable Marriage Problem
The Stable Marriage Problem
 
5. cs8451 daa anna univ question bank unit 5
5. cs8451 daa anna univ question bank unit 55. cs8451 daa anna univ question bank unit 5
5. cs8451 daa anna univ question bank unit 5
 
4. cs8451 daa anna univ question bank unit 4
4. cs8451 daa anna univ question bank unit 44. cs8451 daa anna univ question bank unit 4
4. cs8451 daa anna univ question bank unit 4
 

Recently uploaded

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 

Recently uploaded (20)

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 

1. 2 Singly Linked List

  • 1. DATA STRUCTURES Dr. P. Subathra Professor Dept. of Information Technology KAMARAJ College of Engineering & Technology Madurai Tamil Nadu India subathrakishore@yahoo.com
  • 2. CS8391 – DATA STRUCTURES ONLINE CLASSES – CLASS NO. 2 13.08.2020 (10:00 AM – 11:00 AM & 11:30 AM – 12:30 PM)
  • 4. WHAT IS WRONG WITH ARRAY…??
  • 5. • STATIC MEMORY ALLOCATION • BUT… IS IT NOT DYNAMIC…?? • malloc ( ) WHAT IS WRONG WITH ARRAY…??
  • 6. • malloc ( ) …………NO..!!! • HOW ABOUT realloc( ) …??? • realloc ( )…. NO….??!!! WHAT IS WRONG WITH ARRAY…??
  • 7. HEAP 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 LOOK CLOSER……
  • 8. HEAP 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 LOOK CLOSER……
  • 9. int * a = (int *) malloc (4); HEAP 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  • 10. int * a = (int *) malloc (4); HEAP 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  • 11. int * a = (int *) realloc (8); HEAP 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  • 12. int * a = (int *) realloc (4); HEAP 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  • 13. int * a = (int *) realloc (4); HEAP 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  • 14. int * a = (int *) realloc (4); HEAP 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  • 16. 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  • 17. 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  • 18. 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  • 19. 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  • 20. 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  • 21. 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  • 22. Linked List • NODE – Data Field – Link / Pointer Field • HEADER • NULL Pointer
  • 23. TYPES OF LINKED LISTS 1. SINGLY LINKED LIST 2. CIRCULAR SINGLY LINKED LIST
  • 24. TYPES OF LINKED LISTS 1. DOUBLY LINKED LIST 2. CIRCULAR DOUBLY LINKED LIST
  • 26. Singly Linked List • Linked list or list is an ordered collection of elements. • Each element in the list is referred as a node. • Each node contains two fields namely. – Data field. – Link field. (or) Address field
  • 27. Operations on a Singly Linked List • Creating a new list • Insertion - at the beginning - at the end - after and element - before an element • Deletion - at the beginning - at the end - a given element - at a position • Display • Search Element • Reverse List • Sort List • Find Duplicates • ………………
  • 28. CREATING A LIST • Template for a node struct node { int element ; struct node * next ; }; • Creating an Empty List struct node * head = NULL; Operations on a Singly Linked List NULL head Data field Link field 2 B 4 B 4 B head
  • 29. CREATING A LIST • Creating a New Node and Attaching it to the List struct node * temp = (node *) malloc (sizeof (struct node)); head = temp; tempnext = NULL Operations on a Singly Linked List NULL head Data field Link field temp1005
  • 30. CREATING A LIST • Creating a New Node and Attaching it to the List struct node * temp = (node *) malloc (sizeof (struct node)); tempnext = NULL tempdata =222; head = temp; Operations on a Singly Linked List 1005 head Data field Link field NULL temp 1005 222
  • 31. INSERTION – FIRST • Algorithm STEP 1: Dynamically create a new node. STEP 2: Read the element to the element field of the new node. STEP 3: Copy the address value available in the Head pointer (also called as Header) into the next field of the new node. STEP 4: Make the Header to pointer to this new node. • Case 1 – Insertion into an Empty List • Case 2 – Insertion into a Non Empty List Operations on a Singly Linked List
  • 32. INSERTION – FIRST • Algorithm STEP 1: Dynamically create a new node. STEP 2: Read the element to the element field of the new node. STEP 3: Copy the address value available in the Head pointer (also called as Header) into the next field of the new node. STEP 4: Make the Header to pointer to this new node. Case 1 – Empty List Operations on a Singly Linked List
  • 33. INSERTION – FIRST • Algorithm Case 2 : Non Empty List STEP 1: Dynamically create a new node. STEP 2: Read the element to the element field of the new node. STEP 3: Copy the address value available in the Head pointer (also called as Header) into the next field of the new node. STEP 4: Make the Header to pointer to this new node. Operations on a Singly Linked List
  • 34. INSERTION – FIRST Pseudo code void InsertFirst (int x ) { creat new node temp, temp  element = x; tempnext = header; // copy header value into T next header = temp; // make header to point to new node T } Operations on a Singly Linked List
  • 35. INSERTION – FIRST Code Snippet in C void InsertFirst (int x ) { struct node * temp = (node*) malloc(sizeof(struct node*)); temp  data = x; tempnext = head; // copy header value into T next head = temp; // make header to point to new node T } Operations on a Singly Linked List
  • 36. INSERTION – LAST • Algorithm STEP 1: Dynamically create a new node. STEP 2: Read the element to the element field of the new node and make the next field to be NULL STEP 3: If the list is empty insert this new node as we did for insert first module ie, make the header to point to this node. STEP 4: If a non empty list already exist, traverse the list till the last node is reached. STEP 5: Make the last node to point to this new node. • Case 1 – Insertion into an Empty List • Case 2 – Insertion into a Non Empty List Operations on a Singly Linked List
  • 37. INSERTION – LAST Algorithm : Case 1 - Empty List STEP 1: Dynamically create a new node. STEP 2: Read the element to the element field of the new node and make the next field to be NULL STEP 3: If the list is empty insert this new node as we did for insert first module ie, make the header to point to this node. Operations on a Singly Linked List
  • 38. INSERTION – LAST Algorithm : Case 1 – Non Empty List STEP 1: Dynamically create a new node. STEP 2: Read the element to the element field of the new node and make the next field to be NULL STEP 3: If the list is empty insert this new node as we did for insert first module ie, make the header to point to this node. STEP 4: If a non empty list already exist, traverse the list till the last node is reached. STEP 5: Make the last node to point to this new node. Operations on a Singly Linked List
  • 39. Insert Last – Non empty list
  • 40. ALGORITHM Insert Last void InsertLast (int x ) { create a new node temp temp  data = x; tempnext = NULL; if (head== NULL) // List is empty { head = temp; } else { / * List Traversal * / node * current = head; while (current next! = NULL) // Traverse to the last node. current = current next; current  next = temp ; // make last node to point to temp } } Operations on a Singly Linked List