Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write a method in java that goes inside: public void sortList() { } in the LinkedList class given below Given Linked List class import java.util.Iterator;

write a method in java that goes inside:

public void sortList() {

}

in the LinkedList class given below

image text in transcribed

Given Linked List class

import java.util.Iterator;

import java.util.NoSuchElementException;

public class LinkedList implements Iterable{

private Node head;

/**

* Constructs an empty list

*/

public LinkedList() {

this.head = null;

}

/**

* Returns size of the list

*/

public int size() {

int size = 0;

if(head != null) {

size ++;

Node current = head;

while(current.next!= null) {

size ++;

current = current.next;

}

}

return size;

}

/**

* Returns true if the list is empty

*

*/

public boolean isEmpty() {

return head == null;

}

/**

* Removes all nodes from the list.

*

*/

public void clear() {

this.head = null;

}

/**

* Returns the data at the specified position in the list.

*/

public T get(int pos) {

if(head == null) throw new IndexOutOfBoundsException();

int currentIndex = 0;

Node current = head;

while(currentIndex

currentIndex ++;

current = current.next;

}

if(current != null) {

return current.data;

} else{

throw new IndexOutOfBoundsException();

}

}

/**

* Returns true if this list contains the specified element.

*

*/

public boolean contains(T x) {

for(T temp: this) {

if(temp.equals(x)) {

return true;

}

}

return false;

}

/**

* Returns the first element in the list.

*

*/

public T getFirst() {

if(head == null) throw new NoSuchElementException();

return head.data;

}

/**

* Inserts a new node at the beginning of this list.

*

*/

public void addToFirst(T data) {

this.head = new Node(data, head);

}

/**

* Removes the first element in the list.

*

*/

public T removeFirst() {

T temp = getFirst();

head = head.next;

return temp;

}

/**

* Returns the last element in the list.

*

*/

public T getLast() {

if(head == null) throw new NoSuchElementException();

Node current = head;

while(current.next != null) {

current = current.next;

}

return current.data;

}

/**

* Inserts a new node to the end of this list.

*

*/

public void addToLast(T data) {

if(head == null) {

head = new Node(data, head);

} else{

Node current = head;

while(current.next != null) {

current = current.next;

}

current.next = new Node(data, null);

}

}

/**

* Removes the first occurrence of the specified element in this list.

*

*/

public void remove(T key) {

if(head == null) throw new NoSuchElementException();

if(head.data.equals(key)) {

head = head.next;

return;

}

Node prev = null;

Node current = head;

while(current != null && !current.data.equals(key)) {

prev = current;

current = current.next;

}

if(current != null) {

prev.next = current.next;

} else{

throw new NoSuchElementException();

}

}

/**

* The Node class

*/

private static class Node {

private T data;

private Node next;

public Node(T data, Node next) {

this.data = data;

this.next = next;

}

}

/**

*

* The Iterator class

*/

@Override

public Iterator iterator() {

return new LinkedListIterator();

}

private class LinkedListIterator implements Iterator {

private Node nextNode;

public LinkedListIterator() {

this.nextNode = head;

}

@Override

public boolean hasNext() {

return nextNode != null;

}

@Override

public T next() {

if(!hasNext()) throw new NoSuchElementException();

T d = nextNode.data;

nextNode = nextNode.next;

return d;

}

}

/**

* Returns a string representation

*

*/

@Override

public String toString() {

StringBuffer result = new StringBuffer();

for(Object x : this)

result.append(x + " ");

return result.toString();

}

/*

* *************************

* Homework Problems

*

* *************************

*/

/**

* Inserts a new node at pos.

*/

public void insertAt(int pos, T toInsert) {

}

/**

* Removes the item at position pos in this list.

*

*/

public void removeAt(int pos) {

}

/**

* Inserts a new node after a node containing the key.

*

*/

public void insertAfter(T key, T toInsert) {

}

/**

* Inserts a new node before a node containing the key.

*/

public void insertBefore(T key, T toInsert) {

}

/**

* sort the list

*/

public void sortList() {

}

/**

* removes duplicate elements from the list

*/

public void removeDuplicates() {

}

}

Problem:2 Implement sorting and duplicate removal operations on the linked list: sortList0: sort the list in ascending order of the keys Example: 1. List: 467259 sortListO List: 2 45679

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

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

Students also viewed these Databases questions

Question

Choosing Your Topic Researching the Topic

Answered: 1 week ago

Question

The Power of Public Speaking Clarifying the

Answered: 1 week ago