SlideShare a Scribd company logo
1 of 27
Linked Lists in Data Structure
Department of Information Technology
University of The Punjab,
Jhelum Campus
By Mohd. Umair Hassan (BCS-F13-23)
Why Lists
• Most simple and effective way to store things
• Shopping lists
• Works very well for the following
• Small size of list items
• Does not require any ordering of items
• Typically, no searching is required
• Processing is typically done in the order items are added to list
2Linked Lists in Data StructuresUniversity of The Punjab, Jhelum Campus
Defining Lists
• Linked List is an ordered collection of elements called nodes which
has two parts
• The nodes are connected by pointers
• The Data part and Next part
• The data contains elements
• Next contains address of another node
3Linked Lists in Data Structures
Data Next
Information part : store element of the list
Address part : pointer that indicates
the location of next node
Data Next Data Head
If no next node, the pointer
contains a NULL value
University of The Punjab, Jhelum Campus
Array vs. Linked List
Aspect Array Linked List
Size • Fixed number
• Size need to be specific during
declaration
• Grow and contract since of
insertions and deletions
• Maximum size depends on heap
Storage capacity • Static: It’s location is allocated
during compile time
• Dynamic: It’s node is located
during run time
Order and sorting • Stored consecutively • Stored randomly
Accessing the element • Direct or random access method
• Specify the array index or
subscript
• Sequential access method
• Traverse starting from the first
node in the list by pointer
Searching • Binary search and linear search • Linear search
4Linked Lists in Data StructuresUniversity of The Punjab, Jhelum Campus
List: Technical Terms
• Empty List
• When there are no elements in the list
• List Size:
• Total number of elements in the list
• Head: Identifies beginning of the list
• May refer to first element of the list
• Tail: Identifies last element of the list
• Typically, refer to last element of the list
• Node:
• Implementation of list data item
5Linked Lists in Data StructuresUniversity of The Punjab, Jhelum Campus
List: Basic Requirements
• List should be able to grow
• Provides ability to add or insert elements
• List should be able to shrink
• Provides ability to remove or delete elements
• Accessing list item
• Provides ability to access (or read) any element
• Provides ability to modify items (contents of node)
• Initialize
• Ability to clear or re-initialize list
• Create an empty list
6Linked Lists in Data StructuresUniversity of The Punjab, Jhelum Campus
List: Basic Operations
• The basic operations on liked lists are
• Creation
• Insertion
• Deletion
• Traversing
• Searching
• Concatenation
• Display
7Linked Lists in Data StructuresUniversity of The Punjab, Jhelum Campus
List: Basic Operations
• Creation
• The creation operation is used to create a linked list.
• Insertion
• The insertion operation is used to insert a new node in the linked list at the
specified position. A new node may be inserted at the beginning of a linked
list , at the end of the linked list , at the specified position in a linked list. If the
list itself is empty , then the new node is inserted as a first node.
8Linked Lists in Data StructuresUniversity of The Punjab, Jhelum Campus
List: Basic Operations
• Deletion
• The deletion operation is used to delete an item from the linked list. It may be
deleted from the beginning of a linked list , specified position in the list.
• Traversing
• The traversing operation is a process of going through all the nodes of a linked
list from one end to the another end. If we start traversing from the very first
node towards the last node, it is called forward traversing.
• If the traversal start from the last node towards the first node , it is called
back word traversing.
9Linked Lists in Data StructuresUniversity of The Punjab, Jhelum Campus
List: Basic Operations
• Searching
• The searching operation is a process of accessing the desired node in the list.
We start searching node –by-node and compare the data of the node with the
key.
• Concatenation
• The concatenation operation is the process of appending the second list to
the end of the first list. When we concatenate two lists , the resultant list
becomes larger in size.
• Display
• The display operation is used to print each and every node’s information.
10Linked Lists in Data StructuresUniversity of The Punjab, Jhelum Campus
List: Basic Operations
• Create a dynamic node
• Create a list
• Traversing a list
• Insert node
• Delete node
• Example:
struct NumNode
{ int value;
NumNode * Next;
};
NumNode *head, *current, *temp, *Nptr;
head=NULL; //the list is empty
Linked Lists in Data Structures 11
To hold the list
To traverse the list
To mark a node in the list
To allocate new node
University of The Punjab, Jhelum Campus
Create a Dynamic Node
• Using new operator.
• Example :
Nptr = new NumNode;
cout<<“Enter a number: “; Nptr NumNode
cin>>Nptr->value;
Nptr->Next=NULL;
Linked Lists in Data Structures 12
10
University of The Punjab, Jhelum Campus
Delete a Dynamic Node
• Using delete operator.
delete Nptr; //delete the node pointed by
NptrNptr=Null; //set the pointer to Null
• If the link list more than one node: Need to change pointer of record before the one
being deleted.
Linked Lists in Data Structures 13University of The Punjab, Jhelum Campus
Create a Linked List
• Using new operator.
Example :
// create a node
Nptr = new NumNode;
cout<<“Enter a number: “;
cin>>Nptr->value;
Nptr->Next=NULL;
//test if the list empty
if (head==NULL)
head=Nptr; //head pointer to
//the new node
Linked Lists in Data Structures 14University of The Punjab, Jhelum Campus
Traversing a Linked List
• There are purposes:
• To process every node in the list.
• To find the last node in the list.
while (current->Next != NULL)
current=current->Next;
Linked Lists in Data Structures 15University of The Punjab, Jhelum Campus
//create a new list
Nptr=new NumNode;
cout<<“Enter a new number: “;
cin>>Nptr->value;
Nptr-Next=NULL;
//test if the list is empty
if (head==NULL)
head=Nptr; //head pointer points to the new node
else //head pointer points to new node
{ Nptr->Next=head;
head=Nptr;
}
Linked Lists in Data Structures 16
10
head
15
head=Nptr
Inserting Node at the Beginning of the List
Process
Nptr->Next=Head
Nptr
15
head
10
Nptr
Result
University of The Punjab, Jhelum Campus
Linked List (continued)
struct listItem {
type payload;
struct listItem *next;
};
struct listItem *head;
Linked Lists in Data Structures 17
payload
next
payload
next
payload
next
payload
next
University of The Punjab, Jhelum Campus
Adding an Item to a List
Linked Lists in Data Structures 18
struct listItem *p, *q;
• Add an item pointed to by q after item pointed to by p
• Neither p nor q is NULL
payload
next
payload
next
payload
next
payload
next
payload
next
University of The Punjab, Jhelum Campus
Adding an Item to a List
Linked Lists in Data Structures 19
listItem *addAfter(listItem *p, listItem *q){
q -> next = p -> next;
p -> next = q;
return p;
}
payload
next
payload
next
payload
next
payload
next
payload
next
University of The Punjab, Jhelum Campus
Adding an Item to a List
Linked Lists in Data Structures 20
listItem *addAfter(listItem *p, listItem *q){
q -> next = p -> next;
p -> next = q;
return p;
}
payload
next
payload
next
payload
next
payload
next
payload
next
University of The Punjab, Jhelum Campus
Adding an Item to a List
Linked Lists in Data Structures 21
listItem *addAfter(listItem *p, listItem *q){
q -> next = p -> next;
p -> next = q;
return p;
}
payload
next
payload
next
payload
next
payload
next
payload
next
Question: What to do if we cannot
guarantee that p and q are non-NULL?
University of The Punjab, Jhelum Campus
Adding an Item to a List (continued)
Linked Lists in Data Structures 22
listItem *addAfter(listItem *p, listItem *q){
if (p && q) {
q -> next = p -> next;
p -> next = q;
}
return p;
}
payload
next
payload
next
payload
next
payload
next
payload
next
University of The Punjab, Jhelum Campus
What about Adding an Item before another Item?
Linked Lists in Data Structures 23
struct listItem *p;
• Add an item before item pointed to by p (p != NULL)
payload
next
payload
next
payload
next
payload
next
payload
next
University of The Punjab, Jhelum Campus
Advantages of Linked Lists
• We can dynamically allocate memory space as needed.
• We can release the unused space in the situation where the allocated
space seems to be more.
• Operation related to data elements like insertions or deletion are
more simplified.
• Operation like insertion or deletion are less time consuming.
• Linked lists provide flexibility in allowing the items to be arranged
efficiently.
Linked Lists in Data Structures 24University of The Punjab, Jhelum Campus
Applications
• Linked lists are used in many
other data structures.
• Linked lists are used in
polynomial manipulation.
• Representation of trees, stacks,
queues. etc.
• The cache in your browser that
allows you to hit the BACK
button (a linked list of URLs).
Linked Lists in Data Structures 25University of The Punjab, Jhelum Campus
• Real life example
Applications
More practical applications would be:
• A list of images that need to be burned to a CD in any imaging application
• A list of users of a website that need to be emailed some notification
• A list of objects in a 3D game that need to be rendered to the screen
Linked Lists in Data Structures 26University of The Punjab, Jhelum Campus
Topic Finished…!!!
University of The Punjab, Jhelum Campus Linked Lists in Data Structures 27

