Chapter-3 Linked Lists Lecture Note by SHMDCH

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Chapter-3: Linked Lists

Chapter 3: Linked Lists (4hr)


3.1. Review on Pointer and Dynamic Memory allocation
3.2. Singly Linked List and Its Implementation
3.3. Doubly Linked List and Its Implementation
3.4. Circular Linked Lists and Its Implementation
Linked Lists introduction
What is Linked List?
• A linked list is also a collection of elements, but the elements are not
stored in a consecutive location
• A linked list is a linear data structure that includes a series of
connected nodes.
• Each node stores the data and the address of the next node.
• Each struct node has a data item and a pointer to another struct
node.
• The address of the first node a special name called HEAD
• The last node in the linked list can be identified because its next
portion points to NULL.
For example,
Linked Lists introduction …
• Linked list can also be defined as the collection of the nodes in which
one node is connected to another node, and node consists of two parts:
i.e.,
• one is the data part and
• the second one is the address part.
Representation of Linked Lists …
Representation of Linked List
Let's see how each node of the linked list is represented. Each node consists:
•A data item
•An address of another node
We wrap both the data item and the next node reference in a struct as:

✓ From the figure there are three unique hubs


having address 100, 200, and 300
individually.
✓ The principal hub contains the location of Fig. Linked List
the following hub, i.e., 200,
✓ The subsequent hub contains the location of
the last hub, i.e., 300, and
✓ The third hub contains the NULL
Let us create a simple Linked List with three items to understand how this works.

/* Initialize nodes */
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL; /* Allocate memory */
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node)); /* Assign data values */
One->data = 1;
two->data = 2;
three->data=3; /* Connect nodes */
one->next = two;
two->next = three;
three->next = NULL; /* Save address of first node in head */
head = one;
// Linked list implementation in C++ // Assign value values
#include <bits/stdc++.h> one->value = 1;
#include <iostream> two->value = 2;
using namespace std;
three->value = 3;
// Creating a node
class Node {
public: // Connect nodes
int value; one->next = two;
Node* next; two->next = three;
}; three->next = NULL;
int main() { // print the linked list value
Node* head; head = one;
Node* one = NULL; while (head != NULL) {
Node* two = NULL;
cout << head->value;
Node* three = NULL;
// allocate 3 nodes in the heap head = head->next;
one = new Node(); }
two = new Node(); }
three = new Node();
Linked List Complexity and Applications
Linked List Complexity Applications of Linked List
Time Complexity
•Dynamic memory allocation
Worst case Average Case •Implemented in stack and queue
Search O(n) O(n) •In undo functionality of
Insert O(1) O(1)
software
•Hash tables, Graphs
Deletion O(1) O(1)

Space complexity: O(n)


Different Types of Linked List
1. Single Linked List
2. Doubly Linked List
3. Circular Linked List
4. Doubly Circular Linked List
1. Single Linked List
✓ It is the most common. Each node has data and a pointer to the next node.

struct node {
int data;
struct node *next;
}

Ex. of single linked list with data and address


2. Doubly Linked List
❖ A pointer to the previous node in a doubly-linked list. Thus, we can go in either direction:
forward or backward.
❖ Doubly linked list is a type of linked list in which each node consists of 3 components:
✓ *prev - address of the previous node
✓ *data - data item
✓ *next - address of next node
a doubly linked list node

Ex. of doubly linked list with data and address


Representation of Doubly Linked List
✓ Let's see how we can represent a doubly linked list on an algorithm/code.
Suppose we have a doubly linked list:

Newly created doubly linked list

In the following code, one, two, and three are the nodes with data items 1, 2 and 3 respectively.
✓ For node one: next stores the address of two and prev stores null (there is no node before it)
✓ For node two: next stores the address of three and prev stores the address of one
✓ For node three: next stores null (there is no node after it) and prev stores the address of two
/* Initialize nodes */
struct node *head;
Doubly Linked List Represented as: struct node *one = NULL;
struct node { struct node *two = NULL;
struct node *three = NULL; /* Allocate memory */
int data;
one = malloc(sizeof(struct node));
struct node *next; two = malloc(sizeof(struct node));
struct node *prev; three = malloc(sizeof(struct node)); /* Assign data values */
} one->data = 1;
two->data = 2;
three->data = 3; /* Connect nodes */
one->next = two;
one->prev = NULL;
two->next = three;
two->prev = one;
three->next = NULL;
three->prev = two; /* Save address of first node in head */
head = one;
Doubly Linked List Complexity
Doubly Linked List Time
Space Complexity
Complexity Complexity

Insertion Operation O(1) or O(n) O(1)


Deletion Operation O(1) O(1)

