SlideShare a Scribd company logo
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
 
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
deepak596396
 
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
 
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
Conint29
 
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
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 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
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
 
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
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

Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 

Recently uploaded (20)

Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 

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)