Day 6 - Linkedlists
Day 6 - Linkedlists
and Algorithms
LinkedList
Objectives singly, doubly LinkedList
Linked List
A linked list is a linear data structure that includes a series of connected nodes.
Here, each node stores the data and the address of the next node (a reference
to the next node).
For example,
This representation of a linked list depicts that each node consists of two fields.
The first field consists of data, and the second field consists of
pointers/references that point to another node.
Here, the start pointer/reference stores the address of the first node, and at the
end, there is a null pointer that states the end of the Linked List.
Reference
A reference is a number that refers to an object. It’s the object’s address in the
computer’s memory, but you don’t need to know its value; you just treat it as a
magic number that tells you where the object is.
Relationship, not position
In an array each item occupies a particular position. This position can be
directly accessed using an index number. It’s like a row of houses: You can find a
particular house using its address.
In a list the only way to find a particular element is to follow along the chain of
elements. It’s more like human relations.
Maybe you ask Harry where Bob is. Harry doesn’t know, but he thinks Jane might know, so you go and ask
Jane. Jane saw Bob leave the office with Sally, so you call Sally’s cell phone. She dropped Bob off at
Peter’s office, so…but you get the idea. You can’t access a data item directly; you must use relationships
between the items to locate it. You start with the first item, go to the second, then the third, until you find
what you’re looking for.
You have to start somewhere, so we give the address of the first node a special
name called HEAD. Also, the last node in the linked list can be identified
because its next portion points to NULL.
Java LinkedList
The LinkedList class of the Java collections framework provides the functionality
of the linked list data structure
Contd
Each element in a linked list is known as a node. It consists of 3 fields:
Prev - stores an address of the previous element in the list. It is null for the first
element
Next - stores an address of the next element in the list. It is null for the last
element
Data - stores the actual data
LinkedList<String> animals = new LinkedList<>();
animals.add("Dog");
animals.add("Cat");
animals.add("Cow");
Operations in java Linkedlist
add()
get()
set()
remove()
A data item
An address of another node
A simple Node class
class Node{
int value; // integer data field
Node next; // references to next node
Node(int data){
value = data;
next = null;
}
Try this
class LinkedLists { public static void main(String[] args) {
Node head; LinkedLists numlist =
new LinkedLists();
static class Node{ numlist.head = new Node(1);
int value; Node node2 = new Node(2);
Node next; Node node3 = new Node(3);
numlist.head.next = node2;
Node(int data){ node2.next = node3;
value =
data; //printing node-value
next = while(numlist.head != null) {
Syspr(numlist.head.value + " ");
null;
numlist.head =
}
}
numlist.head.next;
}
}
}
In just a few steps, we have created a simple linked list with three nodes.
The power of a linked list comes from the ability to break the chain and rejoin it.
E.g. if you wanted to put an element 4 between 1 and 2, the steps would be:
Doing something similar in an array would have required shifting the positions
of all the subsequent elements.
Types of Linkedlists
Singly
Doubly
Circular
Singly LinkedList – unidrxnal traversal
each element has a next pointer but not a prev pointer
The last node in the list has a null next link
Each node has data and an address field that contains a reference to the next
node.
Circular singly LinkedList
The end node is connected to the first node of the liked list.
Doubly linkedlist – bidrxnal traversal
Each node contains one data and two references, that references to the prev
and next node
Circular doubly linked list
The last node is connected to the first node and the first node is connected to
the last node of the linked list
Array vs Linkedlist
Array LinkedList
Size of an array is fixed Size of a linked list is not fixed
Memory allocation is static Memory allocation is dynamic
We can access an element randomly We can’t access a node randomly (it’s
Stored in consecutive memory location sequential)
Insertion/deletion ---inefficient Stored in non-consecutive memory
location
Insertion/deletion ---efficient
Array representation in RAM
Linkedlist representation in RAM
Areas where linked lists are used
To implement other complex data structures such as stacks, queues, and hash
tables.
Dynamic memory allocation and deallocation
In file systems to manage the allocation of disk blocks and to maintain the
directory structure.
Linked lists are used in networking for implementing protocols like TCP/IP,
where they can be used to manage packet queues.
Linked lists can be used to implement task scheduling algorithms, such as
priority scheduling or round-robin scheduling.
Linked lists can be used to implement undo functionality in applications,
allowing users to reverse actions they have taken.
Linked lists can be used in simulations to represent queues of events or entities.
Circular Lists in Music Players: In music players, circular linked lists can be used
to implement playlists where the last song is followed by the first song, creating
a continuous loop of playback.
Circular linked lists can be used to implement circular iterators in programming
languages or data structures, allowing for efficient traversal through a looped
set of elements.
Circular linked lists can be used to manage and allocate resources in systems
where resources need to be cyclically distributed or accessed.
Game Object Pooling: Circular linked lists can be utilized to implement object
pooling systems, where game objects are dynamically reused to improve
performance and memory usage. Objects that are deactivated or destroyed can
be added back to the pool and reused in a circular manner, maintaining a
constant set of available objects.
Animation Systems: Circular linked lists can be employed in animation systems
to manage sequences of animation states or frames. Each node in the list can
represent an animation state or frame, and transitions between animations can
be facilitated by traversing the circular list.
Sound Management: Circular linked lists can be employed to manage sound
effects or music tracks in games. Each node in the list can represent a sound
effect or music track, and audio playback can proceed in a circular manner,
looping back to the beginning of the list when the end is reached.
Undo Functionality: In applications such as text editors, where users may want
to undo or redo actions, doubly linked lists can be used to maintain a sequence
of states. Each state change can be represented by a node in the list, allowing
for efficient traversal in both directions during undo and redo operations.
Image Viewer: Doubly linked lists can be used to implement an image viewer
application where users can navigate through a collection of images. Each image
can be represented by a node in the list, and users can traverse the list forwards
and backwards to view images in sequence.
Browser History: Web browsers often use doubly linked lists to implement the
navigation history. Each visited web page is represented by a node in the list,
and users can navigate forward and backward through their browsing history
using the browser's back and forward buttons.
References