Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please i need help fixing my remove method for my doublylinked list class /**Inserts the specified element at the correct position in the sorted list.

please i need help fixing my remove method for my doublylinked list class

/**Inserts the specified element at the correct position in the sorted list. * Notice we can insert the same element several times. * Your implementation must traverse the list only once in order to perform the insertion. * Do not implement this method using iterators. * Notice that you don't need to call any of the super class methods in order to implement this method. * @param data -the data to be added to the list * @return a reference to the current object */ public SortedDoubleLinkedList add(T data) { /*if (data == null) { return null; }

Node newnode = new Node(data, null, null); if (head == null) { // The list was empty head = tail = new Node(data, head, tail); } else { // Check if it needs to go right at the head if (comparator.compare(data, head.getData()) <= 0) { newnode.setNext(head); head = newnode; } // Check if it needs to go right at the tail else if (comparator.compare(data, tail.getData()) >= 0) { tail.setNext(newnode); tail = newnode; } else { // It needs to be inserted into the middle of the list Node next = head.getNext(); Node prev = head; while (comparator.compare(data, next.getData()) > 0) { prev = next; next = next.getNext(); } // Do the actual insertion prev.setNext(newnode); newnode.setNext(next); } } size++;*/ Node nptr = new Node(data, null, null); Node tmp, ptr; boolean ins = false; if(head == null) head = nptr; else if(comparator.compare(data, head.getData()) <= 0) { nptr.setNext(head); head.setPrev(nptr); head = nptr; } else { tmp = head; ptr = head.getNext(); while(ptr != null) { if(comparator.compare(data, head.getData()) <= 0) { tmp.setNext(nptr); nptr.setPrev(tmp); nptr.setNext(ptr); ptr.setPrev(nptr); ins = true; break; } else { tmp = ptr; ptr = ptr.getNext(); } } if(!ins) { tmp.setNext(nptr); nptr.setPrev(tmp); } } size++; return this;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

7. Discuss the implications of a skill-based pay plan for training.

Answered: 1 week ago

Question

4. Make recommendations on steps to take to melt the glass ceiling.

Answered: 1 week ago