SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

Mais procurados (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
 

Destaque (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
 

Semelhante a Linked List Basics

Unit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdfUnit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdfKanchanPatil34
 
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.pptxBlueSwede
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in CJabs6
 
Unit 1 linked list
Unit 1 linked listUnit 1 linked list
Unit 1 linked listLavanyaJ28
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operationsbabuk110
 
SIX WEEKS SUMMER TRAINING REPORT.pptx
SIX WEEKS SUMMER TRAINING REPORT.pptxSIX WEEKS SUMMER TRAINING REPORT.pptx
SIX WEEKS SUMMER TRAINING REPORT.pptxsagabo1
 
Data structure unitfirst part1
Data structure unitfirst part1Data structure unitfirst part1
Data structure unitfirst part1Amar Rawat
 
Data structure unitfirst part1
Data structure unitfirst part1Data structure unitfirst part1
Data structure unitfirst part1Amar Rawat
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructuresNguync91368
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdfSonaPathak5
 

Semelhante a Linked List Basics (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
 

Último

Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
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.pptxheathfieldcps1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 

Último (20)

Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
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
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 

Linked List Basics

  • 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