0% found this document useful (0 votes)
3 views

CSe207 Assignment

Uploaded by

skfeath.coding
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CSe207 Assignment

Uploaded by

skfeath.coding
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Introduction

This project involves a Self-Organizing Doubly Linked List, which is a type of linked list that rearranges
itself to make frequently accessed elements easier to reach. The list automatically moves accessed nodes
to the front to speed up future access. The project includes Java classes and methods to add nodes,
access and reorder them, and display the list in both forward and reverse order.

ADTs
The project utilizes the following Abstract Data Types (ADTs):

Node Class : Represents an individual node in the doubly linked list. Each node has an integer data field
and pointers to both the previous and next nodes in the list.

Details:
• Purpose: Defines a node in the doubly linked list.
• Fields:
o data: Holds the integer value for the node.
o prev: A reference to the previous node in the list.
o next: A reference to the next node in the list.

• Constructor: Initializes the node with a given data value and sets both prev and next to null.

class Node {
int data;
Node prev;
Node next;

public Node(int data) {


this.data = data;
this.prev = null;
this.next = null;
}
}
SelfOrganizingDoublyLinkedList Class: Represents the doubly linked list itself. It provides methods to add
new nodes, access nodes (reordering them to the front), and display the list in both forward and reverse
directions.

Details:
• Purpose: Manages the doubly linked list and provides operations for adding nodes, accessing
nodes, and displaying the list.
• Fields:
o head: A reference to the first node in the list.
o tail: A reference to the last node in the list.

