a. Write an array implementation of self-adjusting lists. In a self-adjusting list, all insertions are performed at the front. A self-adjusting list adds a find operation, and when an element is accessed by a find, it is moved to the front of the list without changing the relative order of the other items.
b. Write a linked list implementation of self-adjusting lists.
c. Suppose each element has a fixed probability, pi, of being accessed. Show that the elements with highest access probability are expected to be close to the front.
(a)
Program Plan :
• Import Required Library Files.
• Start the class named SelfAdjustingList.
• Start the main() method.
• Insert in the ArrayList using add() method.
• Search the element that it is in the list or not using contains() method.
• Delete the element using remove() method.
Program Code :
/*********************************************************** Program to implement Self-adjusting list. Apply search * * on the list and sets the searched element at front. * * Relative order of other items would not be changed. * **********************************************************/
//Import library files
import java.util.*;
//Start the class SelfAdjustingList
public class SelfAdjustingList
{
//start the main method
public static void main(String[] args)
{
//Declare the required elements
int i=0,x,ch;
// create an empty array list with
//an initial capacity
ArrayList
ArrayList
//Create object of scanner class
Scanner scan=new Scanner(System.in);
//start do-while loop
do
{
//Prompts the user to print a message
//of inserting a new element
System.out.println("Enter a new element:" );
//scans the x variable
x=scan.nextInt();
// call add() method to insert the element
list.add(x);
//Prompts the user to print a message
//of inserting a new element or to continue
//do-while loop
System.out.println("Do you want to insert "
+ "a new element. " );
//scans the ch variable
ch=scan.nextInt();
}while(ch!=0);
// Prints the array list
for (Integer n : list)
{
System.out.println("Number = " + n);
}
//Prompts the user to print a message
//of searching an element
System.out.println("Enter the elemnt to be "
+ "searched : " );
//scans the x variable
x=scan.nextInt();
//calls the contains method to check whether the
//element present or not
boolean retval = list.contains(x);
//If list does not contain the item then prints
//the message that list does not contain the item
if(retval==false)
{
System.out.println("Sorry!!!The element you"
+ " searched is not in the list");
}
//Start for loop to traverse the list items
for (Integer number : list)
{
//Checks for the entered element
if(number==x)
{
System.out.println("Element is "
break;
}
i++;
}
// Prints the location of the entered element
System.out.println("Location of x is : " + i);
//removes the searched element
int temp = list.remove(x);
//Inserts at front of the list
list.add(0, temp);
for (Integer number : list)
{
System.out.println("Number = " + number);
}
}
}
Sample Output :
Enter a new element:
5
Do you want to insert a new element.
1
Enter a new element:
6
Do you want to insert a new element.
1
Enter a new element:
8
Do you want to insert a new element.
1
Enter a new element:
4
Do you want to insert a new element.
1
Enter a new element:
3
Do you want to insert a new element.
0
Number = 5
Number = 6
Number = 8
Number = 4
Number = 3
Enter the element to be searched :
4
Element is contained in the list
Location of x is : 3
Number = 3
Number = 5
Number = 6
Number = 8
Number = 4
(b)
Program Plan :
• Import Required Library Files.
• Start the class named NodeList.
• Start the class named LinkedList.
• Insert in the front using pushNode() method.
• Search the element that it is in the list or not using search() method.
• Delete the element using deleteNode() method.
• Prints the elements of the list using display() method.
• Start the main() method to implement all the functioning.
Program Code :
/*********************************************************** Program to implement Self-adjusting list. Apply search * * on the list and sets the searched element at front. * * Relative order of other items would not be changed. Use * * Linked List. * **********************************************************/
//NodeList class
class NodeList
{
//Declare the variables
int data;
NodeList next;
//Constructor to initialize the
//LinkedList
NodeList(int d)
{
data = d;
next = null;
}
}
//Import required library files
import java.util.Scanner;
//Start the class Linked list
class LinkedList
{
//Headptr of the linked list
NodeList headptr;
//static variables
static int i=0,x;
//PushNode method to insert at front of
//the linked list
public void pushNode(int new_data)
{
//Create object of NodeList to push the data
NodeList newNode = new NodeList(new_data);
//Create new headptr of the data
newNode.next = headptr;
//Now headptr points to new node
headptr = newNode;
}
//Search() method to search the given element
public void search(NodeList headptr, int x)
{
//Initialize current
NodeList node = headptr;
//Search until list becomes null
while (node != null)
{
//check whether data present or not
if (node.data == x)
{
//Prompts the message to display the
//index of the data
System.out.println("Data found.");
//Call the deleteNode() method to
//delete the node
deleteNode(i);
System.out.println("Linked List After "
+ "deletion : ");
//Prints the list after deletion
display();
//Breaks the while loop
break;
}
//Points to the next pointer
node = node.next;
//increments the value of i
i++;
}
//Prompts the message if element
//does not present in the list
System.out.println("\nData not found ");
}
//Define the deleteNode method to delete the index
//of the given element
void deleteNode(int index)
{
// Checks whether linked list is empty or not
if (headptr == null)
{
//Prints the message to console
//about empty list
System.out.println("List is empty...");
return;
}
// Store the node of the headptr into swap
NodeList swap = headptr;
// If read is to be deleted
if (index == 0)
{
// Change headptr
headptr = swap.next;
return;
}
// Search the recent node to delete
//from the given node
for (int i=0; swap!=null && i
swap = swap.next;
//Delete the node
NodeList next = swap.next.next;
swap.next = next;
//Call the pushNode() method to insert the
//element at front
pushNode(x);
System.out.println("\nLinked List After "
+ "inserting the deleted element in"
//Call display() method to display the list
display();
}
// This function prints contents of linked list
//starting from the given node
public void display()
{
//Create object of NodeList class
NodeList first = headptr;
//Start loop until list becomes empty
while (first != null)
{
System.out.print(first.data+" ");
//Points to next element
first = first.next;
}
}
//Main method to run the program
public static void main(String args[])
{
//Create object of Linked List
//Create a list
LinkedList list = new LinkedList();
//create object of scanner class
Scanner scan=new Scanner(System.in);
//Declare variable
int ch;
//Start do-while loop
do
{
System.out.println("Enter the element"
+ " to insert : ");
//Take input from the user
x=scan.nextInt();
//Insert the element at the front of the
//list
list.pushNode(x);
//Asks the user to insert another element
System.out.println("Do you want to "
+ "insert more elements : ");
ch=scan.nextInt();
}while(ch!=0);
System.out.println("\nLinked List : ");
//Call display() method to print the
//contents of the list
list.display();
System.out.println("\nEnter the element "
+ "to search : ");
x=scan.nextInt();
//Call search() method to search in
//the linked list
list.search(list.headptr, x);
//Close the scanner class
scan.close();
}
}
Sample Output :
Enter the element to insert :
5
Do you want to insert more elements :
1
Enter the element to insert :
7
Do you want to insert more elements :
1
Enter the element to insert :
3
Do you want to insert more elements :
1
Enter the element to insert :
9
Do you want to insert more elements :
1
Enter the element to insert :
6
Do you want to insert more elements :
0
Linked List :
6 9 3 7 5
Enter the element to search :
7
Linked List After deletion :
6 9 3 5
Linked List After inserting the deleted element in the front :
7 6 9 3 5
(c)
Given: Probability to access each element is pi.
To prove: The elements have highest probability to be accessed is near to the front of the list.
Proof : In the above program, a node after deletion is inserted at the front of the list.
• A self-organizing list arranges the location of the deleted elements at the front.
• In simple words, the deleted node is kept in the head pointer. Means it reduces the number of comparisons.
• Means the element has the highest priority will be closer to the head of the list or to the front of the list.