Professional Documents
Culture Documents
Lecture 5 Linked Lists in C++
Lecture 5 Linked Lists in C++
Lecture 5 Linked Lists in C++
• The elements in a linked list are linked using pointers as shown in the image
below:
• In simple words, a linked list consists of nodes where each node contains a data
field and a pointer(link) to the next node in the list.
• A node is a collection of two sub-
What is a Linked List?(2)
elements or parts. A data part
that stores the element and
a pointer(link) part, that points
to the next node.
• A linked list is formed when many such nodes are linked together to
form a chain. Each node points to the next node present in the order.
The first node is always used as a reference to traverse the list and is
called HEAD. The last node always points to NULL.
What is a Linked List?(4)
• Linked lists are one of the most important data structures ,in fact they are the
second most used data structure after arrays but unlike arrays they can store an
indefinite amount of items.
• We often face situations, where the data is dynamic in nature and number of
data can’t be predicted or the number of data keeps changing during program
execution. Linked lists are very useful in this type of situations
• Linked lists are a way to store data with structures so that the programmer can
automatically create a new place to store data whenever necessary. Specifically,
the programmer writes a struct or class definition that contains variables holding
information about something, and then has a pointer to a struct of its type. Each
of these individual struct or classes in the list are commonly known as a node.
Key Differences Between
Array and Linked List
1. An array is the data structure that contains a collection of
similar type data elements whereas the Linked list is considered as
non-primitive data structure contains a collection of unordered
linked elements known as nodes.
(1) The size of the arrays is fixed: So we must know the upper limit on
the number of elements in advance. Also, generally, the allocated
memory is equal to the upper limit irrespective of the usage.
node* ptr;
int data;
cout<<"Enter value to create new node: "; //prompts user to enter a value
cin>>data; //accepts input from the user
ptr=create(data); //passes input from user to function called create on r previous slide to
create a node
}
Basic Operations on a Linked List
1. Creating a List
2. Inserting an element in a list
3. Deleting an element from a list
4. Searching a list
5. Reversing a list
Inserting the node in a SLL
• There are 3 cases here:-
temp=start; //Remember start stored the address of the previous first node(3), thus assign that
address to a variable called temp
start=p; //set the address of the new node to start i.e. insert new node at the beginning
p->next=temp; //make the new node created (2)point to the previous first node(3)
}}
Inserting at the end
• Here we simply need to make the
next pointer of the last node
point to the new node
void insert_end(node* p){ //p is the new node to be inserted
node *q=start; //declares q as the pointer pointing to the head or start i.e. points to the first
element of the linked list
if(start==NULL) //because the last element of a SLL points ALWAYS points to NULL, if then the
value of q or pointer pointing to the head is NULL
{
start=p; //insert the new node p at the end of the SLL which was formerly NULL
cout<<”\n Node inserted successfully at the end…!!!\n”;
}
else{ //if start however not equal to NULL loop till you get to a point where it is NULL
while(q->link!=NULL) //thus while q is NOT pointing to NULL i.e. (q isn’t the last node)
q=q->link; //traverse the list until q is the last node.
q->next=p; //insert the new node then q at the last node
}
}
• Here we again need to do 2 Inserting after an
steps :-
element
• (a)Make the next pointer of
the node to be inserted point
to the next node of the node
after which you want to insert
the node
• But NOT in case of linked list since random access is NOT available it would
become complex to do binary search in it
• We can thus perform a simple linear search traversal. In linear search each
node is traversed till the data in the node matches with the required value
void search(int x){
node*temp=start;
while(temp!=NULL){
if(temp->data==x){
cout<<“FOUND ”<<temp->data;
break;
}
temp=temp->next;
}
}
Basic Operations on a Linked List
1. Creating a List
2. Inserting an element in a list
3. Deleting an element from a list
4. Searching a list
5. Reversing a list
Reversing a linked list
• We can reverse a linked list by reversing the direction of the
links between 2 nodes
Reversing a linked list(2)
• We make use of 3 structure pointers say p,q,r. At any instant q will
point to the node next to p and r will point to the node next to q
• For next iteration p=q and q=r
• At the end we will change head to the last node
void reverse( ){ while(q!=NULL){
node*p,*q,*r; r=q->link;
if(start==NULL){ q->link=p;
cout<<"\n List is empty\n"; p=q;
return; q=r;
} }
p=start; start=p;
q=p->link; cout<<"\n Reversed successfully";
p->link=NULL; }
Some Applications of Linked List
1. The cache in your browser that allows 6. Image viewer – Previous and next
you to hit the BACK button (a linked list images are linked, hence can be
of URLs) accessed by next and previous button.
2. Undo functionality in Photoshop or Word 7. Music Player – Songs in music player are
(a linked list of state) linked to previous and next song. you
can play songs either from starting or
3. Linked list are used as a building block for ending of the list.
many other data structures such as 8. Maintaining directory of names.
stacks, queues, hash tables, binary trees
etc.
9. Performing arithmetic operations on
4. representing sparse matrices long integers.
• Linked lists allow insertion and removal of nodes at any point in the list, and allow doing
so with a constant number of operations by keeping the link previous to the link being
added or removed in memory during list traversal.
• On the other hand, since simple linked lists by themselves do not allow random access to
the data or any form of efficient indexing, many basic operations—such as obtaining the
last node of the list, finding a node that contains a given datum, or locating the place
where a new node should be inserted—may require iterating through most or all of the
list elements.