Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i am looking for help on the method AddSorted and the method Copy only public class LinkedList

i am looking for help on the method AddSorted and the method Copy only

public class LinkedList > implements ListInterface {

private LinkedListNode first = null; private LinkedListNode last = null; private int count = 0; @Override public ListInterface copy() { // TODO Auto-generated method stub return null; }

@Override public int size() { return this.count; }

@Override public boolean isEmpty() { return (this.count == 0); }

@Override public void add(I element) { LinkedListNode newNode = new LinkedListNode(element); if (this.isEmpty()) { this.first = newNode; this.last = newNode; } else { this.last.setNext(newNode); this.last = newNode; } this.count++; }

@Override public void add(I element, int index) throws IndexOutOfBoundsException { if ((this.isEmpty()) && (index == 0)) { this.add(element); } else if (index == 0) { LinkedListNode newNode = new LinkedListNode(element); newNode.setNext(this.first); this.first = newNode; this.count++; } else if (this.isValidIndex(index)) { LinkedListNode newNode = new LinkedListNode(element); LinkedListNode prevNode = null; LinkedListNode curNode = this.first; int curIndex = 0; while (curIndex != index) { prevNode = curNode; curNode = curNode.getNext(); curIndex++; } prevNode.setNext(newNode); newNode.setNext(curNode); count++; } else if (index == this.count) { this.add(element); } else { throw new IndexOutOfBoundsException("index = " + index + " is invalid"); } }

@Override public void addSorted(I element) { // TODO Auto-generated method stub }

@Override public I get(int index) throws IndexOutOfBoundsException { if (this.isValidIndex(index)) { LinkedListNode curNode = this.first; int curIndex = 0; while (curIndex != index) { curNode = curNode.getNext(); curIndex++; } return curNode.getElement(); } else { throw new IndexOutOfBoundsException("index = " + index + " is invalid"); } }

@Override public I replace(I element, int index) throws IndexOutOfBoundsException { if (this.isValidIndex(index)) { LinkedListNode curNode = this.first; int curIndex = 0; while (curIndex != index) { curNode = curNode.getNext(); curIndex++; } I oldElement = curNode.getElement(); curNode.setElement(element); return oldElement; } else { throw new IndexOutOfBoundsException("index = " + index + " is invalid"); } }

@Override public I remove(int index) throws IndexOutOfBoundsException { if (this.isValidIndex(index)) { if (index == 0) { I element = this.first.getElement(); if (this.count == 1) { this.removeAll(); } else { this.first = this.first.getNext(); this.count--; } return element; } else if (index == this.count - 1) { LinkedListNode prevNode = null; LinkedListNode curNode = this.first; int curIndex = 0; while (curIndex != index) { prevNode = curNode; curNode = curNode.getNext(); curIndex++; } this.last = prevNode; this.last.setNext(null); this.count--; return curNode.getElement(); } else { LinkedListNode prevNode = null; LinkedListNode curNode = this.first; int curIndex = 0; while (curIndex != index) { prevNode = curNode; curNode = curNode.getNext(); curIndex++; } prevNode.setNext(curNode.getNext()); this.count--; return curNode.getElement(); } } else { throw new IndexOutOfBoundsException("index = " + index + " is invalid"); } }

@Override public void removeAll() { this.first = null; this.last = null; this.count = 0; }

@Override public String toString() { String s = new String("Linked List: "); s = s + "Count = " + this.count + " "; s = s + "{"; LinkedListNode curNode = this.first; while (curNode != null) { s = s + curNode.getElement(); if (curNode != this.last) { s = s + ", "; } curNode = curNode.getNext(); } s = s + "}"; return s; }

private boolean isValidIndex(int index) { if ((index >= 0) && (index < this.count)) { return true; } else { return false; } }

}

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

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago