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

Linked Lists

  • 1.
    Linked Lists inData Structure Department of Information Technology University of The Punjab, Jhelum Campus By Mohd. Umair Hassan (BCS-F13-23)
  • 2.
    Why Lists • Mostsimple 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 • LinkedList 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. LinkedList 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 DynamicNode • 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 DynamicNode • 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 LinkedList • 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 LinkedList • 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 newlist 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) structlistItem { 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 Itemto 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 Itemto 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 Itemto 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 Itemto 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 Itemto 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 Addingan 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 LinkedLists • 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 listsare 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 applicationswould 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 ofThe Punjab, Jhelum Campus Linked Lists in Data Structures 27