Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

1

Lecture #12

Linked List

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


2 Definition of linked list

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations.
The elements in a linked list are linked using pointers as shown in the below image:

In simple words, a linked list consists of nodes where each node contains a data field and a
reference(link) to the next node in the list.

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


3

A linked list is made up of two items that are data and a reference to the next node. A reference to the next
node is given with the help of pointers and data is the value of a node. Each node contains data and links to
the other nodes. It is an ordered collection of data elements called a node and the linear order is maintained
by pointers.

Some of the features of the linked list are as follows:

 The consecutive elements are connected by pointers.


 The size of a linked list is not fixed.
 The last node of the linked list points to null.
 Memory is not wasted but extra memory is consumed as it also uses pointers to keep track of the
next successive node.
 The entry point of a linked list is known as the head.
© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh
4 The various types of linked lists are as follows:

 Singly Linked List


 Doubly Linked List
 Circular Linked List

Singly Linked List: It is the most basic linked list in which traversal is unidirectional i.e. from the head
node to the last node.

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


5

Doubly Linked List: In this linked list, traversal can be done in both ways, and hence it requires
an extra pointer.

Circular Linked List: This linked list is unidirectional but in this list, the last node points to the first i.e. the
head node and hence it becomes circular in nature.

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


6 Arrow operator (->)

Arrow operator (->) in C++ also known as Class Member Access Operator is a combination of two
different operators that is Minus operator (-) and greater than operator (>). It is used to access the public
members of a class, structure, or members of union with the help of a pointer variable.

There is a .(dot) operator in C++ that is also used to access the members of a class. But .(dot operator)
accesses the member or variable directly means without using the pointers whereas instead of accessing
members directly the arrow operator(->) in C++ uses a pointer to access them.

So the advantage of the -> operator is that it can deal with both pointer and non-pointer access but the
dot(.) operator can only be used to access the members of a class.

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


7 Arrow Operators -> in C++
int main()
#include <iostream>
{
using namespace std;
// allocating memory to the object of the class
// Creating a class
Student *s = new Student();
class Student
// accessing member functions of the class
{
s->percentage(387);
private:
s->print();
int total_marks;
float total_percentage;
//Creating Object of Class
public:
Student b;
void percentage(int tm)
b.percentage(250);
{
b.print();
total_marks=tm;
total_percentage = (total_marks*100)/500;
return 0;
}
}
// Method to print total percentage
void print()
{
cout<<"Total percentage of the Student: "<<endl;
cout<<total_percentage<<"%";
}
};
© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh
8 Program to Create / Insert / Traverse / Delete Linked List - 1
class Linkedlist void Linkedlist::insertNode(int data)
#include <iostream> { {
using namespace std; Node* head; // Create the new Node.
class Node { public: Node* newNode = new Node(data);
public: // Default constructor // Assign to head
int data; Linkedlist() if (head == NULL)
Node* next; { {
// Default constructor head = NULL; head = newNode;
Node() } return;
{ //Declare function to be called }
data = 0; void insertNode(int); // Traverse till end of list
next = NULL; void printList(); Node* temp = head;
} void deleteNode(int); while (temp->next != NULL)
// Parameterised Constructor int deletebyValue(int); {
Node(int data) }; // Update temp
{ temp = temp->next;
this->data = data; }
this->next = NULL; // Insert at the last.
} temp->next = newNode;
}; }

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


9 Program to Create Linked List - 2
int Linkedlist::deletebyValue(int xx)
void Linkedlist::printList()
{
{
Node* temp = head;
Node* temp = head;
int ListLen = 0;
// Check for empty list.
while (temp != NULL)
if (head == NULL) {
{
cout << "List empty" << endl;
if(temp->data==xx)
return;
{
}
cout << temp->data << " --> ";
// Traverse the list.
break;
while (temp != NULL) {
}
cout << temp->data << " --> ";
ListLen++;
temp = temp->next;
temp = temp->next;
}
}
}
//cout<<"Delete Index"<<ListLen + 1;
return ListLen + 1;
}

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


Program to Create Linked List - 3 // Declare temp1
10 temp1 = head;
void Linkedlist::deleteNode(int nodeOffset) // Deleting the head.
{ if (nodeOffset == 1) {
Node *temp1 = head, *temp2 = NULL; // Update head
int ListLen = 0; head = head->next;
delete temp1;
if (head == NULL) { return;
cout << "List empty." << endl; }
return; // Traverse the list to
} // find the node to be deleted.
while (nodeOffset--> 1)
// Find length of the linked-list. {
while (temp1 != NULL) { // Update temp2
temp1 = temp1->next; temp2 = temp1;
ListLen++; // Update temp1
} temp1 = temp1->next;
// Check if the position to be }
// deleted is greater than the length // Change the next pointer
// of the linked list. // of the previous node.
if (ListLen < nodeOffset) { temp2->next = temp1->next;
cout << "Index out of range" // Delete the node
<< endl; delete temp1;
return; }
} © LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh
11 Program to Create Linked List - 4

int main()
{
Linkedlist list;
// Inserting nodes
list.insertNode(11);
list.insertNode(22);
list.insertNode(33);
cout << "Elements of the list are: ";
// Print the list
list.printList();
cout << endl;
int x= list.deletebyValue(33);
cout<<x;
// Delete node at position 2.
list.deleteNode(x);

cout << "Elements of the list are: ";


list.printList();
cout << endl;
return 0;
} © LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh
12 Advantages / Disadvantages of Singly Linked List

Advantages
 it is very easier for the accessibility of a node in the forward direction.
 the insertion and deletion of a node are very easy.
 the Requirement will less memory when compared to doubly, circular or doubly circular linked list.
 the Singly linked list is the very easy data structure to implement.
 During the execution, we can allocate or deallocate memory easily.
 Insertion and deletion of elements don’t need the movement of all the elements when compared to an array.

Disadvantages

 Accessing the preceding node of a current node is not possible as there is no backward traversal.
 the Accessing of a node is very time-consuming.

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


13 Doubly Linked List

A doubly linked list (DLL) is a special type of linked list in which each node contains a pointer to the
previous node as well as the next node of the linked list.

Syntax: class Node {


public:
int data;
// Pointer to next node in DLL
Node* next;

// Pointer to previous node in DLL


Node* prev;
};

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


14 Doubly Linked List

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


15 Advantages of Doubly Linked List over the singly linked list:

 A DLL can be traversed in both forward and backward directions.


 The delete operation in DLL is more efficient if a pointer to the node to be deleted is given.
 We can quickly insert a new node before a given node.
 In a singly linked list, to delete a node, a pointer to the previous node is needed. To get this
previous node, the list is traversed. In DLL, we can get the previous node using the previous
pointer.of Doubly Linked List over the singly linked list:
Disadvantages

 Every node of DLL Requires extra space for a previous pointer. It is possible to implement DLL
with a single pointer though.
 All operations require an extra pointer previous to be maintained. For example, in insertion, we
need to modify previous pointers together with the next pointers. For example in the following
functions for insertions at different positions, we need 1 or 2 extra steps to set the previous pointer.

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


16 Applications of Doubly Linked List:

 It is used by web browsers for backward and forward navigation of web pages
 LRU ( Least Recently Used ) / MRU ( Most Recently Used ) Cache are constructed using Doubly Linked
Lists.
 Used by various applications to maintain undo and redo functionalities.
 In Operating Systems, a doubly linked list is maintained by thread scheduler to keep track of processes that
are being executed at that time.

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


17

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh


18

© LPU :: CAP267 Data Structures :: Dr. Amanpreet Singh

You might also like