SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Linked List
Pointer Implementation (Linked List)
Ensure that the list is not stored contiguously
 use a linked list
 a series of structures that are not necessarily adjacent in
memory
 Each node contains the element and a pointer to a
structure containing its successor
the last cell’s next link points to NULL
 Compared to the array implementation,
the pointer implementation uses only as much space as is needed for the
elements currently on the list
but requires space for the pointers in each cell
Nodes and Linked Lists
 A linked list is a list that can grow and shrink while
the program is running
 A linked list is constructed using pointers
 A linked list often consists of structs that
contain a pointer variable connecting them to other
dynamic variables
 A linked list can be visualized as items, drawn as
boxes, connected to other items by arrows
10 12 14 end
head
Linked Lists
A linked list is a series of connected nodes
Each node contains at least
 A piece of data (any type)
 Pointer to the next node in the list
Head: pointer to the first node
The last node points to NULL
10 ∅
Head
20 30
10
data pointer
node
Implementing Nodes
Nodes are implemented in C as “struct”
 Example: A structure to store two data items and
a pointer to another node of the same type,
along with a type definition might be:
struct Node
{
int item;
struct Node *next;
};
Self-Referential Structures
A structure may not contain an object of its own type.
struct Node {
int item;
struct Node n; /* Invalid */
};
However, a structure may contain a pointer to the same
type.
struct Node {
int item;
struct Node *pn; /* Valid */
};
The head of a List
The box labeled head, is not a node, but a pointer
variable that points to a node.
Pointer variable head is declared as:
struct Node *head;
10 ∅
head
20 30
Accessing Items in a Node
One way to change the number in the first node from
10 to 12:
head->item = 12;
12 ∅
head
20 30
NULL
The defined constant NULL is used as…
 An end marker for a linked list
 A program can step through a list of nodes by following
the pointers, but when it finds a node containing NULL,
it knows it has come to the end of the list
 The value of a pointer that has nothing to point to
The value of NULL is 0
Any pointer can be assigned the value NULL:
double* there = NULL;
Linked Lists
A linked list is a list of nodes in which each node
has a member variable that is a pointer that
points to the next node in the list
 The first node is called the head
 The pointer variable head, points to the first node
 The pointer named head is not the head of the list…it
points to the head of the list
 The last node contains a pointer set to NULL
Building a Linked List:
Declaring Pointer Variable head
With the node defined and a type definition to
make or code easier to understand, we can
declare the pointer variable head:
struct Node *head;
 head is a pointer variable that will point to the head node
when the node is created
Building a Linked List:
Creating the First Node
To create the first node, the operator new is used
to create a new dynamic variable:
head = (struct Node *)malloc(sizeof(struct Node));
 Now head points to the first, and only, node in the
list
Building a Linked List:
Initializing the Node
Now that head points to a node, we need to
give values to the member variables of the node:
head->item = 10;
head->next = NULL;
 Since this node is the last node, the link is set to NULL
10 ∅
head
Traversing Singly Linked List
void Traverse(struct Node *head)
 Print the data of all the elements
 Print the number of the nodes in the list
 From “main()” function call “Traverse(head)”.
