SlideShare a Scribd company logo
1 of 10
Download to read offline
Fix my code:
Code:
/***************************************************************************
* A Linked List class with a private static inner Node class.
*
*****************************************************************************/
import java.util.*;
public class MySingleLinkedList
{
private Node head;
/**
* Constructs an empty list
*/
public MySingleLinkedList()
{
head = null;
}
/**
* Returns true if the list is empty
* @return true/false - empty/not empty
*/
public boolean isEmpty()
{
return head == null;
}
/**
* Inserts a new node at the beginning of this list.
* @param item - element to add
*
*/
public void addFirst(E item)
{
head = new Node(item, head);
}
/**
* Returns the first element in the list.
* @return the head of the list
* @exception NoSuchElementException if an empty list
*/
public E getFirst()
{
if(head == null) throw new NoSuchElementException();
return head.data;
}
/**
* Removes the first element in the list.
* @return head of the list
*
*/
public E removeFirst()
{
E tmp = getFirst();
head = head.next;
return tmp;
}
/**
* Inserts a new node to the end of this list.
* @param item - the element to add at the end of list
*/
public void addLast(E item)
{
if( head == null)
addFirst(item);
else
{
Node tmp = head;
while(tmp.next != null) tmp = tmp.next;
tmp.next = new Node(item, null);
}
}
/**
* Removes all nodes from the list.
*
*/
public void clear()
{
head = null;
}
/**
* Inserts a new node after the first appearance of the node containing the key
* @param key - the data of existing node included in the list
* @param toInsert - the data of the new node to be inserted
*/
public void insertAfter(E key, E toInsert)
{
Node tmp = head;
while(tmp != null && !tmp.data.equals(key))
tmp = tmp.next;
if(tmp != null)
tmp.next = new Node(toInsert, tmp.next);
}
/*
* ================================ The following functions need to be filled
===================================================
*
*/
/**
* Returns the last element in the list.
* @return last element in the list
* @throws NoSuchElementException if the list is empty
*/
public E getLast()
{
// TODO: please fill your code here.
Node tmp = head;
while(tmp.next != null) {
tmp = tmp.next;
}
return tmp.data;
}
/**
* Returns true if this list contains the specified element.
* @param item - element to be checked
* @return True - if contained; otherwise False
*
*/
public boolean contains(E item)
{
Node tmp = head;
while (tmp != null && !tmp.data.equals(item))
tmp = tmp.next;
if (tmp.data.equals(item) ) {
return true;
}
return false;
}
/**
* Returns the data at the specified position in the list.
* @param the position index
* @return the data contained at the given position index
* @throws IndexOutOfBoundsException if pos is larger than the size of list
*
*/
public E get(int pos)
{
Node tmp = head;
int i = 0;
while (i < pos) {
i++;
tmp = tmp.next;
}
return tmp.data;
}
/**
* Inserts a new node before the first appearance of the node containing the key.
* @param key the data of existing node included in the list
* @param toInsert the data of the new node to be inserted
*
*/
public void insertBefore(E key, E toInsert)
{
Node pre = null;
Node cur = head;
while (!cur.data.equals(key))
{
pre = cur;
cur = cur.next;
}
if (cur != null)
pre.next = new Node(toInsert, cur);
}
/**
* Removes the first occurrence of the specified element in this list.
* @param key - the element to be removed
*
*/
public void remove(E key)
{
}
/*************************** Extra Credit Exercises
*****************************************/
/***** This is for Extra Credit (5 points) *********************
/**
* Reverses the list: A->B->C to C->B->A
* @return the new head
*/
public Node reverse()
{
// TODO: please fill your code here.
return null; //remove this line after your implementation
}
/*
* ================================ The end of functions need to be filled
===================================================
*
*/
/*******************************************************
*
* The Node class
*
********************************************************/
private static class Node
{
private E data;
private Node next;
public Node(E data, Node next)
{
this.data = data;
this.next = next;
}
}
}
TESTER DO NOT CHANGE:
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
public class TestMySingleyLinkedList
{
private MySingleLinkedList empty ;
private MySingleLinkedList one ;
private MySingleLinkedList several ;
@Before
public void setUp()
{
empty = new MySingleLinkedList() ;
one = new MySingleLinkedList() ;
one.addFirst(0);
several = new MySingleLinkedList() ;
several.addFirst(2) ;
several.addFirst(1) ;
several.addFirst(0) ;
}
@Test(expected = NoSuchElementException.class)
public void testGetFirstException() {
empty.getFirst();
}
@Test
public void testGetFirst() {
assertEquals(new Integer(0),one.getFirst()) ;
assertEquals(new Integer(0),several.getFirst()) ;
}
@Test
public void testGetLast() {
assertEquals(new Integer(0),one.getLast()) ;
assertEquals(new Integer(2),several.getLast()) ;
}
@Test
public void testContain() {
assertTrue(one.contains(0)) ;
assertTrue(several.contains(0)) ;
assertTrue(several.contains(1)) ;
assertTrue(several.contains(2)) ;
}
@Test
public void testget()
{
assertEquals(new Integer(0), several.get(0));
assertEquals(new Integer(1), several.get(1));
assertEquals(new Integer(2), several.get(2));
}
@Test
public void testinsertBefore()
{
several.insertBefore(0, 20);
assertEquals(new Integer(20), several.getFirst());
}
@Test
public void testRemove()
{
assertTrue(several.contains(2));
assertTrue(several.contains(1));
several.remove(1);
several.remove(2);
assertFalse(several.contains(1));
assertFalse(several.contains(2));
}
@Test(timeout = 100)
public void testReverse()
{
several.reverse();
assertEquals(new Integer(2), several.getFirst());
assertEquals(new Integer(1), several.get(1));
assertEquals(new Integer(0), several.getLast());
}
}
TestMySingleyLinkedList [Runner: JUnit 5] (0.011 s) testGetFirst (0.000s) testget (0.000s)
testGetLast ( 0.000s) testGetFirstException ( 0.000s ) testContain (0.000 s) testReverse (0.008s) :
testinsertBefore (0.002s) : testRemove (0.001 s)

More Related Content

Similar to Fix my codeCode.pdf

Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdf
annaelctronics
 
1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf
saradashata
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
VictorXUQGloverl
 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdf
ARCHANASTOREKOTA
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
kingsandqueens3
 
we using java dynamicArray package modellinearpub imp.pdf
we using java dynamicArray    package modellinearpub   imp.pdfwe using java dynamicArray    package modellinearpub   imp.pdf
we using java dynamicArray package modellinearpub imp.pdf
adityagupta3310
 
Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docx
cgraciela1
 
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
LabProgram.javaimport java.util.NoSuchElementException;public .pdfLabProgram.javaimport java.util.NoSuchElementException;public .pdf
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
fantasiatheoutofthef
 
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
Stewart29UReesa
 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docx
VictorzH8Bondx
 
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
aptind
 
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdfNeed Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Edwardw5nSlaterl
 
Please help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfPlease help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdf
seoagam1
 
Program 8 C++newproblems.txt12333142013KristinBrewer1032823.docx
Program 8 C++newproblems.txt12333142013KristinBrewer1032823.docxProgram 8 C++newproblems.txt12333142013KristinBrewer1032823.docx
Program 8 C++newproblems.txt12333142013KristinBrewer1032823.docx
wkyra78
 
This is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdfThis is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdf
fcaindore
 
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docxAvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
rock73
 
How to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdfHow to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdf
arihantelehyb
 
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
BANSALANKIT1077
 
