Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please help me fix this add method for my soreteddoublylinked list class as it is supposed to do as it suggests but i cant get

please help me fix this add method for my soreteddoublylinked list class as it is supposed to do as it suggests but i cant get it right what am i doing wrong?

/**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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2022 Grenoble France September 19 23 2022 Proceedings Part 4 Lnai 13716

Authors: Massih-Reza Amini ,Stephane Canu ,Asja Fischer ,Tias Guns ,Petra Kralj Novak ,Grigorios Tsoumakas

1st Edition

3031264118, 978-3031264115

More Books

Students also viewed these Databases questions

Question

1. What are your creative strengths?

Answered: 1 week ago