SlideShare a Scribd company logo
LINKED LIST
Linked list
• linear data structure
• Elements are not stored at contiguous
memory locations
• Elements in a linked list are linked using
pointers
TYPES
• Linked list consists of nodes where each node
contains a data field and a reference(link) to the
next node in the list.
• Linked list comprise of group or list of nodes in
which each node have link to next node to form a
chain
• Types of linked list
– Singly linked list
– Doubly linked list
– Circular linked list
Linked List vs Array
Array Linked List
data structure that contains a collection
of similar type of data elements
non-primitive data structure contains a
collection of unordered linked elements
known as nodes
Accessing an element in an array is fast Accessing an element in an array is bit
slower.
Operations in arrays consume a lot of
time
operations in Linked lists is fast
Arrays are of fixed size. Linked lists are dynamic and flexible and
can expand and contract its size.
In array, memory is assigned during
compile time
Linked list it is allocated during execution
or runtime.
Elements are stored consecutively in
arrays
Elements are stored randomly in Linked
lists.
requirement of memory is less due to
actual data being stored within the index
in the array
more memory in Linked Lists due to
storage of additional next and previous
referencing elements.
memory utilization is inefficient in the
array
memory utilization is efficient in the
linked list.
Why Linked List?
• Arrays have the following limitations
– The size of the array is fixed
– To insert a new element in an array, existing
elements have to be shifted
• Advantages of LL over arrays
- Dynamic size
- Ease of insertion/deletion
- Random access is not allowed
Representation of linked list
• Represented by a pointer to the first node of
the linked list
• The first node is called the head.
• If the linked list is empty, then the value of the
head is NULL.
• Each node in a list consists of at least two
parts:
1) data
2) Pointer (Or Reference) to the next node
Representation of linked list
Basic Operations
• Insertion − Adds an element into the list
• Deletion − Deletes an element from the list
• Display − Displays the complete list
• Search − Searches an element using the given
key
Linked list node creation
struct Node {
int data;
struct Node* next;
} *head;
Explanation:
declared structure of type “NODE”,
First field stores actual data and another field
stores address
Insertion
• insertion operation can be performed in three
ways
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific position in the list
Insert at Beginning of the list
1)newnode->next=head;
2)head=newnode
newnode
Inserting At Beginning of the list
steps to insert a new node at
beginning of the single linked list
Step 1 - Create a newnode with
given value.
Step 2 - Check whether list
is Empty (head == NULL)
Step 3 - If it is Empty then,
set newnode→next = NULL
and head = new Node.
Step 4 - If it is Not Empty then,
set newnode→next = head and head
= newnode
void insertAtBeginning(int value)
{
struct Node *newnode;
newnode = (struct
Node*)malloc(sizeof(struct Node));
newnode->data = value;
if(head == NULL)
{
newnode->next = NULL;
head = newnode;
}
else
{
newnode->next = head;
head = newnode;
Inserting At Beginning of the list
void insertAtBeginning(int value)
{
struct Node *newnode;
newnode = (struct Node*)malloc(sizeof(struct Node));
newnode->data = value;
newnode->next = head;
head = newnode;
}
Insert at End of the list
1) Traverse –Now temp points to last node
2) temp ->next=newnode;
Insert at End of the list
steps to insert a new node at end of the
single linked list
Step 1 - Create a newnode with given
value and newnode → next as NULL.
Step 2 - Check whether list
is Empty (head == NULL).
Step 3 - If it is Empty then,
set head = newnode.
Step 4 - If it is Not Empty then, define a
node pointer temp and initialize
with head.
Step 5 - Keep moving the temp to its
next node until it to the last node in
the list (until temp → next is equal
to NULL).
Step 6 - Set temp → next = newnode.
void insertAtEnd(int value)
{
struct Node *newnode;
newnode = (struct
Node*)malloc(sizeof(struct Node));
newnode->data = value;
newnode->next = NULL;
if(head == NULL)
head = newnode;
else
{
struct Node *temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newnode;
}
}
Insert at a given position
void insertatmiddle(int value, int pos)
{
struct Node *newNode,*temp;
Int i,pos;
newnode = (struct Node*)malloc(sizeof(struct Node));
newnode->data = value;
temp=head;
for(i=1;i<pos-1;i++)
{
temp=temp->next;
}
If(temp==head)
{
newnode->next=head;
head=newnode;
}
else
{
newnode->next=temp->next;
temp->next=newnode;
}
Deletion Operation
• locate the target node to be removed
• left (previous) node of the target node now
should point to the next node of the target
node
• LeftNode.next −> TargetNode.next;
• remove the target node is pointing at
– TargetNode.next −> NULL;
Delete at begin
Void deleteatbegin()
{
struct node *temp;
temp=head;
printf(“%d is deleted”, temp->data);
head=temp->next;
free(temp);
}
Delete at last
void deleteatlast()
{
struct node *last, *secondlast;
last=head;
secondlast=head;
while(last->next!=NULL)
{
secondlast= last;
last=last->next;
}
If(last==head)
{
head=NULL;
}
else
{
secondlast->next=NULL;
free(last);
}
Displaying the linked list
Void display()
{
struct node *temp;
temp=head;
while(temp!=NULL)
{
printf(“%d”, temp->data);
temp=temp->next;
}
}
Counting the no. of nodes in a LL
void count()
{
int c=0;
struct node *temp;
temp=head;
while(temp!=NULL)
{
c++;
temp=temp->next;
}
printf(“Number of nodes in the LL is %d”, c);
}
Searching in a LL
void search()
{
int key;
printf("enter the element to search");
scanf("%d",&key);
temp = head;
// Iterate till last element until key is not found
while (temp != NULL && temp->data != key)
{
temp = temp->next;
}
if(temp->data==key)
printf("element found");
else
printf("element not found");
}
Delete first by key
void deleteFirstByKey()
{
int key;
struct node *tempprev;
/* Check if head node contains key */
printf("enter the element to delete");
scanf("%d",&key);
while (head != NULL && head->data == key)
{
// Get reference of head node
temp = head;
// Adjust head node link
head = head->next;
// Delete prev since it contains reference to head node
free(temp);
}
Delete first by key(contd…)
temp = head;
/* For each node in the list */
while (temp != NULL)
{
// Current node contains key
if (temp->data == key)
{
// Adjust links for previous node
if (tempprev != NULL)
tempprev->next = temp->next;
// Delete current node
free(temp);
}
tempprev = temp;
temp = temp->next;
}
}
DOUBLY LINKED LIST
Insertion at beginning of the LL
Void insertatbegin()
{
struct node *newnode;
newnode = (struct node*)malloc(sizeof(struct node));
printf(“Enter the value”);
scanf(“%d”, &newnode->data);
newnode->prev= NULL;
newnode->next = head;
head ->prev= newnode;
head=newnode;
}
Insertion at end of the LL
Void insertatend()
{
struct node *newnode;
If(head==NULL)
{
newnode = (struct node*)malloc(sizeof(struct node));
printf(“Enter the value”);
scanf(“%d”, &newnode->data);
newnode->prev= NULL;
newnode->next = NULL;
head=newnode;
current=head;
}
Insertion at end of the LL(CONTD…)
else
{
newnode = (struct node*)malloc(sizeof(struct node));
printf(“Enter the value”);
scanf(“%d”, &newnode->data);
newnode->prev= current;
newnode->next = NULL;
Current->next=newnode;
current=newnode;
}

More Related Content

What's hot

linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 
Stacks
StacksStacks
Stacks
sweta dargad
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Queues
QueuesQueues
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
stack & queue
stack & queuestack & queue
stack & queue
manju rani
 
Singly link list
Singly link listSingly link list
Singly link list
Rojin Khadka
 
Stack
StackStack
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
Krish_ver2
 
Arrays
ArraysArrays
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
SIVASHANKARIRAJAN
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
MAHALAKSHMI P
 
Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
DivyeshKumar Jagatiya
 
single linked list
single linked listsingle linked list
single linked list
Sathasivam Rangasamy
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
Anandhasilambarasan D
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
Dhrumil Panchal
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
Hossain Md Shakhawat
 

What's hot (20)

linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Stacks
StacksStacks
Stacks
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Queues
QueuesQueues
Queues
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
stack & queue
stack & queuestack & queue
stack & queue
 
Singly link list
Singly link listSingly link list
Singly link list
 
Stack
StackStack
Stack
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Arrays
ArraysArrays
Arrays
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
 
single linked list
single linked listsingle linked list
single linked list
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 

Similar to Linked list

Linked list
Linked list Linked list
Linked list
Arbind Mandal
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
Durga Devi
 
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
Mani .S (Specialization in Semantic Web)
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
kasthurimukila
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
Ain-ul-Moiz Khawaja
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
Dabbal Singh Mahara
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
msohail37
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptx
skilljiolms
 
Linked List Presentation in data structurepptx
Linked List Presentation in data structurepptxLinked List Presentation in data structurepptx
Linked List Presentation in data structurepptx
nikhilcse1
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
JITTAYASHWANTHREDDY
 
module 3-.pptx
module 3-.pptxmodule 3-.pptx
module 3-.pptx
kumarkaushal17
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
SonaPathak5
 
Linked list
Linked listLinked list
Linked list
Luis Goldster
 
Linked list
Linked listLinked list
Linked list
Young Alista
 
Linked list
Linked listLinked list
Linked list
Tony Nguyen
 
Linked list
Linked listLinked list
Linked list
Harry Potter
 
Linked list
Linked listLinked list
Linked list
James Wong
 
Linked list
Linked listLinked list
Linked list
Hoang Nguyen
 
Linked list
Linked listLinked list
Linked list
Fraboni Ec
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
arnold 7490
 

Similar to Linked list (20)

Linked list
Linked list Linked list
Linked list
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
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 and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptx
 
Linked List Presentation in data structurepptx
Linked List Presentation in data structurepptxLinked List Presentation in data structurepptx
Linked List Presentation in data structurepptx
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
module 3-.pptx
module 3-.pptxmodule 3-.pptx
module 3-.pptx
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 

Recently uploaded

官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
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
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
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
 
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
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
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
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
Aditya Rajan Patra
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
NazakatAliKhoso2
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
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
 
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
 

Recently uploaded (20)

官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
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
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
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
 
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...
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.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
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
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
 
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
 

Linked list

  • 2. Linked list • linear data structure • Elements are not stored at contiguous memory locations • Elements in a linked list are linked using pointers
  • 3. TYPES • Linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list. • Linked list comprise of group or list of nodes in which each node have link to next node to form a chain • Types of linked list – Singly linked list – Doubly linked list – Circular linked list
  • 4. Linked List vs Array Array Linked List data structure that contains a collection of similar type of data elements non-primitive data structure contains a collection of unordered linked elements known as nodes Accessing an element in an array is fast Accessing an element in an array is bit slower. Operations in arrays consume a lot of time operations in Linked lists is fast Arrays are of fixed size. Linked lists are dynamic and flexible and can expand and contract its size. In array, memory is assigned during compile time Linked list it is allocated during execution or runtime. Elements are stored consecutively in arrays Elements are stored randomly in Linked lists. requirement of memory is less due to actual data being stored within the index in the array more memory in Linked Lists due to storage of additional next and previous referencing elements. memory utilization is inefficient in the array memory utilization is efficient in the linked list.
  • 5. Why Linked List? • Arrays have the following limitations – The size of the array is fixed – To insert a new element in an array, existing elements have to be shifted • Advantages of LL over arrays - Dynamic size - Ease of insertion/deletion - Random access is not allowed
  • 6. Representation of linked list • Represented by a pointer to the first node of the linked list • The first node is called the head. • If the linked list is empty, then the value of the head is NULL. • Each node in a list consists of at least two parts: 1) data 2) Pointer (Or Reference) to the next node
  • 8. Basic Operations • Insertion − Adds an element into the list • Deletion − Deletes an element from the list • Display − Displays the complete list • Search − Searches an element using the given key
  • 9. Linked list node creation struct Node { int data; struct Node* next; } *head; Explanation: declared structure of type “NODE”, First field stores actual data and another field stores address
  • 10. Insertion • insertion operation can be performed in three ways 1. Inserting At Beginning of the list 2. Inserting At End of the list 3. Inserting At Specific position in the list
  • 11. Insert at Beginning of the list 1)newnode->next=head; 2)head=newnode newnode
  • 12. Inserting At Beginning of the list steps to insert a new node at beginning of the single linked list Step 1 - Create a newnode with given value. Step 2 - Check whether list is Empty (head == NULL) Step 3 - If it is Empty then, set newnode→next = NULL and head = new Node. Step 4 - If it is Not Empty then, set newnode→next = head and head = newnode void insertAtBeginning(int value) { struct Node *newnode; newnode = (struct Node*)malloc(sizeof(struct Node)); newnode->data = value; if(head == NULL) { newnode->next = NULL; head = newnode; } else { newnode->next = head; head = newnode;
  • 13. Inserting At Beginning of the list void insertAtBeginning(int value) { struct Node *newnode; newnode = (struct Node*)malloc(sizeof(struct Node)); newnode->data = value; newnode->next = head; head = newnode; }
  • 14. Insert at End of the list 1) Traverse –Now temp points to last node 2) temp ->next=newnode;
  • 15. Insert at End of the list steps to insert a new node at end of the single linked list Step 1 - Create a newnode with given value and newnode → next as NULL. Step 2 - Check whether list is Empty (head == NULL). Step 3 - If it is Empty then, set head = newnode. Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head. Step 5 - Keep moving the temp to its next node until it to the last node in the list (until temp → next is equal to NULL). Step 6 - Set temp → next = newnode. void insertAtEnd(int value) { struct Node *newnode; newnode = (struct Node*)malloc(sizeof(struct Node)); newnode->data = value; newnode->next = NULL; if(head == NULL) head = newnode; else { struct Node *temp = head; while(temp->next != NULL) temp = temp->next; temp->next = newnode; } }
  • 16. Insert at a given position
  • 17. void insertatmiddle(int value, int pos) { struct Node *newNode,*temp; Int i,pos; newnode = (struct Node*)malloc(sizeof(struct Node)); newnode->data = value; temp=head; for(i=1;i<pos-1;i++) { temp=temp->next; } If(temp==head) { newnode->next=head; head=newnode; } else { newnode->next=temp->next; temp->next=newnode; }
  • 18. Deletion Operation • locate the target node to be removed • left (previous) node of the target node now should point to the next node of the target node • LeftNode.next −> TargetNode.next;
  • 19. • remove the target node is pointing at – TargetNode.next −> NULL;
  • 20. Delete at begin Void deleteatbegin() { struct node *temp; temp=head; printf(“%d is deleted”, temp->data); head=temp->next; free(temp); }
  • 21. Delete at last void deleteatlast() { struct node *last, *secondlast; last=head; secondlast=head; while(last->next!=NULL) { secondlast= last; last=last->next; } If(last==head) { head=NULL; } else { secondlast->next=NULL; free(last); }
  • 22. Displaying the linked list Void display() { struct node *temp; temp=head; while(temp!=NULL) { printf(“%d”, temp->data); temp=temp->next; } }
  • 23. Counting the no. of nodes in a LL void count() { int c=0; struct node *temp; temp=head; while(temp!=NULL) { c++; temp=temp->next; } printf(“Number of nodes in the LL is %d”, c); }
  • 24. Searching in a LL void search() { int key; printf("enter the element to search"); scanf("%d",&key); temp = head; // Iterate till last element until key is not found while (temp != NULL && temp->data != key) { temp = temp->next; } if(temp->data==key) printf("element found"); else printf("element not found"); }
  • 25. Delete first by key void deleteFirstByKey() { int key; struct node *tempprev; /* Check if head node contains key */ printf("enter the element to delete"); scanf("%d",&key); while (head != NULL && head->data == key) { // Get reference of head node temp = head; // Adjust head node link head = head->next; // Delete prev since it contains reference to head node free(temp); }
  • 26. Delete first by key(contd…) temp = head; /* For each node in the list */ while (temp != NULL) { // Current node contains key if (temp->data == key) { // Adjust links for previous node if (tempprev != NULL) tempprev->next = temp->next; // Delete current node free(temp); } tempprev = temp; temp = temp->next; } }
  • 28. Insertion at beginning of the LL Void insertatbegin() { struct node *newnode; newnode = (struct node*)malloc(sizeof(struct node)); printf(“Enter the value”); scanf(“%d”, &newnode->data); newnode->prev= NULL; newnode->next = head; head ->prev= newnode; head=newnode; }
  • 29. Insertion at end of the LL Void insertatend() { struct node *newnode; If(head==NULL) { newnode = (struct node*)malloc(sizeof(struct node)); printf(“Enter the value”); scanf(“%d”, &newnode->data); newnode->prev= NULL; newnode->next = NULL; head=newnode; current=head; }
  • 30. Insertion at end of the LL(CONTD…) else { newnode = (struct node*)malloc(sizeof(struct node)); printf(“Enter the value”); scanf(“%d”, &newnode->data); newnode->prev= current; newnode->next = NULL; Current->next=newnode; current=newnode; }