Complete in JavaCardApp.javapublic class CardApp { private.pdf
Complete in JavaCardApp.javapublic class CardApp {   private.pdfComplete in JavaCardApp.javapublic class CardApp {   private.pdf
Complete in JavaCardApp.javapublic class CardApp { private.pdf
MAYANKBANSAL1981
 

Similar to Fix my codeCode.pdf (20)

Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdf
 
1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdf
 
Describe an algorithm for concatenating two singly linked lists L and.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdfDescribe an algorithm for concatenating two singly linked lists L and.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdf
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
 
we using java dynamicArray package modellinearpub imp.pdf
we using java dynamicArray    package modellinearpub   imp.pdfwe using java dynamicArray    package modellinearpub   imp.pdf
we using java dynamicArray package modellinearpub imp.pdf
 
Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docx
 
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
LabProgram.javaimport java.util.NoSuchElementException;public .pdfLabProgram.javaimport java.util.NoSuchElementException;public .pdf
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
 
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- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docx
 
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
 
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdfNeed Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
 
Please help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfPlease help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdf
 
Program 8 C++newproblems.txt12333142013KristinBrewer1032823.docx
Program 8 C++newproblems.txt12333142013KristinBrewer1032823.docxProgram 8 C++newproblems.txt12333142013KristinBrewer1032823.docx
Program 8 C++newproblems.txt12333142013KristinBrewer1032823.docx
 
This is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdfThis is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdf
 
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docxAvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
 
How to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdfHow to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdf
 
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
 
Complete in JavaCardApp.javapublic class CardApp { private.pdf
Complete in JavaCardApp.javapublic class CardApp {   private.pdfComplete in JavaCardApp.javapublic class CardApp {   private.pdf
Complete in JavaCardApp.javapublic class CardApp { private.pdf
 

More from Conint29

Given BinaryNode.javapackage util;import java.util.;T.pdf
Given BinaryNode.javapackage util;import java.util.;T.pdfGiven BinaryNode.javapackage util;import java.util.;T.pdf
Given BinaryNode.javapackage util;import java.util.;T.pdf
Conint29
 
Domain Description for the Lunar Rover Back in July 30th 1971, the c.pdf
Domain Description for the Lunar Rover  Back in July 30th 1971, the c.pdfDomain Description for the Lunar Rover  Back in July 30th 1971, the c.pdf
Domain Description for the Lunar Rover Back in July 30th 1971, the c.pdf
Conint29
 
I need help with this assignment Ive gotten abit stuck with the cod.pdf
I need help with this assignment Ive gotten abit stuck with the cod.pdfI need help with this assignment Ive gotten abit stuck with the cod.pdf
I need help with this assignment Ive gotten abit stuck with the cod.pdf
Conint29
 
I am looking for some assistance with SQLite database. I have tried se.pdf
I am looking for some assistance with SQLite database. I have tried se.pdfI am looking for some assistance with SQLite database. I have tried se.pdf
I am looking for some assistance with SQLite database. I have tried se.pdf
Conint29
 
How do I declare the following constructors in my .h file Below.pdf
How do I declare the following constructors in my .h file Below.pdfHow do I declare the following constructors in my .h file Below.pdf
How do I declare the following constructors in my .h file Below.pdf
Conint29
 
Here I have a function in my code that checks the separation of airc.pdf
Here I have a function in my code that checks the separation of airc.pdfHere I have a function in my code that checks the separation of airc.pdf
Here I have a function in my code that checks the separation of airc.pdf
Conint29
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
Conint29
 
Encapsulating method details in a class [ Choose ] instance vari.pdf
Encapsulating method details in a class [ Choose ] instance vari.pdfEncapsulating method details in a class [ Choose ] instance vari.pdf
Encapsulating method details in a class [ Choose ] instance vari.pdf
Conint29
 
Hello. I need help with my assignment. Translate the ER Diagram for .pdf
Hello. I need help with my assignment. Translate the ER Diagram for .pdfHello. I need help with my assignment. Translate the ER Diagram for .pdf
Hello. I need help with my assignment. Translate the ER Diagram for .pdf
Conint29
 

More from Conint29 (13)

Given BinaryNode.javapackage util;import java.util.;T.pdf
Given BinaryNode.javapackage util;import java.util.;T.pdfGiven BinaryNode.javapackage util;import java.util.;T.pdf
Given BinaryNode.javapackage util;import java.util.;T.pdf
 
Domain Description for the Lunar Rover Back in July 30th 1971, the c.pdf
Domain Description for the Lunar Rover  Back in July 30th 1971, the c.pdfDomain Description for the Lunar Rover  Back in July 30th 1971, the c.pdf
Domain Description for the Lunar Rover Back in July 30th 1971, the c.pdf
 
Design a PIC based wearable device that keeps a tract of the activit.pdf
Design a PIC based wearable device that keeps a tract of the activit.pdfDesign a PIC based wearable device that keeps a tract of the activit.pdf
Design a PIC based wearable device that keeps a tract of the activit.pdf
 
Describe any five (5) advantages of computer networkingList any (5) ex.pdf
Describe any five (5) advantages of computer networkingList any (5) ex.pdfDescribe any five (5) advantages of computer networkingList any (5) ex.pdf
Describe any five (5) advantages of computer networkingList any (5) ex.pdf
 
I need help with this assignment Ive gotten abit stuck with the cod.pdf
I need help with this assignment Ive gotten abit stuck with the cod.pdfI need help with this assignment Ive gotten abit stuck with the cod.pdf
I need help with this assignment Ive gotten abit stuck with the cod.pdf
 
I am looking for some assistance with SQLite database. I have tried se.pdf
I am looking for some assistance with SQLite database. I have tried se.pdfI am looking for some assistance with SQLite database. I have tried se.pdf
I am looking for some assistance with SQLite database. I have tried se.pdf
 
how to Create a PDF holding all of your database data from Module 8..pdf
how to Create a PDF holding all of your database data from Module 8..pdfhow to Create a PDF holding all of your database data from Module 8..pdf
how to Create a PDF holding all of your database data from Module 8..pdf
 
How do I declare the following constructors in my .h file Below.pdf
How do I declare the following constructors in my .h file Below.pdfHow do I declare the following constructors in my .h file Below.pdf
How do I declare the following constructors in my .h file Below.pdf
 
Here I have a function in my code that checks the separation of airc.pdf
Here I have a function in my code that checks the separation of airc.pdfHere I have a function in my code that checks the separation of airc.pdf
Here I have a function in my code that checks the separation of airc.pdf
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
 
Exchange Rate Determination Theory OvershootingExchange rate over.pdf
Exchange Rate Determination Theory OvershootingExchange rate over.pdfExchange Rate Determination Theory OvershootingExchange rate over.pdf
Exchange Rate Determination Theory OvershootingExchange rate over.pdf
 
Encapsulating method details in a class [ Choose ] instance vari.pdf
Encapsulating method details in a class [ Choose ] instance vari.pdfEncapsulating method details in a class [ Choose ] instance vari.pdf
Encapsulating method details in a class [ Choose ] instance vari.pdf
 
Hello. I need help with my assignment. Translate the ER Diagram for .pdf
Hello. I need help with my assignment. Translate the ER Diagram for .pdfHello. I need help with my assignment. Translate the ER Diagram for .pdf
Hello. I need help with my assignment. Translate the ER Diagram for .pdf
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 

Fix my codeCode.pdf

  • 1. Fix my code: Code: /*************************************************************************** * A Linked List class with a private static inner Node class. * *****************************************************************************/ import java.util.*; public class MySingleLinkedList { private Node head; /** * Constructs an empty list */ public MySingleLinkedList() { head = null; } /** * Returns true if the list is empty * @return true/false - empty/not empty */ public boolean isEmpty() { return head == null; } /** * Inserts a new node at the beginning of this list. * @param item - element to add *
  • 2. */ public void addFirst(E item) { head = new Node(item, head); } /** * Returns the first element in the list. * @return the head of the list * @exception NoSuchElementException if an empty list */ public E getFirst() { if(head == null) throw new NoSuchElementException(); return head.data; } /** * Removes the first element in the list. * @return head of the list * */ public E removeFirst() { E tmp = getFirst(); head = head.next; return tmp; } /** * Inserts a new node to the end of this list. * @param item - the element to add at the end of list */ public void addLast(E item) {
  • 3. if( head == null) addFirst(item); else { Node tmp = head; while(tmp.next != null) tmp = tmp.next; tmp.next = new Node(item, null); } } /** * Removes all nodes from the list. * */ public void clear() { head = null; } /** * Inserts a new node after the first appearance of the node containing the key * @param key - the data of existing node included in the list * @param toInsert - the data of the new node to be inserted */ public void insertAfter(E key, E toInsert) { Node tmp = head; while(tmp != null && !tmp.data.equals(key)) tmp = tmp.next; if(tmp != null) tmp.next = new Node(toInsert, tmp.next); }
  • 4. /* * ================================ The following functions need to be filled =================================================== * */ /** * Returns the last element in the list. * @return last element in the list * @throws NoSuchElementException if the list is empty */ public E getLast() { // TODO: please fill your code here. Node tmp = head; while(tmp.next != null) { tmp = tmp.next; } return tmp.data; } /** * Returns true if this list contains the specified element. * @param item - element to be checked * @return True - if contained; otherwise False * */ public boolean contains(E item) {
  • 5. Node tmp = head; while (tmp != null && !tmp.data.equals(item)) tmp = tmp.next; if (tmp.data.equals(item) ) { return true; } return false; } /** * Returns the data at the specified position in the list. * @param the position index * @return the data contained at the given position index * @throws IndexOutOfBoundsException if pos is larger than the size of list * */ public E get(int pos) { Node tmp = head; int i = 0; while (i < pos) { i++; tmp = tmp.next; } return tmp.data; } /** * Inserts a new node before the first appearance of the node containing the key. * @param key the data of existing node included in the list
  • 6. * @param toInsert the data of the new node to be inserted * */ public void insertBefore(E key, E toInsert) { Node pre = null; Node cur = head; while (!cur.data.equals(key)) { pre = cur; cur = cur.next; } if (cur != null) pre.next = new Node(toInsert, cur); } /** * Removes the first occurrence of the specified element in this list. * @param key - the element to be removed * */ public void remove(E key) {
  • 7. } /*************************** Extra Credit Exercises *****************************************/ /***** This is for Extra Credit (5 points) ********************* /** * Reverses the list: A->B->C to C->B->A * @return the new head */ public Node reverse() { // TODO: please fill your code here. return null; //remove this line after your implementation } /* * ================================ The end of functions need to be filled =================================================== * */ /******************************************************* * * The Node class * ********************************************************/ private static class Node
  • 8. { private E data; private Node next; public Node(E data, Node next) { this.data = data; this.next = next; } } } TESTER DO NOT CHANGE: import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import java.util.*; public class TestMySingleyLinkedList { private MySingleLinkedList empty ; private MySingleLinkedList one ; private MySingleLinkedList several ; @Before public void setUp() { empty = new MySingleLinkedList() ; one = new MySingleLinkedList() ; one.addFirst(0); several = new MySingleLinkedList() ; several.addFirst(2) ; several.addFirst(1) ; several.addFirst(0) ; } @Test(expected = NoSuchElementException.class)
  • 9. public void testGetFirstException() { empty.getFirst(); } @Test public void testGetFirst() { assertEquals(new Integer(0),one.getFirst()) ; assertEquals(new Integer(0),several.getFirst()) ; } @Test public void testGetLast() { assertEquals(new Integer(0),one.getLast()) ; assertEquals(new Integer(2),several.getLast()) ; } @Test public void testContain() { assertTrue(one.contains(0)) ; assertTrue(several.contains(0)) ; assertTrue(several.contains(1)) ; assertTrue(several.contains(2)) ; } @Test public void testget() { assertEquals(new Integer(0), several.get(0)); assertEquals(new Integer(1), several.get(1)); assertEquals(new Integer(2), several.get(2)); } @Test public void testinsertBefore()
  • 10. { several.insertBefore(0, 20); assertEquals(new Integer(20), several.getFirst()); } @Test public void testRemove() { assertTrue(several.contains(2)); assertTrue(several.contains(1)); several.remove(1); several.remove(2); assertFalse(several.contains(1)); assertFalse(several.contains(2)); } @Test(timeout = 100) public void testReverse() { several.reverse(); assertEquals(new Integer(2), several.getFirst()); assertEquals(new Integer(1), several.get(1)); assertEquals(new Integer(0), several.getLast()); } } TestMySingleyLinkedList [Runner: JUnit 5] (0.011 s) testGetFirst (0.000s) testget (0.000s) testGetLast ( 0.000s) testGetFirstException ( 0.000s ) testContain (0.000 s) testReverse (0.008s) : testinsertBefore (0.002s) : testRemove (0.001 s)