Question

In: Computer Science

Q3) Our implementation of a doubly list relies on two sentinel nodes, header and trailer. Re-implement...

Q3) Our implementation of a doubly list relies on two sentinel nodes, header and trailer. Re-implement the DoublyLinkedList without using these nodes.

I want the answer to be a text not on a paper please

class DoublyLinkedList:

public class DoublyLinkedList {

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 Node getPrev() {

return prev;

}

public Node getNext() {

return next;

}

public void setPrev(Node p) {

next = p;

}

public void setNext(Node n) {

next = n;

}

}

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(){

if(isEmpty())

return null;

return header.getNext().getElement();

}

public E last(){

if(isEmpty())

return null;

return trailer.getPrev().getElement();

}
}

Solutions

Expert Solution

I have modified the code not to use sentinel nodes. Since the add() method was missing, I have created a new method to add objects to the list.

public class DoublyLinkedList<E> {
   public static class Node<E> {
       private E element;
       private Node<E> prev;
       private Node<E> next;

       public Node(E e, Node<E> p, Node<E> n) {
           element = e;
           prev = p;
           next = n;
       }

       public E getElement() {
           return element;
       }

       public Node<E> getPrev() {
           return prev;
       }

       public Node<E> getNext() {
           return next;
       }

       public void setPrev(Node<E> p) {
           next = p;
       }

       public void setNext(Node<E> n) {
           next = n;
       }
   }

   private Node<E> first;
   private Node<E> last;
   private int size = 0;

   public DoublyLinkedList() {
       first = null;
       last = null;
   }

   public int size() {
       return size;
   }

   public boolean isEmpty() {
       return size == 0;
   }

   public E first() {
       if (isEmpty())
           return null;
       return first.getElement();
   }

   public E last() {
       if (isEmpty())
           return null;
       return last.getElement();
   }
  
  
   public void add(E obj) {
       Node<E> n = new Node<E>(obj, last, null);
       if(isEmpty()) {
           first = last = n;
       }
       else {
           last.next = n;
           last = n;
       }
      
       size++;
   }
}


Related Solutions

Hi! I 'm writing a code for a doubly linked list and this is the header...
Hi! I 'm writing a code for a doubly linked list and this is the header file #include<iostream> #include <string> using namespace std; struct node { int data; node *next,*prev; node(int d,node *p=0,node *n=0) { data=d; prev=p; next=n; } }; class list { node *head,*tail; public: list(); bool is_empty(); int size(); void print(); void search(); int search2(int el); void add_last(int el); void add_first(int el); bool add_pos(); bool delete_first(); bool delete_last(); void delete_pos(int pos); void delete_el(); void add_sorted(); }; i want...
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp...
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp respectively. Make sure to properly test your code on your own by creating a test driver that tests every function created in the MyString class. Deliverables: proj3-MyString.h proj3-MyString.cpp proj3-testMain.cpp Memory Requirements: Your MyString should start with 10 bytes of allocated memory and should grow in size by doubling. So, we should be able to predict the capacity of your MyString as acquiring a patten...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy node. This challenge simulates an operating system’s window manager. Requirements Write the following struct struct Window { string appname; Window *next; Window *prev; }; Create a class called WindowManager. In this class, create a private variable Window * head. This will keep track of the location of the head node. Create private variables Window * current, * dummy. current will keep track of the...
In JAVA: Create a circular doubly linked list. It need not be generic. Implement addToStart and...
In JAVA: Create a circular doubly linked list. It need not be generic. Implement addToStart and addToEnd methods, as well as printList method. Implement delete(Node n) method that deletes a node n, if n is in the linked list. Make no assumptions about n. Test your linked list.
Write a Java program to implement a double-linked list with addition of new nodes at the...
Write a Java program to implement a double-linked list with addition of new nodes at the end of the list. Add hard coded nodes 10, 20, 30, 40 and 50 in the program. Print the nodes of the doubly linked list.
A company would like to implement its inventory of smartphones as a doubly linked list, called...
A company would like to implement its inventory of smartphones as a doubly linked list, called MobileList. 1. Write a Mobile node node class, called MobileNode, to hold the following information about a smartphone: • code (as a String) • brand (as a String) • model (as a String) • price (as int) MobileNode should have constructors and methods (getters, setters, and toString()) to manage the above information as well as the link to next and previous nodes in the...
(Implement a doubly linked list) The MyLinkedList class used in Listing 24.5 is a one-way directional...
(Implement a doubly linked list) The MyLinkedList class used in Listing 24.5 is a one-way directional linked list that enables one-way traversal of the list. Modify the Node class to add the new data field name previous to refer to the previous node in the list, as follows : public class Node { E element; Node next; Node previous; public Node(E e) { element = e; } } Implement a new class named TwoWayLinkedList that uses a doubly linked list...
Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and...
Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and deletion can be than at either end. You have to write 6 methods: add front, delete front, add rear, delete rear, print forward (left to right) and print backward (right to left). After each addition or deletion dequeue elements are printed forward or backward respectively. Your main method should be as follow: public static void main(String args[]) { xxxxx dq = new xxxxx ();...
Using java: Implement a basic doubly-linked list that implements a priority system sorting the elements that...
Using java: Implement a basic doubly-linked list that implements a priority system sorting the elements that are inserted. Sort based on the speed of the warrior. Driver code: public class LinkedListDriver { public static void main(String[] args) { LinkedList list = new SortedDoublyLinkedList(); System.out.println(list); Warrior krogg = new Warrior("Krogg", 30, 50, 200); list.insert(krogg); System.out.println(list); Warrior gurkh = new Warrior("Gurkh", 40, 45, 180); list.insert(gurkh); System.out.println(list); Warrior brynn = new Warrior("Brynn", 45, 40, 190); list.insert(brynn); System.out.println(list); Warrior dolf = new Warrior("Dolf", 20,...
(15 pts) Describe an O(n)-time algorithm that partitions a doubly linked list L into two doubly...
(15 pts) Describe an O(n)-time algorithm that partitions a doubly linked list L into two doubly linked lists L1 and L2, where L1 contains the odd-number-th elements in L and L2 contains the even-number-th elements in L. Elements in both L1 and L2 should appear in the same order as they appear in L. For example, if L contains the numbers 4, 7, 9, 1, and -3, in that order, then the output L1 should contain 4, 9, and -3,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT