SlideShare a Scribd company logo
i am looking for help on the method AddSorted and the method Copy only
public class LinkedList <I extends Comparable <? super I>> implements ListInterface <I> {
private LinkedListNode<I> first = null;
private LinkedListNode<I> last = null;
private int count = 0;
@Override
public ListInterface<I> 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<I> newNode = new LinkedListNode<I>(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<I> newNode = new LinkedListNode<I>(element);
newNode.setNext(this.first);
this.first = newNode;
this.count++;
} else if (this.isValidIndex(index)) {
LinkedListNode<I> newNode = new LinkedListNode<I>(element);
LinkedListNode<I> prevNode = null;
LinkedListNode<I> 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<I> 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<I> 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<I> prevNode = null;
LinkedListNode<I> 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<I> prevNode = null;
LinkedListNode<I> 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:n");
s = s + "Count = " + this.count + "n";
s = s + "{";
LinkedListNode<I> 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;
}
}
}
i am looking for help on the method AddSorted and the method Copy only.pdf

More Related Content

PDF
Given below is the completed implementation of MyLinkedList class. O.pdf
PDF
Hi,I have added the methods and main class as per your requirement.pdf
PDF
The LinkedList1 class implements a Linked list. class.pdf
PDF
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
PDF
public class MyLinkedListltE extends ComparableltEgtg.pdf
PDF
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
PDF
File LinkedList.java Defines a doubly-l.pdf
PDF
import java-util--- public class MyLinkedList{ public static void.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
Hi,I have added the methods and main class as per your requirement.pdf
The LinkedList1 class implements a Linked list. class.pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
File LinkedList.java Defines a doubly-l.pdf
import java-util--- public class MyLinkedList{ public static void.pdf

Similar to i am looking for help on the method AddSorted and the method Copy only.pdf (20)

PDF
Note- Can someone help me with the Public boolean add(E value) method.pdf
PDF
please i need help Im writing a program to test the merge sort alg.pdf
DOCX
Note- Can someone help me with the private E get(int index- int curren (1).docx
PDF
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
PDF
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
PDF
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
PDF
Describe an algorithm for concatenating two singly linked lists L and.pdf
PDF
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
PDF
hi i have to write a java program involving link lists. i have a pro.pdf
PDF
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PDF
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
PDF
Exception to indicate that Singly LinkedList is empty. .pdf
PDF
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
PDF
Fix my codeCode.pdf
PDF
Please help me to make a programming project I have to sue them today- (1).pdf
PDF
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
PDF
STAGE 2 The Methods 65 points Implement all the methods t.pdf
PDF
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
PDF
There are a couple of new methods that you will be writing for this pr.pdf
PDF
Rewrite this code so it can use a generic type instead of integers. .pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
please i need help Im writing a program to test the merge sort alg.pdf
Note- Can someone help me with the private E get(int index- int curren (1).docx
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
Exception to indicate that Singly LinkedList is empty. .pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
Fix my codeCode.pdf
Please help me to make a programming project I have to sue them today- (1).pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
There are a couple of new methods that you will be writing for this pr.pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
Ad

More from sonunotwani (20)

PDF
I arncinn curfaca All of the units are sedimentary rocks- K and J are.pdf
PDF
Hydrogen is the lightest element- If two hydrogen atoms are near each.pdf
PDF
Hydrogen bonds Helicase Topoisomerases Pseudoautosomal Regions Barr Bo.pdf
PDF
I am having problems getting the correct output- Here are the instruct (1).pdf
PDF
How would I be able to add in two columns to this dataset- 1 with the (1).pdf
PDF
I am in need of an example of how to do it using the GUI Design and im.pdf
PDF
Hyper-IgM syndrome is a rare genetic disease caused by a loss-of-funct.pdf
PDF
Huang Ltd- records their business transactions into the basic accounti.pdf
PDF
How would I change my code to get the picture below- i.pdf
PDF
How would payments to acquire debt instruments of other entities be cl.pdf
PDF
How would you determine the relative ages of igneous rocks- A- Cross-c.pdf
PDF
How would you answer these questions- 1- How would you feel if a job.pdf
PDF
How would you describe JD's product-.pdf
PDF
How would the central fight against a recessionary gap- Describe the r.pdf
PDF
How would E-coli respond to the environmental change if it has been pl.pdf
PDF
HS450-1- Assess strategic planning techniques for organizational chang.pdf
PDF
Human activity has increased habitat fragmentation and restricted wild.pdf
PDF
Suppose that the feasible region of a maximization LP problem has corn.pdf
PDF
Suppose that it is the year 1999 and the U-S- government has a budget.pdf
PDF
Suppose that past history shows that 30- of university students are sm.pdf
I arncinn curfaca All of the units are sedimentary rocks- K and J are.pdf
Hydrogen is the lightest element- If two hydrogen atoms are near each.pdf
Hydrogen bonds Helicase Topoisomerases Pseudoautosomal Regions Barr Bo.pdf
I am having problems getting the correct output- Here are the instruct (1).pdf
How would I be able to add in two columns to this dataset- 1 with the (1).pdf
I am in need of an example of how to do it using the GUI Design and im.pdf
Hyper-IgM syndrome is a rare genetic disease caused by a loss-of-funct.pdf
Huang Ltd- records their business transactions into the basic accounti.pdf
How would I change my code to get the picture below- i.pdf
How would payments to acquire debt instruments of other entities be cl.pdf
How would you determine the relative ages of igneous rocks- A- Cross-c.pdf
How would you answer these questions- 1- How would you feel if a job.pdf
How would you describe JD's product-.pdf
How would the central fight against a recessionary gap- Describe the r.pdf
How would E-coli respond to the environmental change if it has been pl.pdf
HS450-1- Assess strategic planning techniques for organizational chang.pdf
Human activity has increased habitat fragmentation and restricted wild.pdf
Suppose that the feasible region of a maximization LP problem has corn.pdf
Suppose that it is the year 1999 and the U-S- government has a budget.pdf
Suppose that past history shows that 30- of university students are sm.pdf
Ad

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Classroom Observation Tools for Teachers
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
master seminar digital applications in india
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
01-Introduction-to-Information-Management.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
RMMM.pdf make it easy to upload and study
102 student loan defaulters named and shamed – Is someone you know on the list?
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
STATICS OF THE RIGID BODIES Hibbelers.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Classroom Observation Tools for Teachers
Final Presentation General Medicine 03-08-2024.pptx
master seminar digital applications in india
O5-L3 Freight Transport Ops (International) V1.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
2.FourierTransform-ShortQuestionswithAnswers.pdf
Pharma ospi slides which help in ospi learning
Abdominal Access Techniques with Prof. Dr. R K Mishra
01-Introduction-to-Information-Management.pdf
Chinmaya Tiranga quiz Grand Finale.pdf

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

  • 1. i am looking for help on the method AddSorted and the method Copy only public class LinkedList <I extends Comparable <? super I>> implements ListInterface <I> { private LinkedListNode<I> first = null; private LinkedListNode<I> last = null; private int count = 0; @Override public ListInterface<I> 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<I> newNode = new LinkedListNode<I>(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<I> newNode = new LinkedListNode<I>(element); newNode.setNext(this.first); this.first = newNode; this.count++;
  • 2. } else if (this.isValidIndex(index)) { LinkedListNode<I> newNode = new LinkedListNode<I>(element); LinkedListNode<I> prevNode = null; LinkedListNode<I> 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<I> 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<I> curNode = this.first; int curIndex = 0;
  • 3. 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<I> prevNode = null; LinkedListNode<I> 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<I> prevNode = null; LinkedListNode<I> curNode = this.first; int curIndex = 0; while (curIndex != index) { prevNode = curNode; curNode = curNode.getNext(); curIndex++; }
  • 4. 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:n"); s = s + "Count = " + this.count + "n"; s = s + "{"; LinkedListNode<I> 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; } } }