More Related Content

What's hot

Linked list
Linked listLinked list
Linked list
VONI
 

What's hot (20)

Linked List
Linked ListLinked List
Linked List
 
Linked lists
Linked listsLinked lists
Linked lists
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Linked list
Linked listLinked list
Linked list
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Ppt of operations on one way link list
Ppt of operations on one way  link listPpt of operations on one way  link list
Ppt of operations on one way link list
 
Insertion in singly linked list
Insertion in singly linked listInsertion in singly linked list
Insertion in singly linked list
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
linked list
linked list linked list
linked list
 
What is Link list? explained with animations
What is Link list? explained with animationsWhat is Link list? explained with animations
What is Link list? explained with animations
 
Linked lists 1
Linked lists 1Linked lists 1
Linked lists 1
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
 
Linklist
LinklistLinklist
Linklist
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 

Viewers also liked

07 ds and algorithm session_10
07 ds and algorithm session_1007 ds and algorithm session_10
07 ds and algorithm session_10
Niit Care
 
04 ds and algorithm session_05
04 ds and algorithm session_0504 ds and algorithm session_05
04 ds and algorithm session_05
Niit Care
 
05 ds and algorithm session_07
05 ds and algorithm session_0705 ds and algorithm session_07
05 ds and algorithm session_07
Niit Care
 