1. Complexity of Insertion Operation


✓ The insertion operations that do not require traversal have the time complexity of O(1).
✓ And, insertion that requires traversal has time complexity of O(n).
✓ The space complexity is O(1).
2. Complexity of Deletion Operation
✓ All deletion operations run with time complexity of O(1).
✓ And, the space complexity is O(1).
Linked List Operations
Linked List Operations
✓ Linked list operations that allow us to perform different actions on linked
lists.
✓ For example, the insertion operation adds a new element to the linked list.
✓ Basic linked list operations that we will cover.
a) Traversal
 access each element of the linked list;
 displaying the contents of a linked list
b) Insertion - adds a new element to the linked list
c) Deletion - removes the existing elements
d) Search - find a node in the linked list
e) Sort - sort the nodes of the linked list
Insertion node on Doubly Linked List
Insertion on a Doubly Linked List
✓ Pushing a node to a doubly-linked list is similar to pushing a node to a linked
list, but extra work is required to handle the pointer to the previous node.
✓ We can insert elements at 3 different positions of a doubly-linked list:
1. Insertion at the beginning
2. Insertion in-between nodes
3. Insertion at the End

Suppose we have a double-linked list with elements 1, 2, and 3.


Insertion
I. Insertionat
at first …
the Beginning
Let's add a node with value 6 at the beginning of the doubly linked list we made above.
1. Create a new node
✓ allocate memory for newNode
✓ assign the data to newNode.

2. Set prev and next pointers of new node


✓ point next of newNode to the first node of the doubly linked list
✓ point prev to null

Reorganize the pointers (changes are denoted by purple arrows)


Insertion at first …
3. Make new node as head node
✓ Point prev of the first node to newNode (now the previous head is the second node)
✓ Point head to newNode

Reorganize the pointers


Insertion
II. Insertion in between
between two …nodes
✓ Let's add a node with value 6 after node with value 1 in the doubly linked list.
1. Create a new node
✓ allocate memory for newNode
✓ assign the data to newNode.

2. Set the next pointer of new node and previous node


✓ assign the value of next from previous node to the next of newNode
✓ assign the address of newNode to the next of previous node

Reorganize the pointers


Insertion between …
3. Set the prev pointer of new node and the next node
✓ assign the value of prev of next node to the prev of newNode
✓ assign the address of newNode to the prev of next node

Reorganize the pointers

The final doubly linked list is after this insertion is

Final lists
Insertion end …
III. Insertion at the End
Let's add a node with value 6 at the end of the doubly linked list.
1. Create a new node
✓ allocate memory for newNode
✓ assign the data to newNode

2. Set prev and next pointers of new node and the previous node
✓ If the linked list is empty, make the newNode as the head node.
✓ Otherwise, traverse to the end of the doubly linked list and

Reorganize the pointers


Insertion end …
The final doubly linked list looks like this.

The final list after node 2 added


Complexity
Doubly Linked List Complexity
Doubly Linked List Time Space
Complexity Complexity Complexity

Insertion Operation O(1) or O(n) O(1)

Deletion Operation O(1) O(1)

1. Complexity of Insertion Operation


✓ The insertion operations that do not require traversal have the time
complexity of O(1).
✓ And, insertion that requires traversal has time complexity of O(n).
✓ The space complexity is O(1).
2. Complexity of Deletion Operation
✓ All deletion operations run with time complexity of O(1).
✓ And, the space complexity is O(1)
Circular Linked …
Circular Linked List
✓ A circular linked list is a type of linked list in which the first and the last
nodes are also connected to each other to form a circle.
✓ A circular linked list is a variation of a linked list in which the last
element is linked to the first element.
✓ This forms a circular loop.

✓ A circular linked list can be either singly linked or doubly linked.


✓ for singly linked list, next pointer of last item points to the first item
✓ In the doubly linked list, prev pointer of the first item points to the last item
as well.
Circular Doubly Linked List
2. Circular Doubly Linked List
✓ The last node storing the address of the first node, the first node will also
store the address of the last node.
✓ the singly circular linked list to represent the working of circular linked list.

Circular Doubly Linked List Representation


•Q & A
Assignments
A. Individual assignment (project work)
1. Prepare document that delete linked list:
i. Delete the First Node of Doubly Linked List
ii. Delete the Inner Node of Doubly Linked List
iii. Delete the Last Node of Doubly Linked List
2. Write C++/python program that delete in three cases.

B. Group Assignment(project work)


1. Prepare document on a Circular Linked List that
a) Insertion at the beginning
b) Insertion in-between nodes
c) Insertion at the end
2. Write C++/python program that insert node into circular linked list
in three cases.

You might also like