The document provides code for a linked list class with methods to add, remove, get, and traverse nodes. It contains comments describing each method and placeholders for some methods to be implemented, including getting the last node, checking if a value is contained, getting a node by position, inserting before a key, and reversing the list. It also includes test cases to validate the linked list functionality.
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)