Question: public class DoublyLinkedList { private Node header; private Node trailer; private int size=0; public DoublyLinkedList() { header=new Node(null,null,null); trailer=new Node(null,header, null); header.setNext(trailer); } public int

public class DoublyLinkedList{ private Node header; private Node trailer; private int size=0; public DoublyLinkedList() { header=new Node(null,null,null); trailer=new Node(null,header, null); header.setNext(trailer); } public int size() { return size; } public boolean isEmpty() { return size==0; } public E first() // to return the first element { if (isEmpty()) return null; return header.getNext().getElement(); } public E last() // to return the last element { if (isEmpty()) return null; return trailer.getPrev().getElement(); } public void addBetween(E e, Node predecessor,Node successor) { Node newest=new Node(e,predecessor, successor); predecessor.setNext(newest); successor.setPrev(newest); size++; } public E remove(Node e) { if(size==0) return null; Node predecessor=e.getPrev(); Node successor=e.getNext(); predecessor.setNext(successor); successor.setPrev(predecessor); size--; return e.getElement(); } public void addFirst(E e) { addBetween(e,header,header.getNext()); } public void addLast(E e) { addBetween(e,trailer.getPrev(),trailer); } public E removeFirst() { if(isEmpty()) return null; return remove(header.getNext()); } public E removeLast() { if(isEmpty()) return null; return remove(trailer.getPrev()); } public void displayForward() { for (Node t=header.getNext();t!=trailer;t=t.getNext()) System.out.println(t.getElement()); } public void displayBackward() { for (Node t=trailer.getPrev();t!=header;t=t.getPrev()) System.out.println(t.getElement()); } public void concat(DoublyLinkedList newList) { trailer.getPrev().setNext(newList.header.getNext()); newList.header.getNext().setPrev(trailer.getNext()); trailer=newList.trailer; } public void reverse() { Node i=header.getNext(); Node j=trailer.getPrev(); while (i.getNext()!=j && i!=j) { E t=i.getElement(); i.setElement(j.getElement()); j.setElement(t); i=i.getNext(); j=j.getPrev(); } } public static class Node { private E element; private Node prev; private Node next; public Node(E e,Node p, Node n) { element=e; prev=p; next=n; } public E getElement() { return element; } public void setElement(E e) { element=e; } public Node getPrev() { return prev; } public Node getNext() { return next; } public void setPrev(Node n) { prev=n; } public void setNext(Node n) { next=n; } } }
As presented in the lab, linked lists must be searched sequentially. For large lists, this can result in poor performance. A common technique for improving list-searching performance is to create and maintain an index to the list. An index is a set of references to key places in the list. For example, an application that searches a large list of words could improve performance by creating an index with 26 entries - one for each letter of the alphabet. A earch operation fora last ane beginning it lddn t seach the index to determine where the Y entries begin, then "jump into" the list at that point and search linearly until the desired name is found. This would be much faster than searching the linked list from the beginning. Use the List class in lab 8 as the basis of an IndexedList class Write a program that demonstrates the operation of indexed lists. Be sure to include methods displayIndexedList, insertinIndexedList, searchIndexedList and deleteFromIndexedList
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