void Traverse(struct Node *head)
{
int num = 0;
struct Node *currNode;
currNode = head;
while (currNode != NULL){
printf(“%d “,currNode->item);
currNode = currNode->next;
num++;
}
printf("Number of nodes in the list: %dn”,num);
}
Traversing Singly Linked List (2)
Output: 12 currnode = head
Num: 1
12 ∅
head
20 30
currnode
Traversing Singly Linked List (2)
Output: 12 20
Num: 2
currnode = currnode->next
12 ∅
head
20 30
currnode
Traversing Singly Linked List (2)
Output: 12 20 30
Num: 3
currnode = currnode->next
12 ∅
head
20 30
currnode
Traversing Singly Linked List (2)
Output: 12 20 30
Num: 3
currnode = NULL at this moment. So function
terminates.
12 ∅
head
20 30
currnode
Inserting a new node
Node* InsertNode(int index, double x)
 Insert a node with data equal to x after the index’th elements. (i.e.,
when index = 0, insert the node as the first element;
 when index = 1, insert the node after the first element, and so
on)
 If the insertion is successful, return the inserted node.
 Otherwise, return NULL.
 (If index is < 0 or > length of the list, the insertion will fail.)
Steps
 Locate index’th element
 Allocate memory for the new node
 Point the new node to its successor
 Point the new node’s predecessor to the new node newNode
index’th
element
Inserting a new node
Possible cases of InsertNode
 Insert into an empty list
 Insert in front
 Insert at back
 Insert in middle
But, in fact, only need to handle two cases
 Insert as the first node (Case 1 and Case 2)
 Insert in the middle or at the end of the list (Case 3 and Case 4)
Inserting a new node
struct Node* InsertNode(int index, double x) {
if (index < 0) return NULL;
int currIndex = 1;
struct Node* currNode = head;
while (currNode!=NULL && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;
Node* newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = x;
if (index == 0) {
newNode->next = head;
head = newNode;
}
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return newNode;
}
Try to locate index’th
node. If it doesn’t exist,
return NULL.
Inserting a new node
struct Node* InsertNode(int index, double x) {
if (index < 0) return NULL;
int currIndex = 1;
struct Node* currNode = head;
while (currNode!=NULL && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;
Node* newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = x;
if (index == 0) {
newNode->next = head;
head = newNode;
}
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return newNode;
}
Create a new node
Inserting a new node
struct Node* InsertNode(int index, double x) {
if (index < 0) return NULL;
int currIndex = 1;
struct Node* currNode = head;
while (currNode!=NULL && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;
Node* newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = x;
if (index == 0) {
newNode->next = head;
head = newNode;
}
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return newNode;
}
Insert as first element
head
newNode
Inserting a new node
struct Node* InsertNode(int index, double x) {
if (index < 0) return NULL;
int currIndex = 1;
struct Node* currNode = head;
while (currNode!=NULL && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;
Node* newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = x;
if (index == 0) {
newNode->next = head;
head = newNode;
}
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return newNode;
}
Insert after currNode
newNode
currNode
Finding a node
int FindNode(double x, struct Node *head)
 Search for a node with the value equal to x in the list.
 If such a node is found, return its position. Otherwise, return
0.
int FindNode(double x, struct Node *head) {
struct Node* currNode = head;
int currIndex = 1;
while (currNode!=NULL && currNode->data != x) {
currNode = currNode->next;
currIndex++;
}
if (currNode!=NULL) return currIndex;
else return 0;
}
Deleting a node
int DeleteNode(double x)
 Delete a node with the value equal to x from the list.
 If such a node is found, return its position. Otherwise, return
0.
Steps
 Find the desirable node (similar to FindNode)
 Release the memory occupied by the found node
 Set the pointer of the predecessor of the found node to the
successor of the found node
Like InsertNode, there are two special cases
 Delete first node
 Delete the node in middle or at the end of the list
Deleting a node
int DeleteNode(double x) {
struct Node* prevNode = NULL;
struct Node* currNode = head;
int currIndex = 1;
while (currNode!=NULL && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode!=NULL) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else {
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
Try to find the node with its
value equal to x
Deleting a node
int DeleteNode(double x) {
struct Node* prevNode = NULL;
struct Node* currNode = head;
int currIndex = 1;
while (currNode!=NULL && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode!=NULL) {
if (prevNode!=NULL) {
prevNode->next = currNode->next;
free (currNode);
}
else {
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
currNodeprevNode
Deleting a node
int DeleteNode(double x) {
struct Node* prevNode = NULL;
struct Node* currNode = head;
int currIndex = 1;
while (currNode!=NULL && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode!=NULL) {
if (prevNode!=NULL) {
prevNode->next = currNode->next;
free (currNode);
}
else {
head = currNode->next;
free( currNode);
}
return currIndex;
}
return 0;
}
currNodehead
Destroying the list
void DeleteList(struct Node *head)
 Use the DeleteList() function to release all the memory used by
the list.
 Step through the list and delete each node one by one.
void DelteList(struct Node *head) {
struct Node* currNode = head, *nextNode = NULL;
while (currNode != NULL)
{
nextNode = currNode->next;
// destroy the current node
free (currNode);
currNode = nextNode;
}
}

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
Linked list
Linked listLinked list
Linked list
 
linked list
linked listlinked list
linked list
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked list Linked list
Linked list
 
LINKED LISTS
LINKED LISTSLINKED LISTS
LINKED LISTS
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Linked list
Linked listLinked list
Linked list
 
linked list
linked listlinked list
linked list
 
CSE240 Doubly Linked Lists
CSE240 Doubly Linked ListsCSE240 Doubly Linked Lists
CSE240 Doubly Linked Lists
 
Lecture 6: linked list
Lecture 6:  linked listLecture 6:  linked list
Lecture 6: linked list
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Linked list
Linked listLinked list
Linked list
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)
 
Binary tree
Binary treeBinary tree
Binary tree
 
Linked list
Linked listLinked list
Linked list
 

Ähnlich wie Linkedlist

Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsAakash deep Singhal
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfKylaMaeGarcia1
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.pptWaf1231
 
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfin C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfeyewaregallery
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structuresNiraj Agarwal
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfrohit219406
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfvishalateen
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked listAmit Vats
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfarjunstores123
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfpoblettesedanoree498
 

Ähnlich wie Linkedlist (20)

linked-list.ppt
linked-list.pptlinked-list.ppt
linked-list.ppt
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
 
3.linked list
3.linked list3.linked list
3.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
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.ppt
 
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfin C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdf
 
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
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
 
Linked list
Linked listLinked list
Linked list
 

Mehr von Masud Parvaze

Mehr von Masud Parvaze (6)

Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Discrete Mathematics Tree
Discrete Mathematics  TreeDiscrete Mathematics  Tree
Discrete Mathematics Tree
 
Higher order math
Higher order mathHigher order math
Higher order math
 
The end of something11
The end of something11The end of something11
The end of something11
 
Mechanism of-a-capacitor
Mechanism of-a-capacitorMechanism of-a-capacitor
Mechanism of-a-capacitor
 
Galvanmometer
Galvanmometer Galvanmometer
Galvanmometer
 

Kürzlich hochgeladen

Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
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
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfrs7054576148
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 

Kürzlich hochgeladen (20)

Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
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
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
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
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 

Linkedlist

  • 2. Pointer Implementation (Linked List) Ensure that the list is not stored contiguously  use a linked list  a series of structures that are not necessarily adjacent in memory  Each node contains the element and a pointer to a structure containing its successor the last cell’s next link points to NULL  Compared to the array implementation, the pointer implementation uses only as much space as is needed for the elements currently on the list but requires space for the pointers in each cell
  • 3. Nodes and Linked Lists  A linked list is a list that can grow and shrink while the program is running  A linked list is constructed using pointers  A linked list often consists of structs that contain a pointer variable connecting them to other dynamic variables  A linked list can be visualized as items, drawn as boxes, connected to other items by arrows 10 12 14 end head
  • 4. Linked Lists A linked list is a series of connected nodes Each node contains at least  A piece of data (any type)  Pointer to the next node in the list Head: pointer to the first node The last node points to NULL 10 ∅ Head 20 30 10 data pointer node
  • 5. Implementing Nodes Nodes are implemented in C as “struct”  Example: A structure to store two data items and a pointer to another node of the same type, along with a type definition might be: struct Node { int item; struct Node *next; };
  • 6. Self-Referential Structures A structure may not contain an object of its own type. struct Node { int item; struct Node n; /* Invalid */ }; However, a structure may contain a pointer to the same type. struct Node { int item; struct Node *pn; /* Valid */ };
  • 7. The head of a List The box labeled head, is not a node, but a pointer variable that points to a node. Pointer variable head is declared as: struct Node *head; 10 ∅ head 20 30
  • 8. Accessing Items in a Node One way to change the number in the first node from 10 to 12: head->item = 12; 12 ∅ head 20 30
  • 9. NULL The defined constant NULL is used as…  An end marker for a linked list  A program can step through a list of nodes by following the pointers, but when it finds a node containing NULL, it knows it has come to the end of the list  The value of a pointer that has nothing to point to The value of NULL is 0 Any pointer can be assigned the value NULL: double* there = NULL;
  • 10. Linked Lists A linked list is a list of nodes in which each node has a member variable that is a pointer that points to the next node in the list  The first node is called the head  The pointer variable head, points to the first node  The pointer named head is not the head of the list…it points to the head of the list  The last node contains a pointer set to NULL
  • 11. Building a Linked List: Declaring Pointer Variable head With the node defined and a type definition to make or code easier to understand, we can declare the pointer variable head: struct Node *head;  head is a pointer variable that will point to the head node when the node is created
  • 12. Building a Linked List: Creating the First Node To create the first node, the operator new is used to create a new dynamic variable: head = (struct Node *)malloc(sizeof(struct Node));  Now head points to the first, and only, node in the list
  • 13. Building a Linked List: Initializing the Node Now that head points to a node, we need to give values to the member variables of the node: head->item = 10; head->next = NULL;  Since this node is the last node, the link is set to NULL 10 ∅ head
  • 14. Traversing Singly Linked List void Traverse(struct Node *head)  Print the data of all the elements  Print the number of the nodes in the list  From “main()” function call “Traverse(head)”. void Traverse(struct Node *head) { int num = 0; struct Node *currNode; currNode = head; while (currNode != NULL){ printf(“%d “,currNode->item); currNode = currNode->next; num++; } printf("Number of nodes in the list: %dn”,num); }
  • 15. Traversing Singly Linked List (2) Output: 12 currnode = head Num: 1 12 ∅ head 20 30 currnode
  • 16. Traversing Singly Linked List (2) Output: 12 20 Num: 2 currnode = currnode->next 12 ∅ head 20 30 currnode
  • 17. Traversing Singly Linked List (2) Output: 12 20 30 Num: 3 currnode = currnode->next 12 ∅ head 20 30 currnode
  • 18. Traversing Singly Linked List (2) Output: 12 20 30 Num: 3 currnode = NULL at this moment. So function terminates. 12 ∅ head 20 30 currnode
  • 19. Inserting a new node Node* InsertNode(int index, double x)  Insert a node with data equal to x after the index’th elements. (i.e., when index = 0, insert the node as the first element;  when index = 1, insert the node after the first element, and so on)  If the insertion is successful, return the inserted node.  Otherwise, return NULL.  (If index is < 0 or > length of the list, the insertion will fail.) Steps  Locate index’th element  Allocate memory for the new node  Point the new node to its successor  Point the new node’s predecessor to the new node newNode index’th element
  • 20. Inserting a new node Possible cases of InsertNode  Insert into an empty list  Insert in front  Insert at back  Insert in middle But, in fact, only need to handle two cases  Insert as the first node (Case 1 and Case 2)  Insert in the middle or at the end of the list (Case 3 and Case 4)
  • 21. Inserting a new node struct Node* InsertNode(int index, double x) { if (index < 0) return NULL; int currIndex = 1; struct Node* currNode = head; while (currNode!=NULL && index > currIndex) { currNode = currNode->next; currIndex++; } if (index > 0 && currNode == NULL) return NULL; Node* newNode = (struct Node *)malloc(sizeof(struct Node)); newNode->data = x; if (index == 0) { newNode->next = head; head = newNode; } else { newNode->next = currNode->next; currNode->next = newNode; } return newNode; } Try to locate index’th node. If it doesn’t exist, return NULL.
  • 22. Inserting a new node struct Node* InsertNode(int index, double x) { if (index < 0) return NULL; int currIndex = 1; struct Node* currNode = head; while (currNode!=NULL && index > currIndex) { currNode = currNode->next; currIndex++; } if (index > 0 && currNode == NULL) return NULL; Node* newNode = (struct Node *)malloc(sizeof(struct Node)); newNode->data = x; if (index == 0) { newNode->next = head; head = newNode; } else { newNode->next = currNode->next; currNode->next = newNode; } return newNode; } Create a new node
  • 23. Inserting a new node struct Node* InsertNode(int index, double x) { if (index < 0) return NULL; int currIndex = 1; struct Node* currNode = head; while (currNode!=NULL && index > currIndex) { currNode = currNode->next; currIndex++; } if (index > 0 && currNode == NULL) return NULL; Node* newNode = (struct Node *)malloc(sizeof(struct Node)); newNode->data = x; if (index == 0) { newNode->next = head; head = newNode; } else { newNode->next = currNode->next; currNode->next = newNode; } return newNode; } Insert as first element head newNode
  • 24. Inserting a new node struct Node* InsertNode(int index, double x) { if (index < 0) return NULL; int currIndex = 1; struct Node* currNode = head; while (currNode!=NULL && index > currIndex) { currNode = currNode->next; currIndex++; } if (index > 0 && currNode == NULL) return NULL; Node* newNode = (struct Node *)malloc(sizeof(struct Node)); newNode->data = x; if (index == 0) { newNode->next = head; head = newNode; } else { newNode->next = currNode->next; currNode->next = newNode; } return newNode; } Insert after currNode newNode currNode
  • 25. Finding a node int FindNode(double x, struct Node *head)  Search for a node with the value equal to x in the list.  If such a node is found, return its position. Otherwise, return 0. int FindNode(double x, struct Node *head) { struct Node* currNode = head; int currIndex = 1; while (currNode!=NULL && currNode->data != x) { currNode = currNode->next; currIndex++; } if (currNode!=NULL) return currIndex; else return 0; }
  • 26. Deleting a node int DeleteNode(double x)  Delete a node with the value equal to x from the list.  If such a node is found, return its position. Otherwise, return 0. Steps  Find the desirable node (similar to FindNode)  Release the memory occupied by the found node  Set the pointer of the predecessor of the found node to the successor of the found node Like InsertNode, there are two special cases  Delete first node  Delete the node in middle or at the end of the list
  • 27. Deleting a node int DeleteNode(double x) { struct Node* prevNode = NULL; struct Node* currNode = head; int currIndex = 1; while (currNode!=NULL && currNode->data != x) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (currNode!=NULL) { if (prevNode) { prevNode->next = currNode->next; delete currNode; } else { head = currNode->next; delete currNode; } return currIndex; } return 0; } Try to find the node with its value equal to x
  • 28. Deleting a node int DeleteNode(double x) { struct Node* prevNode = NULL; struct Node* currNode = head; int currIndex = 1; while (currNode!=NULL && currNode->data != x) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (currNode!=NULL) { if (prevNode!=NULL) { prevNode->next = currNode->next; free (currNode); } else { head = currNode->next; delete currNode; } return currIndex; } return 0; } currNodeprevNode
  • 29. Deleting a node int DeleteNode(double x) { struct Node* prevNode = NULL; struct Node* currNode = head; int currIndex = 1; while (currNode!=NULL && currNode->data != x) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (currNode!=NULL) { if (prevNode!=NULL) { prevNode->next = currNode->next; free (currNode); } else { head = currNode->next; free( currNode); } return currIndex; } return 0; } currNodehead
  • 30. Destroying the list void DeleteList(struct Node *head)  Use the DeleteList() function to release all the memory used by the list.  Step through the list and delete each node one by one. void DelteList(struct Node *head) { struct Node* currNode = head, *nextNode = NULL; while (currNode != NULL) { nextNode = currNode->next; // destroy the current node free (currNode); currNode = nextNode; } }