SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Linked Lists
2
Anatomy of a linked list
 A linked list consists of:
 A sequence of nodes
a b c d
 Each node contains a value
 and a link (pointer or reference) to some other node
 The last node contains a null link
 The list may (or may not) have a header
myList
 myList isn’t a header, it’s just a reference
3
More terminology
 A node’s successor is the next node in the sequence
 The last node has no successor
 A node’s predecessor is the previous node in the
sequence
 The first node has no predecessor
 A list’s length is the number of elements in it
 A list may be empty (contain no elements)
4
Creating links
typedef struct link
{
int data;
struct link *next;
}myList;
myList *head, *tail;
int main()
{
myList *p, *q;
p=(myList*)malloc(sizeof(myList));
q=(myList*)malloc(sizeof(myList));
p->data=44;
q->data=97;
p->next=q;
q->next=0;
 }
data next pointer
data next
data next
p:
q:
44
97
next
next
p:
q:
44 97
p: q:
5
Singly-linked lists
 Here is a singly-linked list (SLL):
 Each node contains a value and a link to its successor
(the last node has no successor)
 We have a reference to the first node in the list
 The reference is null if the list is empty
a b c d
myList
6
Creating a simple list
 A head node is just an initial node that exists at the front of
every list, even when the list is empty
 The purpose is to keep the list from being null, and to point at
the first element
 A tail node is just an initial node that exists at the end of every
list, even when the list is empty.
 The purpose is to keep the list from being null, and to point at
the last element
 In the previous code, just add this in main:
