SlideShare ist ein Scribd-Unternehmen logo
1 von 7
Downloaden Sie, um offline zu lesen
import java.util.Iterator;
import java.util.NoSuchElementException;
import lab4.util.List;
public class SinglyLinkedList<E> implements List<E> {
@SuppressWarnings("hiding")
private class SinglyLinkedListIterator<E> implements Iterator<E>{
private Node<E> nextNode;
@SuppressWarnings("unchecked")
public SinglyLinkedListIterator() {
this.nextNode = (Node<E>) header.getNext();
}
@Override
public boolean hasNext() {
return nextNode != null;
}
@Override
public E next() {
if (this.hasNext()) {
E result = this.nextNode.getElement();
this.nextNode = this.nextNode.getNext();
return result;
}
else {
throw new NoSuchElementException();
}
}
}
private static class Node<E> {
private E element;
private Node<E> next;
public Node(E element, Node<E> next) {
super();
this.element = element;
this.next = next;
}
public Node() {
super();
}
public E getElement() {
return element;
}
public void setElement(E element) {
this.element = element;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
}
private Node<E> header;
private int currentSize;
public SinglyLinkedList() {
this.header = new Node<>();
this.currentSize = 0;
}
@Override
public int size() {
return this.currentSize;
}
@Override
public boolean isEmpty() {
return this.size() == 0;
}
@Override
public boolean contains(E e) {
return this.firstIndexOf(e) >= 0;
}
@Override
public int firstIndexOf(E e) {
int i = 0;
for (Node<E> temp = this.header.getNext(); temp != null;
temp = temp.getNext(), ++i) {
if (temp.getElement().equals(e)) {
return i;
}
}
// not found
return -1;
}
@Override
public void add(E e) {
if (this.isEmpty()) {
this.header.setNext(new Node<E>(e, null));
this.currentSize++;
}
else {
Node<E>temp= this.header.getNext();
while (temp.getNext() != null) {
temp = temp.getNext();
}
Node<E> newNode = new Node<>(e, null);
temp.setNext(newNode);
this.currentSize++;
}
}
@Override
public void add(E e, int index) {
if ((index < 0) || (index > this.currentSize)) {
throw new IndexOutOfBoundsException();
}
if (index == this.currentSize) {
this.add(e);
}
else {
Node<E> temp = null;
if (index == 0) {
temp = this.header;
}
else {
temp = this.getPosition(index -1);
}
Node<E> newNode = new Node<>();
newNode.setElement(e);
newNode.setNext(temp.getNext());
temp.setNext(newNode);
this.currentSize++;
}
}
@Override
public E get(int position) {
if ((position < 0) || position >= this.currentSize) {
throw new IndexOutOfBoundsException();
}
Node<E> temp = this.getPosition(position);
return temp.getElement();
}
private Node<E> getPosition(int index){
int currentPosition=0;
Node<E> temp = this.header.getNext();
while(currentPosition != index) {
temp = temp.getNext();
currentPosition++;
}
return temp;
}
@Override
public boolean remove(int index) {
if ((index < 0) || (index >= this.currentSize)){
throw new IndexOutOfBoundsException();
}
else {
Node<E> temp = this.header;
int currentPosition =0;
Node<E> target = null;
while (currentPosition != index) {
temp = temp.getNext();
currentPosition++;
}
target = temp.getNext();
temp.setNext(target.getNext());
target.setElement(null);
target.setNext(null);
this.currentSize--;
return true;
}
}
@Override
public E set(int position, E newElement) {
if ((position < 0) || position >= this.currentSize) {
throw new IndexOutOfBoundsException();
}
Node<E> temp = this.getPosition(position);
E result = temp.getElement();
temp.setElement(newElement);
return result;
}
@Override
public void clear() {
while(!this.isEmpty()) {
this.remove(0);
}
}
@Override
public Iterator<E> iterator() {
return new SinglyLinkedListIterator<E>();
}
@Override
public int lastIndexOf(E e) {
int i = 0, result = -1;
for (Node<E> temp = this.header.getNext(); temp != null;
temp = temp.getNext(), ++i) {
if (temp.getElement().equals(e)) {
result = i;
}
}
// not found
return result;
}
@Override
public E first() {
return get(0);
}
@Override
public E last() {
return get(size() - 1);
}
@Override
public boolean remove(E e) {
int i = this.firstIndexOf(e);
if (i < 0) {
return false;
}else {
this.remove(i);
return true;
}
}
@Override
public int removeAll(E e) {
int count = 0;
while (this.remove(e)) {
count++;
}
return count;
}
/**
* TODO EXERCISE 1:
* Implement an O(n) member method called reverse()
* which reverses the elements in a list with n elements.
*
* For example, if L = {Ken, Al, Bob, Mel} then a call to L.reverse() turns L into
* L = {Mel, Bob, Al, Ken}.
*
* Note: A call to reverse() modifies the contents of the list L, it does not
* create a copy of L.
*/
public void reverse() {
/*TODO ADD YOUR CODE HERE*/
}
}
5. (20 pts) Head over to Hasprefixsumproblem. java after finishing exercise 4. The instructions
for this exercise are as follows: - Implement a method that determines if a LinkedList has an
initial sequence of nodes whose values sum to n. If so, it returns an integer corresponding to how
many elements at the beginning of the list add up to n . - The method receives as parameter a
Node that represents the head node of a singlyLinkedList, as well as an integer n denoting a
target sum to search for. - It is assumed that the List always has at least one node. - All the
elements in the List are assumed to be non-negative integers. - If no sequence of initial elements
adds up to n , the method will return a negative value, which is specified as follows: 1. The
negative of the size of the List if the sum of all elements in the List is less than n . 2. The
negative of the minimum number of elements at the beginning of the List whose sum exceeds n .
Examples (these show the lists as arrays, but you will only be given the head node of each singly
linked list) 1. A call to hasprefixsum ({ 1 , 2 , 3 , 4 , 5 } , 10 ) returns 4 since 1 + 2 + 3 + 4 = 10 ,
which is an exact sum of 10 with the first 4 elements of the list. 2. A call to hasprefixsum ({ 2 , 4
, 6 , 8 , 10 } , 10 ) returns 3 since 2 + 4 + 6 = 12 , which is larger than 10. 3. A call to
hasprefixsum ({ 1 , 2 , 3 , 4 } , 15 ) returns 4 since 1 + 2 + 3 + 4 = 10 , which is smaller than 15
and the list does not have enough elements to sum up to 15

Weitere ähnliche Inhalte

Ă„hnlich wie import java-util-Iterator- import java-util-NoSuchElementException- im.pdf

import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdfasarudheen07
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdffreddysarabia1
 
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdfCopy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdffacevenky
 
I need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfI need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfsauravmanwanicp
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfmail931892
 
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docxJAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docxGavinUJtMathist
 
public class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdfpublic class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdfaccostinternational
 
Works Applications Test - Chinmay Chauhan
Works Applications Test - Chinmay ChauhanWorks Applications Test - Chinmay Chauhan
Works Applications Test - Chinmay ChauhanChinmay Chauhan
 
STAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdfSTAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdfbabitasingh698417
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfarorastores
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfmalavshah9013
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfamazing2001
 
Help explain the code with line comments public class CompletedLis.pdf
Help explain the code with line comments public class CompletedLis.pdfHelp explain the code with line comments public class CompletedLis.pdf
Help explain the code with line comments public class CompletedLis.pdfalmonardfans
 
Please review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdfPlease review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdffathimafancyjeweller
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfravikapoorindia
 
How do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdfHow do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdffmac5
 
In the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdfIn the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdfbirajdar2
 
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfHow do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfmail931892
 
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdfSOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdfarccreation001
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfezycolours78
 

Ă„hnlich wie import java-util-Iterator- import java-util-NoSuchElementException- im.pdf (20)

import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
 
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdfCopy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
 
I need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfI need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdf
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
 
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docxJAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
 
public class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdfpublic class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
 
Works Applications Test - Chinmay Chauhan
Works Applications Test - Chinmay ChauhanWorks Applications Test - Chinmay Chauhan
Works Applications Test - Chinmay Chauhan
 
STAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdfSTAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdf
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
 
Help explain the code with line comments public class CompletedLis.pdf
Help explain the code with line comments public class CompletedLis.pdfHelp explain the code with line comments public class CompletedLis.pdf
Help explain the code with line comments public class CompletedLis.pdf
 
Please review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdfPlease review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdf
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
How do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdfHow do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdf
 
In the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdfIn the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdf
 
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfHow do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
 
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdfSOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdf
 

Mehr von Stewart29UReesa

Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfStewart29UReesa
 
Note- E0- Equilibrium demand and supply for labour - DL- Demand for la.pdf
Note- E0- Equilibrium demand and supply for labour - DL- Demand for la.pdfNote- E0- Equilibrium demand and supply for labour - DL- Demand for la.pdf
Note- E0- Equilibrium demand and supply for labour - DL- Demand for la.pdfStewart29UReesa
 
need in c language Write a function called isIsoceles that accepts thr.pdf
need in c language Write a function called isIsoceles that accepts thr.pdfneed in c language Write a function called isIsoceles that accepts thr.pdf
need in c language Write a function called isIsoceles that accepts thr.pdfStewart29UReesa
 
need in c language Write the body of a function called sumRange that a.pdf
need in c language Write the body of a function called sumRange that a.pdfneed in c language Write the body of a function called sumRange that a.pdf
need in c language Write the body of a function called sumRange that a.pdfStewart29UReesa
 
Need help with this ASAP- This is Database systems- Please draw out th.pdf
Need help with this ASAP- This is Database systems- Please draw out th.pdfNeed help with this ASAP- This is Database systems- Please draw out th.pdf
Need help with this ASAP- This is Database systems- Please draw out th.pdfStewart29UReesa
 
Nheser- Thmus dependest artigert- Thetrus insepensient andigeta- both.pdf
Nheser- Thmus dependest artigert- Thetrus insepensient andigeta- both.pdfNheser- Thmus dependest artigert- Thetrus insepensient andigeta- both.pdf
Nheser- Thmus dependest artigert- Thetrus insepensient andigeta- both.pdfStewart29UReesa
 
Nikke has just received an amended assessment from the Australian Taxa.pdf
Nikke has just received an amended assessment from the Australian Taxa.pdfNikke has just received an amended assessment from the Australian Taxa.pdf
Nikke has just received an amended assessment from the Australian Taxa.pdfStewart29UReesa
 
Orange- Below is a sequence alignment with fixed differences for speci.pdf
Orange- Below is a sequence alignment with fixed differences for speci.pdfOrange- Below is a sequence alignment with fixed differences for speci.pdf
Orange- Below is a sequence alignment with fixed differences for speci.pdfStewart29UReesa
 
Nordic Multinationals Nordic countries have small populations ( 6 mill.pdf
Nordic Multinationals Nordic countries have small populations ( 6 mill.pdfNordic Multinationals Nordic countries have small populations ( 6 mill.pdf
Nordic Multinationals Nordic countries have small populations ( 6 mill.pdfStewart29UReesa
 
Nordic countries have small populations (6 million in Denmark- 9 milli.pdf
Nordic countries have small populations (6 million in Denmark- 9 milli.pdfNordic countries have small populations (6 million in Denmark- 9 milli.pdf
Nordic countries have small populations (6 million in Denmark- 9 milli.pdfStewart29UReesa
 
Ontinostatic typotension Vazodepressor Syncope Shuazanat SyncopeMoveme.pdf
Ontinostatic typotension Vazodepressor Syncope Shuazanat SyncopeMoveme.pdfOntinostatic typotension Vazodepressor Syncope Shuazanat SyncopeMoveme.pdf
Ontinostatic typotension Vazodepressor Syncope Shuazanat SyncopeMoveme.pdfStewart29UReesa
 
One of the concerns in severe ankle sprain is that the patient has sus.pdf
One of the concerns in severe ankle sprain is that the patient has sus.pdfOne of the concerns in severe ankle sprain is that the patient has sus.pdf
One of the concerns in severe ankle sprain is that the patient has sus.pdfStewart29UReesa
 
no more info Given the system represented by the equations- x1-x22x13.pdf
no more info  Given the system represented by the equations- x1-x22x13.pdfno more info  Given the system represented by the equations- x1-x22x13.pdf
no more info Given the system represented by the equations- x1-x22x13.pdfStewart29UReesa
 
On June 13- the board of directors of Siewert Incorporated declared a.pdf
On June 13- the board of directors of Siewert Incorporated declared a.pdfOn June 13- the board of directors of Siewert Incorporated declared a.pdf
On June 13- the board of directors of Siewert Incorporated declared a.pdfStewart29UReesa
 
On May 1- 2023- Romy and Vic formed a partnership contributing assets.pdf
On May 1- 2023- Romy and Vic formed a partnership contributing assets.pdfOn May 1- 2023- Romy and Vic formed a partnership contributing assets.pdf
On May 1- 2023- Romy and Vic formed a partnership contributing assets.pdfStewart29UReesa
 
On January 1- 2020- Fisher Corporation purchased 40 percent (90-000 sh.pdf
On January 1- 2020- Fisher Corporation purchased 40 percent (90-000 sh.pdfOn January 1- 2020- Fisher Corporation purchased 40 percent (90-000 sh.pdf
On January 1- 2020- Fisher Corporation purchased 40 percent (90-000 sh.pdfStewart29UReesa
 
need asap- thank you Symbols for Relational Aleebra Expressions and Ot.pdf
need asap- thank you Symbols for Relational Aleebra Expressions and Ot.pdfneed asap- thank you Symbols for Relational Aleebra Expressions and Ot.pdf
need asap- thank you Symbols for Relational Aleebra Expressions and Ot.pdfStewart29UReesa
 
On December 30- 2020- Inge Co-'s Board of Directors declared a 10- sto.pdf
On December 30- 2020- Inge Co-'s Board of Directors declared a 10- sto.pdfOn December 30- 2020- Inge Co-'s Board of Directors declared a 10- sto.pdf
On December 30- 2020- Inge Co-'s Board of Directors declared a 10- sto.pdfStewart29UReesa
 
On December 10- YR08 the board of directors of Apple Inc- declared a c.pdf
On December 10- YR08 the board of directors of Apple Inc- declared a c.pdfOn December 10- YR08 the board of directors of Apple Inc- declared a c.pdf
On December 10- YR08 the board of directors of Apple Inc- declared a c.pdfStewart29UReesa
 
Objective- Write syntactically correct while-for loops Given a list of.pdf
Objective- Write syntactically correct while-for loops Given a list of.pdfObjective- Write syntactically correct while-for loops Given a list of.pdf
Objective- Write syntactically correct while-for loops Given a list of.pdfStewart29UReesa
 

Mehr von Stewart29UReesa (20)

Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
 
Note- E0- Equilibrium demand and supply for labour - DL- Demand for la.pdf
Note- E0- Equilibrium demand and supply for labour - DL- Demand for la.pdfNote- E0- Equilibrium demand and supply for labour - DL- Demand for la.pdf
Note- E0- Equilibrium demand and supply for labour - DL- Demand for la.pdf
 
need in c language Write a function called isIsoceles that accepts thr.pdf
need in c language Write a function called isIsoceles that accepts thr.pdfneed in c language Write a function called isIsoceles that accepts thr.pdf
need in c language Write a function called isIsoceles that accepts thr.pdf
 
need in c language Write the body of a function called sumRange that a.pdf
need in c language Write the body of a function called sumRange that a.pdfneed in c language Write the body of a function called sumRange that a.pdf
need in c language Write the body of a function called sumRange that a.pdf
 
Need help with this ASAP- This is Database systems- Please draw out th.pdf
Need help with this ASAP- This is Database systems- Please draw out th.pdfNeed help with this ASAP- This is Database systems- Please draw out th.pdf
Need help with this ASAP- This is Database systems- Please draw out th.pdf
 
Nheser- Thmus dependest artigert- Thetrus insepensient andigeta- both.pdf
Nheser- Thmus dependest artigert- Thetrus insepensient andigeta- both.pdfNheser- Thmus dependest artigert- Thetrus insepensient andigeta- both.pdf
Nheser- Thmus dependest artigert- Thetrus insepensient andigeta- both.pdf
 
Nikke has just received an amended assessment from the Australian Taxa.pdf
Nikke has just received an amended assessment from the Australian Taxa.pdfNikke has just received an amended assessment from the Australian Taxa.pdf
Nikke has just received an amended assessment from the Australian Taxa.pdf
 
Orange- Below is a sequence alignment with fixed differences for speci.pdf
Orange- Below is a sequence alignment with fixed differences for speci.pdfOrange- Below is a sequence alignment with fixed differences for speci.pdf
Orange- Below is a sequence alignment with fixed differences for speci.pdf
 
Nordic Multinationals Nordic countries have small populations ( 6 mill.pdf
Nordic Multinationals Nordic countries have small populations ( 6 mill.pdfNordic Multinationals Nordic countries have small populations ( 6 mill.pdf
Nordic Multinationals Nordic countries have small populations ( 6 mill.pdf
 
Nordic countries have small populations (6 million in Denmark- 9 milli.pdf
Nordic countries have small populations (6 million in Denmark- 9 milli.pdfNordic countries have small populations (6 million in Denmark- 9 milli.pdf
Nordic countries have small populations (6 million in Denmark- 9 milli.pdf
 
Ontinostatic typotension Vazodepressor Syncope Shuazanat SyncopeMoveme.pdf
Ontinostatic typotension Vazodepressor Syncope Shuazanat SyncopeMoveme.pdfOntinostatic typotension Vazodepressor Syncope Shuazanat SyncopeMoveme.pdf
Ontinostatic typotension Vazodepressor Syncope Shuazanat SyncopeMoveme.pdf
 
One of the concerns in severe ankle sprain is that the patient has sus.pdf
One of the concerns in severe ankle sprain is that the patient has sus.pdfOne of the concerns in severe ankle sprain is that the patient has sus.pdf
One of the concerns in severe ankle sprain is that the patient has sus.pdf
 
no more info Given the system represented by the equations- x1-x22x13.pdf
no more info  Given the system represented by the equations- x1-x22x13.pdfno more info  Given the system represented by the equations- x1-x22x13.pdf
no more info Given the system represented by the equations- x1-x22x13.pdf
 
On June 13- the board of directors of Siewert Incorporated declared a.pdf
On June 13- the board of directors of Siewert Incorporated declared a.pdfOn June 13- the board of directors of Siewert Incorporated declared a.pdf
On June 13- the board of directors of Siewert Incorporated declared a.pdf
 
On May 1- 2023- Romy and Vic formed a partnership contributing assets.pdf
On May 1- 2023- Romy and Vic formed a partnership contributing assets.pdfOn May 1- 2023- Romy and Vic formed a partnership contributing assets.pdf
On May 1- 2023- Romy and Vic formed a partnership contributing assets.pdf
 
On January 1- 2020- Fisher Corporation purchased 40 percent (90-000 sh.pdf
On January 1- 2020- Fisher Corporation purchased 40 percent (90-000 sh.pdfOn January 1- 2020- Fisher Corporation purchased 40 percent (90-000 sh.pdf
On January 1- 2020- Fisher Corporation purchased 40 percent (90-000 sh.pdf
 
need asap- thank you Symbols for Relational Aleebra Expressions and Ot.pdf
need asap- thank you Symbols for Relational Aleebra Expressions and Ot.pdfneed asap- thank you Symbols for Relational Aleebra Expressions and Ot.pdf
need asap- thank you Symbols for Relational Aleebra Expressions and Ot.pdf
 
On December 30- 2020- Inge Co-'s Board of Directors declared a 10- sto.pdf
On December 30- 2020- Inge Co-'s Board of Directors declared a 10- sto.pdfOn December 30- 2020- Inge Co-'s Board of Directors declared a 10- sto.pdf
On December 30- 2020- Inge Co-'s Board of Directors declared a 10- sto.pdf
 
On December 10- YR08 the board of directors of Apple Inc- declared a c.pdf
On December 10- YR08 the board of directors of Apple Inc- declared a c.pdfOn December 10- YR08 the board of directors of Apple Inc- declared a c.pdf
On December 10- YR08 the board of directors of Apple Inc- declared a c.pdf
 
Objective- Write syntactically correct while-for loops Given a list of.pdf
Objective- Write syntactically correct while-for loops Given a list of.pdfObjective- Write syntactically correct while-for loops Given a list of.pdf
Objective- Write syntactically correct while-for loops Given a list of.pdf
 

KĂĽrzlich hochgeladen

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 

KĂĽrzlich hochgeladen (20)

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
CĂłdigo Creativo y Arte de Software | Unidad 1
CĂłdigo Creativo y Arte de Software | Unidad 1CĂłdigo Creativo y Arte de Software | Unidad 1
CĂłdigo Creativo y Arte de Software | Unidad 1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 

import java-util-Iterator- import java-util-NoSuchElementException- im.pdf

  • 1. import java.util.Iterator; import java.util.NoSuchElementException; import lab4.util.List; public class SinglyLinkedList<E> implements List<E> { @SuppressWarnings("hiding") private class SinglyLinkedListIterator<E> implements Iterator<E>{ private Node<E> nextNode; @SuppressWarnings("unchecked") public SinglyLinkedListIterator() { this.nextNode = (Node<E>) header.getNext(); } @Override public boolean hasNext() { return nextNode != null; } @Override public E next() { if (this.hasNext()) { E result = this.nextNode.getElement(); this.nextNode = this.nextNode.getNext(); return result; } else { throw new NoSuchElementException(); } } } private static class Node<E> { private E element; private Node<E> next; public Node(E element, Node<E> next) { super(); this.element = element; this.next = next; } public Node() {
  • 2. super(); } public E getElement() { return element; } public void setElement(E element) { this.element = element; } public Node<E> getNext() { return next; } public void setNext(Node<E> next) { this.next = next; } } private Node<E> header; private int currentSize; public SinglyLinkedList() { this.header = new Node<>(); this.currentSize = 0; } @Override public int size() { return this.currentSize; } @Override public boolean isEmpty() { return this.size() == 0; } @Override public boolean contains(E e) { return this.firstIndexOf(e) >= 0; } @Override public int firstIndexOf(E e) { int i = 0;
  • 3. for (Node<E> temp = this.header.getNext(); temp != null; temp = temp.getNext(), ++i) { if (temp.getElement().equals(e)) { return i; } } // not found return -1; } @Override public void add(E e) { if (this.isEmpty()) { this.header.setNext(new Node<E>(e, null)); this.currentSize++; } else { Node<E>temp= this.header.getNext(); while (temp.getNext() != null) { temp = temp.getNext(); } Node<E> newNode = new Node<>(e, null); temp.setNext(newNode); this.currentSize++; } } @Override public void add(E e, int index) { if ((index < 0) || (index > this.currentSize)) { throw new IndexOutOfBoundsException(); } if (index == this.currentSize) { this.add(e); } else { Node<E> temp = null; if (index == 0) { temp = this.header; } else { temp = this.getPosition(index -1); } Node<E> newNode = new Node<>(); newNode.setElement(e);
  • 4. newNode.setNext(temp.getNext()); temp.setNext(newNode); this.currentSize++; } } @Override public E get(int position) { if ((position < 0) || position >= this.currentSize) { throw new IndexOutOfBoundsException(); } Node<E> temp = this.getPosition(position); return temp.getElement(); } private Node<E> getPosition(int index){ int currentPosition=0; Node<E> temp = this.header.getNext(); while(currentPosition != index) { temp = temp.getNext(); currentPosition++; } return temp; } @Override public boolean remove(int index) { if ((index < 0) || (index >= this.currentSize)){ throw new IndexOutOfBoundsException(); } else { Node<E> temp = this.header; int currentPosition =0; Node<E> target = null; while (currentPosition != index) { temp = temp.getNext(); currentPosition++; } target = temp.getNext(); temp.setNext(target.getNext()); target.setElement(null);
  • 5. target.setNext(null); this.currentSize--; return true; } } @Override public E set(int position, E newElement) { if ((position < 0) || position >= this.currentSize) { throw new IndexOutOfBoundsException(); } Node<E> temp = this.getPosition(position); E result = temp.getElement(); temp.setElement(newElement); return result; } @Override public void clear() { while(!this.isEmpty()) { this.remove(0); } } @Override public Iterator<E> iterator() { return new SinglyLinkedListIterator<E>(); } @Override public int lastIndexOf(E e) { int i = 0, result = -1; for (Node<E> temp = this.header.getNext(); temp != null; temp = temp.getNext(), ++i) { if (temp.getElement().equals(e)) { result = i; } } // not found return result; }
  • 6. @Override public E first() { return get(0); } @Override public E last() { return get(size() - 1); } @Override public boolean remove(E e) { int i = this.firstIndexOf(e); if (i < 0) { return false; }else { this.remove(i); return true; } } @Override public int removeAll(E e) { int count = 0; while (this.remove(e)) { count++; } return count; } /** * TODO EXERCISE 1: * Implement an O(n) member method called reverse() * which reverses the elements in a list with n elements. * * For example, if L = {Ken, Al, Bob, Mel} then a call to L.reverse() turns L into * L = {Mel, Bob, Al, Ken}. * * Note: A call to reverse() modifies the contents of the list L, it does not * create a copy of L. */
  • 7. public void reverse() { /*TODO ADD YOUR CODE HERE*/ } } 5. (20 pts) Head over to Hasprefixsumproblem. java after finishing exercise 4. The instructions for this exercise are as follows: - Implement a method that determines if a LinkedList has an initial sequence of nodes whose values sum to n. If so, it returns an integer corresponding to how many elements at the beginning of the list add up to n . - The method receives as parameter a Node that represents the head node of a singlyLinkedList, as well as an integer n denoting a target sum to search for. - It is assumed that the List always has at least one node. - All the elements in the List are assumed to be non-negative integers. - If no sequence of initial elements adds up to n , the method will return a negative value, which is specified as follows: 1. The negative of the size of the List if the sum of all elements in the List is less than n . 2. The negative of the minimum number of elements at the beginning of the List whose sum exceeds n . Examples (these show the lists as arrays, but you will only be given the head node of each singly linked list) 1. A call to hasprefixsum ({ 1 , 2 , 3 , 4 , 5 } , 10 ) returns 4 since 1 + 2 + 3 + 4 = 10 , which is an exact sum of 10 with the first 4 elements of the list. 2. A call to hasprefixsum ({ 2 , 4 , 6 , 8 , 10 } , 10 ) returns 3 since 2 + 4 + 6 = 12 , which is larger than 10. 3. A call to hasprefixsum ({ 1 , 2 , 3 , 4 } , 15 ) returns 4 since 1 + 2 + 3 + 4 = 10 , which is smaller than 15 and the list does not have enough elements to sum up to 15