SlideShare a Scribd company logo
Singly Linked List
A Linear Data Structure
SONI GUPTA
ASSISTANT PROFESSOR (NPIU-TEQIP III )
HBTU KANPUR, UP
TEQIP
-III
Insertion Operation
 A linked list consists of nodes where each node contains a data field and a
reference(link/pointer) to the next node in the list.
 A node in the singly linked list consist of two parts: data part and link part.
 Data part of the node stores actual information
 Next part of the node stores the address of its immediate successor.
 Singly linked list can be traversed only in one direction
TEQIP
-III
100 600200 400
200 600 400
TEQIP
-III
head
Node creation
struct node
{
int data;
struct node *next;
};
struct node *head, *ptr;
ptr = (struct node *)malloc(sizeof(struct node *));
TEQIP
-III
Insert at first position
 Inserting any new node at the front of the list.
 Insert_at_First
1. Create a new empty Node
2. Initialize the data field and Next field
3. Set the address pointer of head and newnode
4. Return address of new node (Set the head of node to new node)
TEQIP
-III
 head
10 20
30 40
TEQIP
-III
head
10 20
30 40
x
TEQIP
-III
head
10 20
30 40
x
newNode
newNode→next=head
TEQIP
-III
10 20
30 40
x
head
Head=newNode
TEQIP
-III
Create a New Node
𝑛𝑜𝑑𝑒 𝑐𝑟𝑒𝑎𝑡𝑒𝑁𝑜𝑑𝑒(){
struct *𝑛𝑜𝑑𝑒 𝑛𝑒𝑤𝑁𝑜𝑑𝑒;
𝑛𝑒𝑤𝑁𝑜𝑑𝑒 = (𝑛𝑜𝑑𝑒)𝑚𝑎𝑙𝑙𝑜𝑐(𝑠𝑖𝑧𝑒𝑜𝑓(𝑠𝑡𝑟𝑢𝑐𝑡 𝐿𝑖𝑛𝑘𝑒𝑑𝐿𝑖𝑠𝑡));
𝑛𝑒𝑤𝑁𝑜𝑑𝑒→𝑛𝑒𝑥𝑡 = 𝑁𝑈𝐿𝐿;
𝑟𝑒𝑡𝑢𝑟𝑛 𝑛𝑒𝑤𝑁𝑜𝑑𝑒;
}
TEQIP
-III
node insert_at_Front(node head, int value){
node newNode;
newNode = createNode();
newNode →data = value;
if(head == NULL){
head = newNode; //when linked list is empty
}
else{
newNode→next = head;
head= newNode;
}
return head;
}
TEQIP
-III
Insert at last position
1. Create a new node
2. Initialize the data and next field
3. Check if the list is empty (New node will be the first and last node)
4. Initialize search for the last node
5. Set next field of last node to new node
6. Return head pointer
TEQIP
-III
 head
10 20
30 40
40
newNode
TEQIP
-III
 head
10 20
30 40
40
newNode
p
TEQIP
-III
 head
10 20
30 40
40
newNode
p
TEQIP
-III
//Insert node at the end of the linked list
node insert_at_End(node head, int value){
node newNode,p;
newNode = createNode();
newNode→data = value;
if(head == NULL){
head = newNode; //when linked list is empty
}
else{
p = head;
while(p->next != NULL){
p = p->next;
}
p->next = newNode;
}
return head;
}
TEQIP
-III
Insert at any position p
1. Create a new node
2. Initialize the data and next field
3. Check if the list is empty (New node will be the first and last node)
4. Initialize temporary pointer
5. Initialize search for the predecessor of new node
6. Set next field of predecessor and new node
7. Return head pointer
TEQIP
-III
 head
10 20
30 40
40
newNode
p
TEQIP
-III
 head
10 20
30 40
40
newNode
pp
1. newNode→next=p →next
TEQIP
-III
 head
10 20
30 40
40
newNode
pp
2. p → next=newNode
TEQIP
-III
void insert_at_pos(node head, int value)
{
int i,pos;
struct node *newNode, *temp;
newNode = (struct node *) malloc (sizeof(struct node));
if(head == NULL)
{
printf("n list is empty");
}
else
{
newNode→data = value;
printf("nEnter the location after which you want to insert the new node ");
scanf("n%d",&pos);
TEQIP
-III
temp=head;
for(i=0;i<pos;i++)
{
temp = temp →next;
if(temp == NULL)
{
printf("ncan't insertn");
return;
}
}
newNode →next = temp →next;
temp →next = newNode;
printf("nNode inserted at given location");
}
}
TEQIP
-III
Complexity
 Traverse/Access- O(n)
 Search- O(n)
 Insertion- O(1)
 Deletion- O(1)