Sparse matrices
Sparse matricesSparse matrices
Sparse matrices
Zain Zafar
 

Viewers also liked (20)

linked list
linked list linked list
linked list
 
Linked list
Linked listLinked list
Linked list
 
Ppt of operations on one way link list
Ppt of operations on one way  link listPpt of operations on one way  link list
Ppt of operations on one way link list
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
07 ds and algorithm session_10
07 ds and algorithm session_1007 ds and algorithm session_10
07 ds and algorithm session_10
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
04 ds and algorithm session_05
04 ds and algorithm session_0504 ds and algorithm session_05
04 ds and algorithm session_05
 
Lec6 mod linked list
Lec6 mod linked listLec6 mod linked list
Lec6 mod linked list
 
Linked list
Linked listLinked list
Linked list
 
Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)
 
05 ds and algorithm session_07
05 ds and algorithm session_0705 ds and algorithm session_07
05 ds and algorithm session_07
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
 
Applications of data structures
Applications of data structuresApplications of data structures
Applications of data structures
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and Polynomial
 
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked listsMultiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
 
Sparse matrices
Sparse matricesSparse matrices
Sparse matrices
 
Linked lists
Linked listsLinked lists
Linked lists
 
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)
 
Link List
Link ListLink List
Link List
 
Linked list
Linked listLinked list
Linked list
 

Similar to Linked Lists

1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx
BlueSwede
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in C
Jabs6
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
babuk110
 
SIX WEEKS SUMMER TRAINING REPORT.pptx
SIX WEEKS SUMMER TRAINING REPORT.pptxSIX WEEKS SUMMER TRAINING REPORT.pptx
SIX WEEKS SUMMER TRAINING REPORT.pptx
sagabo1
 

Similar to Linked Lists (20)

Data Structures 3
Data Structures 3Data Structures 3
Data Structures 3
 
Unit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdfUnit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdf
 
lecture 02.2.ppt
lecture 02.2.pptlecture 02.2.ppt
lecture 02.2.ppt
 
Linked List
Linked ListLinked List
Linked List
 
1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in C
 
Unit 1 linked list
Unit 1 linked listUnit 1 linked list
Unit 1 linked list
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
 
unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
 
SIX WEEKS SUMMER TRAINING REPORT.pptx
SIX WEEKS SUMMER TRAINING REPORT.pptxSIX WEEKS SUMMER TRAINING REPORT.pptx
SIX WEEKS SUMMER TRAINING REPORT.pptx
 
ds bridge.pptx
ds bridge.pptxds bridge.pptx
ds bridge.pptx
 
Link list
Link listLink list
Link list
 
module 3-.pptx
module 3-.pptxmodule 3-.pptx
module 3-.pptx
 
Data structure unitfirst part1
Data structure unitfirst part1Data structure unitfirst part1
Data structure unitfirst part1
 
Data structure unitfirst part1
Data structure unitfirst part1Data structure unitfirst part1
Data structure unitfirst part1
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
 
