CSe207 Assignment
CSe207 Assignment
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;
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;
}
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.
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.
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.
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.
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.