** Insertion and deletion will take O(n) time if you need to first traverse the list and
then insert or delete.
TEQIP
-III

More Related Content

What's hot

Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
Akila Krishnamoorthy
 
Linked list
Linked listLinked list
Linked list
akshat360
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
Sumathi Kv
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
Khulna University of Engineering & Tecnology
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
maamir farooq
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked lists
student
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
Khuram Shahzad
 
CSE240 Doubly Linked Lists
CSE240 Doubly Linked ListsCSE240 Doubly Linked Lists
CSE240 Doubly Linked Lists
Garrett Gutierrez
 
Data Structure (Double Linked List)
Data Structure (Double Linked List)Data Structure (Double Linked List)
Data Structure (Double Linked List)
Adam Mukharil Bachtiar
 
Single linked list
Single linked listSingle linked list
Single linked list
jasbirsingh chauhan
 
Ch17
Ch17Ch17
Ch17
Abbott
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
kulachihansraj
 
Linked lists
Linked listsLinked lists
Linked list
Linked listLinked list
Linked list
VONI
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracing
Samsil Arefin
 
linked list
linked list linked list
linked list
Narendra Chauhan
 
Link List
Link ListLink List
Link List
umiekalsum
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
swajahatr
 
Linked list
Linked listLinked list
Linked list
eShikshak
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
Afaq Mansoor Khan
 

What's hot (20)

Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
Linked list
Linked listLinked list
Linked list
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked lists
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
CSE240 Doubly Linked Lists
CSE240 Doubly Linked ListsCSE240 Doubly Linked Lists
CSE240 Doubly Linked Lists
 
Data Structure (Double Linked List)
Data Structure (Double Linked List)Data Structure (Double Linked List)
Data Structure (Double Linked List)
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Ch17
Ch17Ch17
Ch17
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Linked lists
Linked listsLinked lists
Linked lists
 
Linked list
Linked listLinked list
Linked list
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracing
 
linked list
linked list linked list
linked list
 
Link List
Link ListLink List
Link List
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
 
Linked list
Linked listLinked list
Linked list
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 

Similar to Linked list part-2

Linked list
Linked listLinked list
Linked list
A. S. M. Shafi
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
Ain-ul-Moiz Khawaja
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
PoonamPatil120
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
JITTAYASHWANTHREDDY
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
Reazul Islam
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
EricvtJFraserr
 
Data structures linked list introduction.pptx
Data structures linked list introduction.pptxData structures linked list introduction.pptx
Data structures linked list introduction.pptx
Kalpana Mohan
 
data structures lists operation of lists
data structures lists operation of listsdata structures lists operation of lists
data structures lists operation of lists
muskans14
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
Amit Vats
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
KylaMaeGarcia1
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
arnold 7490
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
ssuser0be977
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
kasthurimukila
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
ISHAN194169
 
linkedlistwith animations.ppt
linkedlistwith animations.pptlinkedlistwith animations.ppt
linkedlistwith animations.ppt
MuhammadShafi89
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.ppt
AlliVinay1
 
Singly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptxSingly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptx
ssusera965f6
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
Masud Parvaze
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
Himadri Sen Gupta
 

Similar to Linked list part-2 (20)

Linked list
Linked listLinked list
Linked list
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
Data structures linked list introduction.pptx
Data structures linked list introduction.pptxData structures linked list introduction.pptx
Data structures linked list introduction.pptx
 
data structures lists operation of lists
data structures lists operation of listsdata structures lists operation of lists
data structures lists operation of lists
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
 
linkedlistwith animations.ppt
linkedlistwith animations.pptlinkedlistwith animations.ppt
linkedlistwith animations.ppt
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.ppt
 
Singly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptxSingly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptx
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 

Recently uploaded

ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
KrishnaveniKrishnara1
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
mamamaam477
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
Madan Karki
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 

Recently uploaded (20)

ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 

