Lecture 3:Liked list
description
Transcript of Lecture 3:Liked list
![Page 1: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/1.jpg)
LECTURE 3:LIKED LIST
![Page 2: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/2.jpg)
linear list Concept:
Linear list : Each element has a unique successor. Two approaches to implement a linear listArray: Inefficient when element needs to be
inserted or deleted.Linked list: Efficient when element needs to be
inserted or deleted.
![Page 3: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/3.jpg)
Categories of linear list
![Page 4: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/4.jpg)
Linked list
One disadvantage of using arrays to store data is that arrays are static structures and therefore cannot be easily extended or reduced to fit the data set .
![Page 5: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/5.jpg)
Linked list con..
Arrays are also expensive to maintain new insertions and deletions.
In this chapter we consider another data structure called Linked Lists that addresses some of the limitations of arrays.
![Page 6: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/6.jpg)
Linked list con..
A linked list, in its simplest form, is a collection of nodes that together form a linear ordering.
Each node is an object that stores a reference to an element and a reference, called next, point to another node.
![Page 7: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/7.jpg)
Linked list con..
The order of the nodes is determined by the address, called the link, stored in each node
Every node (except the last node) contains the address of the next node
Components of a node : Data: stores the relevant information Link: stores the address of the next node
![Page 8: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/8.jpg)
Linked list –Node Structure
Structure of a node:
Data link
![Page 9: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/9.jpg)
Linked list –Node Structure con..
A node with one data field
number
name
Home_Address phone
A node with three data fields
A structure in a node
name
id grdPts
![Page 10: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/10.jpg)
Linked list –Head Structure
Head or first : Holds the address of the first node in
the list The info part of the node can be either a
value of a primitive type or a reference to an object
![Page 11: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/11.jpg)
Linked list –Head Structure con..
![Page 12: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/12.jpg)
Linked list classes
Class Node Represents nodes on a list It has two instance variables - info (of type int, but it can be any other type) - link (of type Node) public class Node { public int info; public Node link;}
![Page 13: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/13.jpg)
Linked List: Some Properties
Linked list with four nodes
![Page 14: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/14.jpg)
Linked List: Some Properties
Now consider the statement current = head;
Linked list after current = head; executes
![Page 15: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/15.jpg)
Linked List: Some Properties
Now consider the statement current = current.link;
List after the statement current = current.link;
![Page 16: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/16.jpg)
Linked List: Some Properties
![Page 17: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/17.jpg)
Singly Linked list Operations Insertion Deletion Traversal
![Page 18: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/18.jpg)
Traversing a Linked List
Basic operations of a linked list that require the link
to be traversed Search the list for an item Insert an item in the list Delete an item from the list You cannot use head to traverse the list You would lose the nodes of the list Use another reference variable of the same type
as head: current
![Page 19: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/19.jpg)
Traversing a Linked List con..
The following code traverses the listcurrent = head;while (current != null) {//Process currentcurrent = current.link;
![Page 20: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/20.jpg)
Insertion in a Singly Linked List Three steps : Allocate memory for the new node and insert
data. Point the new node to its successor. Point the new node’s predecessor to the new nodeThree cases: Insert into an empty list Insert at beginning Insert at middle Insert at end.
![Page 21: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/21.jpg)
Insertion at the beginning: The list is empty if the head points to
null. When using a singly linked list, we can
easily insert an element at the head of the list.
The main idea is that we create a new node, set its next link to refer to the same object as head, and then set head to point to the new node.
![Page 22: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/22.jpg)
![Page 23: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/23.jpg)
Insertion at the beginning con..
Insertion of an element at the head of a singly linked list
(a) before the insertion.(b) creation of a new node.(c) after the insertion.
![Page 24: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/24.jpg)
Algorithm of Insertion at the beginning Inserting a new node v at the beginning of
a singly linked list. Note that this method works even if the
list is empty. we set the next pointer for the new node v
before we make variable head point to v.
![Page 25: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/25.jpg)
Insertion in the middle:
Consider the following linked list
You want to create a new node with info 50 and
insert it after p
![Page 26: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/26.jpg)
Insertion in the middle con..
The following statements create and store 50 in the
info field of a new node Node newNode = new Node(); //create newNode newNode.info = 50; //store 50 in the new node
Create newNode and store 50 in it
![Page 27: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/27.jpg)
Insertion in the middle con..
The following statements insert the node in the
linked list at the required placenewNode.link = p.link;p.link = newNode; The sequence of statements to insert
the node is very important.
![Page 28: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/28.jpg)
Insertion in the middle con..
newNode.link = p.link; executes
p.link = newNode; executes
![Page 29: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/29.jpg)
Insertion at the Tail (end)
We can also easily insert an element at the tail of the list, provided we keep a reference to the tail node
In this case, we create a new node, assign its next reference to point to the null object, set the next reference of the tail to point to this new object, and then assign the tail reference itself to this new node.
![Page 30: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/30.jpg)
![Page 31: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/31.jpg)
Insertion at the Tail (end) con..
(a) before the insertion (b) creation of a new node (c) after the insertion. Note that: we set the next link for the tail in (b)
before we assign the tail variable to point to the new node in (c).
![Page 32: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/32.jpg)
Algorithm of Insertion at the end
Inserting a new node at the end of a singly linked list. This method works also if the list is empty. Note that we set the next pointer for the old tail node before we make variable tail point to the new node.
![Page 33: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/33.jpg)
Deletion in a Singly Linked List
The reverse operation of inserting a new element at the head of a linked list remove an element at the head.
This operation is illustrated in removal of an element at the head of a singly linked list
![Page 34: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/34.jpg)
(a) before the removal(b) "linking out" the old new node(c) after the removal.
![Page 35: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/35.jpg)
Algorithm of Removing the node from the beginning of a singly linked list
![Page 36: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/36.jpg)
Unfortunately, we cannot easily delete the tail node of a singly linked list.
Even if we have a tail reference directly to the last node of the list, we must be able to access the node before the last node in order to remove the last node. But we can not reach the node before the tail by following next links from the tail.
The only way to access this node is to start from the head of the list and search all the way through the list. But such a sequence of link hopping operations could take a long time.
![Page 37: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/37.jpg)
Doubly Linked Lists
As we saw in the previous section, , removing an element at the tail of a singly linked list is not easy.
Indeed, it is time consuming to remove any node other than the head
since we do not have a quick way of accessing the node in front of the one we want to remove we do not have quick access to such a predecessor node.
For such applications, it would be nice to have a way of going both directions in a linked list.
![Page 38: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/38.jpg)
Doubly Linked Lists (con….)
There is a type of linked list that allows us to go in both directions - forward and reverse in a linked list
It is the doubly linked list. Such lists allow for a great variety of quick update operations, including insertion and removal at both ends, and in the middle.
A node in a doubly linked list stores two references—a next link which points to the next node in the list, and a prev link, which points to the previous node in the list.
![Page 39: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/39.jpg)
• A doubly linked list with sentinels, header and trailer.
• An empty list would have these sentinels pointing to each other.
![Page 40: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/40.jpg)
Adding an element at the front
we can easily perform an insertion of a new element at the beginning of a doubly linked list
![Page 41: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/41.jpg)
Algorithm of Inserting a new node v at the beginning of a doubly linked list.
Variable size keeps track of the current number of elements in the list.
Note that this method works also on an empty list.
![Page 42: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/42.jpg)
Insertion in the Middle of a Doubly Linked List
They also are convenient for maintaining a list of elements while allowing for insertion and removal in the middle of the list.
Given a node v of a doubly linked list (which could be possibly the header but not the trailer)
we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute the following steps:
![Page 43: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/43.jpg)
![Page 44: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/44.jpg)
Algorithm of Inserting a new node z after a given node v in a doubly linked list.
1. make z's prev link refer to v 2. make z's next link refer to w 3. make w's prev link refer to z 4. make v's next link refer to z
![Page 45: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/45.jpg)
Deletion from double linked list Removal in the Middle of a Doubly
Linked List
(a) before the removal(b) linking out the old node(c) after the removal (and garbage collection).
![Page 46: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/46.jpg)
Removing a node v in a doubly linked list. This method works even if v is the first, last, or only nonsentinel node.
![Page 47: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/47.jpg)
Circular linked List
Same as single linked list Last node link is connected to the first. When insert/ delete at the last node Make sure to update the link field of the last node to the first node.
![Page 48: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/48.jpg)
Circular linked list with more than one node
It is convenient to make first point to the last node
![Page 49: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/49.jpg)
Insert a node at the front of CLL
![Page 50: Lecture 3:Liked list](https://reader035.fdocuments.in/reader035/viewer/2022062314/56813ac5550346895da2d8e5/html5/thumbnails/50.jpg)
Insert a node at the end of CLL