head=p
tail=q
44 97
p: q:
head:
tail:
7
Traversing a SLL
 The following method traverses a list (and
prints its elements):
void traverse()
{ p=head;
while(p!=0)
{
printf(“%d “, p->data);
p=p->next;
}
8
Traversing a SLL (animation)
489744
head
p
9
Inserting a node into a SLL
 There are many ways you might want to insert a new
node into a list:
 As the new first element
 As the new last element
 Before a given value
 After a given value
 All are possible, but differ in difficulty
10
Inserting as a new last element
 This is probably the easiest method to implement
 void insertAtLast(int value)
{
p=(myList*)malloc(sizeof(myList));
p->data=value;
p->next=tail->next;
tail=p;
}
 Use this as: insertAtLast(value);
11
Inserting at last (animation)
9744
head
48p
Find the tail node
First, copy the link from the node that’s already in the list
Then, change the link in the node that’s already in the list
tail
Then, change the link in the tail node
Example: Call: insertAtLast(48)
12
Inserting a node as a new first element
 void insertAtFirst(int value)
{
p=(myList*)malloc(sizeof(myList));
p->data=value;
p->next=head;
head=p;
}
 Use this as: insertAtFirst(value)
13
Inserting at first (animation)
9744
head
14p
Find the head node
First, change the link in the new node
tail
Then, change the link in the head node
48
Example: Call: insertAtFirst(14)
14
Inserting a node as a new first element
 void insertAtMiddle(int search_data, int value)
{
q=head;
while(q!=0)
{
if(q->data==search_data)
{
p=(myList*)malloc(sizeof(myList));
p->data=value;
p->next=q->next;
q->next=p;
}
p=p->next;
}
Use this as: insertAtMiddle(search_data, value)
Don’t change
the sequence
15
Inserting after (animation)
489744
head
29p
Find the node you want to insert after
First, copy the link from the node that’s already in the list
Then, change the link in the node that’s already in the list
q
Example: Call: insertAtMiddle(29)
16
Deleting a node from a SLL
 In order to delete a node from a SLL, you have to
change the link in its predecessor
 This is slightly tricky, because you can’t follow a
pointer backwards
 Deleting the first node in a list is a special case, because
the node’s predecessor is the list header
17
Inserting as a new last element
 This is probably the easiest method to implement
 void delete(int value)
{ q=0;
p=head;
while(p!=0)
{
if(p->data==value)
{
if(p==head)
head=p->next;
else
q->next=p->next;
}
q=p;
p=p->next;
}
Use this as: delete(value);
18
Deleting an element from a SLL
489744
head
• To delete the first element, change the link in the header
• To delete some other element, change the link in its predecessor
• What will happen for last node??? Code &Visualize yourself
(predecessor)
974414
head
48
q p
p
19
Other operations on linked lists
 Most “algorithms” on linked lists—such as insertion,
deletion, and searching—are pretty obvious; you just
need to be careful
 Sorting a linked list is just messy, since you can’t
directly access the nth
element—you have to count your
way through a lot of other elements
Question: How would you reverse a singly-linked list in
place (that is, without creating any new nodes)?
20
The End

Weitere ähnliche Inhalte

Was ist angesagt?

Circular linked list
Circular linked listCircular linked list
Circular linked list
dchuynh
 
Linked list
Linked listLinked list
Linked list
VONI
 

Was ist angesagt? (19)

Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
LINKED LISTS
LINKED LISTSLINKED LISTS
LINKED LISTS
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Linked lists
Linked listsLinked lists
Linked lists
 
Linked list
Linked listLinked list
Linked list
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6
 
linked list
linked listlinked list
linked list
 
Lecture 6: linked list
Lecture 6:  linked listLecture 6:  linked list
Lecture 6: linked list
 
Linked list
Linked list Linked list
Linked list
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
 
Linked list
Linked listLinked list
Linked list
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
linked list using c
linked list using clinked list using c
linked list using c
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
linked list
linked list linked list
linked list
 

Ähnlich wie Linked lists

Ähnlich wie Linked lists (20)

Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
CH5_Linked List.ppt
CH5_Linked List.pptCH5_Linked List.ppt
CH5_Linked List.ppt
 
CH5_Linked List.pptx
CH5_Linked List.pptxCH5_Linked List.pptx
CH5_Linked List.pptx
 
Ll.pptx
Ll.pptxLl.pptx
Ll.pptx
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptx
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
linked-list.ppt
linked-list.pptlinked-list.ppt
linked-list.ppt
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.ppt
 
Data structures
Data structuresData structures
Data structures
 
Linked list
Linked listLinked list
Linked list
 
Data Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptxData Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptx
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
Circular_Linked_List.ppt
Circular_Linked_List.pptCircular_Linked_List.ppt
Circular_Linked_List.ppt
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
Linked Lists.pdf
Linked Lists.pdfLinked Lists.pdf
Linked Lists.pdf
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 

Mehr von Himadri Sen Gupta (8)

Report of industrial training
Report of industrial trainingReport of industrial training
Report of industrial training
 
Binary search trees (1)
Binary search trees (1)Binary search trees (1)
Binary search trees (1)
 
Graphs
GraphsGraphs
Graphs
 
14 recursion
14 recursion14 recursion
14 recursion
 
Tree
TreeTree
Tree
 
Heap
HeapHeap
Heap
 
Queue
QueueQueue
Queue
 
1311004
13110041311004
1311004
 

Kürzlich hochgeladen

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Kürzlich hochgeladen (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 

Linked lists

  • 2. 2 Anatomy of a linked list  A linked list consists of:  A sequence of nodes a b c d  Each node contains a value  and a link (pointer or reference) to some other node  The last node contains a null link  The list may (or may not) have a header myList  myList isn’t a header, it’s just a reference
  • 3. 3 More terminology  A node’s successor is the next node in the sequence  The last node has no successor  A node’s predecessor is the previous node in the sequence  The first node has no predecessor  A list’s length is the number of elements in it  A list may be empty (contain no elements)
  • 4. 4 Creating links typedef struct link { int data; struct link *next; }myList; myList *head, *tail; int main() { myList *p, *q; p=(myList*)malloc(sizeof(myList)); q=(myList*)malloc(sizeof(myList)); p->data=44; q->data=97; p->next=q; q->next=0;  } data next pointer data next data next p: q: 44 97 next next p: q: 44 97 p: q:
  • 5. 5 Singly-linked lists  Here is a singly-linked list (SLL):  Each node contains a value and a link to its successor (the last node has no successor)  We have a reference to the first node in the list  The reference is null if the list is empty a b c d myList
  • 6. 6 Creating a simple list  A head node is just an initial node that exists at the front of every list, even when the list is empty  The purpose is to keep the list from being null, and to point at the first element  A tail node is just an initial node that exists at the end of every list, even when the list is empty.  The purpose is to keep the list from being null, and to point at the last element  In the previous code, just add this in main: head=p tail=q 44 97 p: q: head: tail:
  • 7. 7 Traversing a SLL  The following method traverses a list (and prints its elements): void traverse() { p=head; while(p!=0) { printf(“%d “, p->data); p=p->next; }
  • 8. 8 Traversing a SLL (animation) 489744 head p
  • 9. 9 Inserting a node into a SLL  There are many ways you might want to insert a new node into a list:  As the new first element  As the new last element  Before a given value  After a given value  All are possible, but differ in difficulty
  • 10. 10 Inserting as a new last element  This is probably the easiest method to implement  void insertAtLast(int value) { p=(myList*)malloc(sizeof(myList)); p->data=value; p->next=tail->next; tail=p; }  Use this as: insertAtLast(value);
  • 11. 11 Inserting at last (animation) 9744 head 48p Find the tail node First, copy the link from the node that’s already in the list Then, change the link in the node that’s already in the list tail Then, change the link in the tail node Example: Call: insertAtLast(48)
  • 12. 12 Inserting a node as a new first element  void insertAtFirst(int value) { p=(myList*)malloc(sizeof(myList)); p->data=value; p->next=head; head=p; }  Use this as: insertAtFirst(value)
  • 13. 13 Inserting at first (animation) 9744 head 14p Find the head node First, change the link in the new node tail Then, change the link in the head node 48 Example: Call: insertAtFirst(14)
  • 14. 14 Inserting a node as a new first element  void insertAtMiddle(int search_data, int value) { q=head; while(q!=0) { if(q->data==search_data) { p=(myList*)malloc(sizeof(myList)); p->data=value; p->next=q->next; q->next=p; } p=p->next; } Use this as: insertAtMiddle(search_data, value) Don’t change the sequence
  • 15. 15 Inserting after (animation) 489744 head 29p Find the node you want to insert after First, copy the link from the node that’s already in the list Then, change the link in the node that’s already in the list q Example: Call: insertAtMiddle(29)
  • 16. 16 Deleting a node from a SLL  In order to delete a node from a SLL, you have to change the link in its predecessor  This is slightly tricky, because you can’t follow a pointer backwards  Deleting the first node in a list is a special case, because the node’s predecessor is the list header
  • 17. 17 Inserting as a new last element  This is probably the easiest method to implement  void delete(int value) { q=0; p=head; while(p!=0) { if(p->data==value) { if(p==head) head=p->next; else q->next=p->next; } q=p; p=p->next; } Use this as: delete(value);
  • 18. 18 Deleting an element from a SLL 489744 head • To delete the first element, change the link in the header • To delete some other element, change the link in its predecessor • What will happen for last node??? Code &Visualize yourself (predecessor) 974414 head 48 q p p
  • 19. 19 Other operations on linked lists  Most “algorithms” on linked lists—such as insertion, deletion, and searching—are pretty obvious; you just need to be careful  Sorting a linked list is just messy, since you can’t directly access the nth element—you have to count your way through a lot of other elements Question: How would you reverse a singly-linked list in place (that is, without creating any new nodes)?