Linked list part-2

  • 1. Singly Linked List A Linear Data Structure SONI GUPTA ASSISTANT PROFESSOR (NPIU-TEQIP III ) HBTU KANPUR, UP TEQIP -III
  • 3.  A linked list consists of nodes where each node contains a data field and a reference(link/pointer) to the next node in the list.  A node in the singly linked list consist of two parts: data part and link part.  Data part of the node stores actual information  Next part of the node stores the address of its immediate successor.  Singly linked list can be traversed only in one direction TEQIP -III
  • 4. 100 600200 400 200 600 400 TEQIP -III head
  • 5. Node creation struct node { int data; struct node *next; }; struct node *head, *ptr; ptr = (struct node *)malloc(sizeof(struct node *)); TEQIP -III
  • 6. Insert at first position  Inserting any new node at the front of the list.  Insert_at_First 1. Create a new empty Node 2. Initialize the data field and Next field 3. Set the address pointer of head and newnode 4. Return address of new node (Set the head of node to new node) TEQIP -III
  • 7.  head 10 20 30 40 TEQIP -III
  • 11. Create a New Node 𝑛𝑜𝑑𝑒 𝑐𝑟𝑒𝑎𝑡𝑒𝑁𝑜𝑑𝑒(){ struct *𝑛𝑜𝑑𝑒 𝑛𝑒𝑤𝑁𝑜𝑑𝑒; 𝑛𝑒𝑤𝑁𝑜𝑑𝑒 = (𝑛𝑜𝑑𝑒)𝑚𝑎𝑙𝑙𝑜𝑐(𝑠𝑖𝑧𝑒𝑜𝑓(𝑠𝑡𝑟𝑢𝑐𝑡 𝐿𝑖𝑛𝑘𝑒𝑑𝐿𝑖𝑠𝑡)); 𝑛𝑒𝑤𝑁𝑜𝑑𝑒→𝑛𝑒𝑥𝑡 = 𝑁𝑈𝐿𝐿; 𝑟𝑒𝑡𝑢𝑟𝑛 𝑛𝑒𝑤𝑁𝑜𝑑𝑒; } TEQIP -III
  • 12. node insert_at_Front(node head, int value){ node newNode; newNode = createNode(); newNode →data = value; if(head == NULL){ head = newNode; //when linked list is empty } else{ newNode→next = head; head= newNode; } return head; } TEQIP -III
  • 13. Insert at last position 1. Create a new node 2. Initialize the data and next field 3. Check if the list is empty (New node will be the first and last node) 4. Initialize search for the last node 5. Set next field of last node to new node 6. Return head pointer TEQIP -III
  • 14.  head 10 20 30 40 40 newNode TEQIP -III
  • 15.  head 10 20 30 40 40 newNode p TEQIP -III
  • 16.  head 10 20 30 40 40 newNode p TEQIP -III
  • 17. //Insert node at the end of the linked list node insert_at_End(node head, int value){ node newNode,p; newNode = createNode(); newNode→data = value; if(head == NULL){ head = newNode; //when linked list is empty } else{ p = head; while(p->next != NULL){ p = p->next; } p->next = newNode; } return head; } TEQIP -III
  • 18. Insert at any position p 1. Create a new node 2. Initialize the data and next field 3. Check if the list is empty (New node will be the first and last node) 4. Initialize temporary pointer 5. Initialize search for the predecessor of new node 6. Set next field of predecessor and new node 7. Return head pointer TEQIP -III
  • 19.  head 10 20 30 40 40 newNode p TEQIP -III
  • 20.  head 10 20 30 40 40 newNode pp 1. newNode→next=p →next TEQIP -III
  • 21.  head 10 20 30 40 40 newNode pp 2. p → next=newNode TEQIP -III
  • 22. void insert_at_pos(node head, int value) { int i,pos; struct node *newNode, *temp; newNode = (struct node *) malloc (sizeof(struct node)); if(head == NULL) { printf("n list is empty"); } else { newNode→data = value; printf("nEnter the location after which you want to insert the new node "); scanf("n%d",&pos); TEQIP -III
  • 23. temp=head; for(i=0;i<pos;i++) { temp = temp →next; if(temp == NULL) { printf("ncan't insertn"); return; } } newNode →next = temp →next; temp →next = newNode; printf("nNode inserted at given location"); } } TEQIP -III
  • 24. Complexity  Traverse/Access- O(n)  Search- O(n)  Insertion- O(1)  Deletion- O(1) ** Insertion and deletion will take O(n) time if you need to first traverse the list and then insert or delete. TEQIP -III