Linked list
Linked listLinked list
Linked list
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
DSA - Copy.pptx
DSA - Copy.pptxDSA - Copy.pptx
DSA - Copy.pptx
 
Linkedlists
LinkedlistsLinkedlists
Linkedlists
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 

Recently uploaded (20)

Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
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.
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 

Linked Lists

  • 1. Linked Lists in Data Structure Department of Information Technology University of The Punjab, Jhelum Campus By Mohd. Umair Hassan (BCS-F13-23)
  • 2. Why Lists • Most simple and effective way to store things • Shopping lists • Works very well for the following • Small size of list items • Does not require any ordering of items • Typically, no searching is required • Processing is typically done in the order items are added to list 2Linked Lists in Data StructuresUniversity of The Punjab, Jhelum Campus
  • 3. Defining Lists • Linked List is an ordered collection of elements called nodes which has two parts • The nodes are connected by pointers • The Data part and Next part • The data contains elements • Next contains address of another node 3Linked Lists in Data Structures Data Next Information part : store element of the list Address part : pointer that indicates the location of next node Data Next Data Head If no next node, the pointer contains a NULL value University of The Punjab, Jhelum Campus
  • 4. Array vs. Linked List Aspect Array Linked List Size • Fixed number • Size need to be specific during declaration • Grow and contract since of insertions and deletions • Maximum size depends on heap Storage capacity • Static: It’s location is allocated during compile time • Dynamic: It’s node is located during run time Order and sorting • Stored consecutively • Stored randomly Accessing the element • Direct or random access method • Specify the array index or subscript • Sequential access method • Traverse starting from the first node in the list by pointer Searching • Binary search and linear search • Linear search 4Linked Lists in Data StructuresUniversity of The Punjab, Jhelum Campus
  • 5. List: Technical Terms • Empty List • When there are no elements in the list • List Size: • Total number of elements in the list • Head: Identifies beginning of the list • May refer to first element of the list • Tail: Identifies last element of the list • Typically, refer to last element of the list • Node: • Implementation of list data item 5Linked Lists in Data StructuresUniversity of The Punjab, Jhelum Campus
  • 6. List: Basic Requirements • List should be able to grow • Provides ability to add or insert elements • List should be able to shrink • Provides ability to remove or delete elements • Accessing list item • Provides ability to access (or read) any element • Provides ability to modify items (contents of node) • Initialize • Ability to clear or re-initialize list • Create an empty list 6Linked Lists in Data StructuresUniversity of The Punjab, Jhelum Campus
  • 7. List: Basic Operations • The basic operations on liked lists are • Creation • Insertion • Deletion • Traversing • Searching • Concatenation • Display 7Linked Lists in Data StructuresUniversity of The Punjab, Jhelum Campus
  • 8. List: Basic Operations • Creation • The creation operation is used to create a linked list. • Insertion • The insertion operation is used to insert a new node in the linked list at the specified position. A new node may be inserted at the beginning of a linked list , at the end of the linked list , at the specified position in a linked list. If the list itself is empty , then the new node is inserted as a first node. 8Linked Lists in Data StructuresUniversity of The Punjab, Jhelum Campus
  • 9. List: Basic Operations • Deletion • The deletion operation is used to delete an item from the linked list. It may be deleted from the beginning of a linked list , specified position in the list. • Traversing • The traversing operation is a process of going through all the nodes of a linked list from one end to the another end. If we start traversing from the very first node towards the last node, it is called forward traversing. • If the traversal start from the last node towards the first node , it is called back word traversing. 9Linked Lists in Data StructuresUniversity of The Punjab, Jhelum Campus
  • 10. List: Basic Operations • Searching • The searching operation is a process of accessing the desired node in the list. We start searching node –by-node and compare the data of the node with the key. • Concatenation • The concatenation operation is the process of appending the second list to the end of the first list. When we concatenate two lists , the resultant list becomes larger in size. • Display • The display operation is used to print each and every node’s information. 10Linked Lists in Data StructuresUniversity of The Punjab, Jhelum Campus
  • 11. List: Basic Operations • Create a dynamic node • Create a list • Traversing a list • Insert node • Delete node • Example: struct NumNode { int value; NumNode * Next; }; NumNode *head, *current, *temp, *Nptr; head=NULL; //the list is empty Linked Lists in Data Structures 11 To hold the list To traverse the list To mark a node in the list To allocate new node University of The Punjab, Jhelum Campus
  • 12. Create a Dynamic Node • Using new operator. • Example : Nptr = new NumNode; cout<<“Enter a number: “; Nptr NumNode cin>>Nptr->value; Nptr->Next=NULL; Linked Lists in Data Structures 12 10 University of The Punjab, Jhelum Campus
  • 13. Delete a Dynamic Node • Using delete operator. delete Nptr; //delete the node pointed by NptrNptr=Null; //set the pointer to Null • If the link list more than one node: Need to change pointer of record before the one being deleted. Linked Lists in Data Structures 13University of The Punjab, Jhelum Campus
  • 14. Create a Linked List • Using new operator. Example : // create a node Nptr = new NumNode; cout<<“Enter a number: “; cin>>Nptr->value; Nptr->Next=NULL; //test if the list empty if (head==NULL) head=Nptr; //head pointer to //the new node Linked Lists in Data Structures 14University of The Punjab, Jhelum Campus
  • 15. Traversing a Linked List • There are purposes: • To process every node in the list. • To find the last node in the list. while (current->Next != NULL) current=current->Next; Linked Lists in Data Structures 15University of The Punjab, Jhelum Campus
  • 16. //create a new list Nptr=new NumNode; cout<<“Enter a new number: “; cin>>Nptr->value; Nptr-Next=NULL; //test if the list is empty if (head==NULL) head=Nptr; //head pointer points to the new node else //head pointer points to new node { Nptr->Next=head; head=Nptr; } Linked Lists in Data Structures 16 10 head 15 head=Nptr Inserting Node at the Beginning of the List Process Nptr->Next=Head Nptr 15 head 10 Nptr Result University of The Punjab, Jhelum Campus
  • 17. Linked List (continued) struct listItem { type payload; struct listItem *next; }; struct listItem *head; Linked Lists in Data Structures 17 payload next payload next payload next payload next University of The Punjab, Jhelum Campus
  • 18. Adding an Item to a List Linked Lists in Data Structures 18 struct listItem *p, *q; • Add an item pointed to by q after item pointed to by p • Neither p nor q is NULL payload next payload next payload next payload next payload next University of The Punjab, Jhelum Campus
  • 19. Adding an Item to a List Linked Lists in Data Structures 19 listItem *addAfter(listItem *p, listItem *q){ q -> next = p -> next; p -> next = q; return p; } payload next payload next payload next payload next payload next University of The Punjab, Jhelum Campus
  • 20. Adding an Item to a List Linked Lists in Data Structures 20 listItem *addAfter(listItem *p, listItem *q){ q -> next = p -> next; p -> next = q; return p; } payload next payload next payload next payload next payload next University of The Punjab, Jhelum Campus
  • 21. Adding an Item to a List Linked Lists in Data Structures 21 listItem *addAfter(listItem *p, listItem *q){ q -> next = p -> next; p -> next = q; return p; } payload next payload next payload next payload next payload next Question: What to do if we cannot guarantee that p and q are non-NULL? University of The Punjab, Jhelum Campus
  • 22. Adding an Item to a List (continued) Linked Lists in Data Structures 22 listItem *addAfter(listItem *p, listItem *q){ if (p && q) { q -> next = p -> next; p -> next = q; } return p; } payload next payload next payload next payload next payload next University of The Punjab, Jhelum Campus
  • 23. What about Adding an Item before another Item? Linked Lists in Data Structures 23 struct listItem *p; • Add an item before item pointed to by p (p != NULL) payload next payload next payload next payload next payload next University of The Punjab, Jhelum Campus
  • 24. Advantages of Linked Lists • We can dynamically allocate memory space as needed. • We can release the unused space in the situation where the allocated space seems to be more. • Operation related to data elements like insertions or deletion are more simplified. • Operation like insertion or deletion are less time consuming. • Linked lists provide flexibility in allowing the items to be arranged efficiently. Linked Lists in Data Structures 24University of The Punjab, Jhelum Campus
  • 25. Applications • Linked lists are used in many other data structures. • Linked lists are used in polynomial manipulation. • Representation of trees, stacks, queues. etc. • The cache in your browser that allows you to hit the BACK button (a linked list of URLs). Linked Lists in Data Structures 25University of The Punjab, Jhelum Campus • Real life example
  • 26. Applications More practical applications would be: • A list of images that need to be burned to a CD in any imaging application • A list of users of a website that need to be emailed some notification • A list of objects in a 3D game that need to be rendered to the screen Linked Lists in Data Structures 26University of The Punjab, Jhelum Campus
  • 27. Topic Finished…!!! University of The Punjab, Jhelum Campus Linked Lists in Data Structures 27