class SelfOrganizingDoublyLinkedList {
private Node head;
private Node tail;

public SelfOrganizingDoublyLinkedList() {
head = null;
tail = null;
}

public void add(int data) {


Node newNode = new Node(data);
if (head == null) {
head = tail = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
}
Functions
Add(int data) :
• Purpose: Adds a new node with the given data to the end of the list.
• Steps :
o Create a new Node with the specified data.
o If the list is empty, set both head and tail to this new node.
o Otherwise, link the new node to the end of the list by updating tail.next and tail,
and adjust pointers accordingly.

public void Add(int data) {


Node newNode = new Node(data);
if (head == null) {
head = tail = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
}
MoveToFront(Node node):
• Purpose: Moves the specified node to the front of the list.
• Steps :
o If the node is already at the front, do nothing.
o Detach the node by updating the prev and next pointers of surrounding nodes.
o If the node is the tail, update tail to be the previous node.
o Insert the node at the front of the list by updating the head and its prev pointer.

private void MoveToFront(Node node) {


if (node == head) return;

if (node.prev != null) {
node.prev.next = node.next;
}
if (node.next != null) {
node.next.prev = node.prev;
}

if (node == tail) {
tail = node.prev;
}

node.next = head;
head.prev = node;
node.prev = null;
head = node;
}
Access(int data):
• Purpose: Finds a node with the specified data and moves it to the front.
• Steps:
o Traverse the list to find the node with the given data.
o If found, call moveToFront to reorder the node.
o Return the found node, or null if not found.

public Node Access(int data) {


Node current = head;
while (current != null) {
if (current.data == data) {
moveToFront(current);
return current;
}
current = current.next;
}
return null;
}
Display():
• Purpose: Prints all the nodes from the head to the tail.
• Steps:
o Start from the head and print the data of each node as you traverse to the end.
public void Display() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
DisplayReverse():
• Purpose: Prints all the nodes from the tail to the head.
• Steps:
o Start from the tail and print the data of each node as you traverse backward to
the beginning.
public void DisplayReverse() {
Node current = tail;
while (current != null) {
System.out.print(current.data + " ");
current = current.prev;
}
System.out.println();
}
}
Main Class:
• Purpose: Demonstrates the use of the SelfOrganizingDoublyLinkedList class.
• Steps:
o Create an instance of SelfOrganizingDoublyLinkedList.
o Add several nodes with different data values.
o Display the list, access certain nodes (which moves them to the front), and
display the list again to show the effect.
o Display the list in reverse order to show the complete list from end to start.

Implementation Overview
The code creates a Self-Organizing Doubly Linked List in Java. It uses a Node class to store each
element’s value and links to the previous and next nodes. The SelfOrganizingDoublyLinkedList
class manages the list, allowing you to add new nodes, move accessed nodes to the front, and
show the list from start to end or end to start. When a node is accessed, it’s moved to the front
to make it quicker to reach next time. The Main class shows how to use these features by
adding nodes, accessing them, and displaying the list in different orders.

Why Use a Self-Organizing Doubly Linked List?


A Self-Organizing Doubly Linked List is helpful when certain elements are accessed more often
than others. It rearranges itself so that frequently accessed elements are quicker to reach,
which can improve performance in systems where some data is used more than others.

Advantages
1. Faster Access: Frequently accessed elements are moved to the front, making them
quicker to find next time.
2. Two-Way Navigation: The list can be easily traversed in both directions, allowing you to
move forwards or backwards through the list.
3. Easy Reordering: The list can be reordered efficiently by simply adjusting a few pointers.
4. Flexible Memory Use: Unlike arrays, which need a block of memory, linked lists can use
scattered memory, which can be more efficient.
Disadvantages
1. More Complex: The self-organizing feature adds extra complexity, making the list harder
to implement and manage compared to a simple linked list.
2. Reordering Overhead: Moving elements to the front every time they are accessed adds
some extra processing.
3. Extra Memory: Each node uses more memory because it stores pointers to both the
next and previous nodes.
4. Limited Benefit in Some Cases: If all elements are accessed equally, the self-organizing
feature might not provide much benefit.
5.

Time and Space Analysis


Time Complexity:
• Accessing an Element: In the worst case, accessing an element can take O(n) time if the
element is at the end of the list. However, since frequently accessed elements are
moved to the front, the average access time for these elements can be significantly
reduced.
• Adding a New Node: Adding a node to the end of the list takes O(1) time, as it involves
only a few pointer updates.
• Moving to the Front: Moving a node to the front of the list generally takes O(1) time, as
it involves detaching the node from its current position and updating the necessary
pointers. However, if the node is far from the front, it may take O(n) time to find it.

Space Complexity:
• Memory Use: The space required for storing n elements is O(n), where each node
contains additional pointers for the previous and next nodes.
• Additional Space: Beyond the list itself, no extra space is needed, so the additional space
complexity is O(1).
Limitations

1. Best for Uneven Access Patterns: The list is most effective when some elements are accessed
much more frequently than others. In cases where all elements are accessed with similar
frequency, the self-organizing feature provides little to no benefit.

2. Reordering Costs: Moving nodes to the front every time they are accessed can introduce
some processing overhead, especially in scenarios where reordering is frequent but doesn't
yield significant performance improvements.

3. Increased Memory Usage: Each node in the doubly linked list contains additional pointers to
the previous and next nodes, leading to higher memory usage compared to a singly linked list or
an array.

Real-Life Implementation Ideas


• In cache systems, where recently or frequently accessed items are kept ready for quick
retrieval, a Self-Organizing Doubly Linked List can improve performance by keeping the
most accessed items at the front of the list, reducing the time needed to locate them.
• The list can be used to maintain indexes in databases, ensuring that frequently searched
data is kept at the front, making search operations faster and more efficient.
• In file systems, frequently accessed files or directories can be moved to the front of the
list, allowing quicker access. This can be particularly useful in systems where certain files
are opened or modified regularly.
• In real-time systems, such as network monitoring or event logging, the list can prioritize
frequently accessed or recently relevant data, making it easier to respond to critical
events quickly.
• Search engines can use this data structure to manage search terms or results, keeping
the most popular or relevant items at the front, thereby reducing the time it takes to
return results for common queries.

Conclusion
The Self-Organizing Doubly Linked List is a smart way to improve access times by rearranging
itself based on what you use most often. Although it adds some complexity and uses more
memory, its ability to make frequently accessed items easier to find can be very useful in many
situations.

You might also like