






starter code
![Node.java TestLinkedList.java 1ep. eauthor 3 * 2 * 4 5 public class TestLinkedList 6 f 7e public static void main(String [ ]](http://img.homeworklib.com/images/ed161ce0-cfb0-49bd-b7ef-f252783c8e23.png?x-oss-process=image/resize,w_560)
To write a program using the starter code which is TestLinkedList to see if the LinkedList program has bugs. It will produce ether a pass or fail.More information is in the first two pictures.
LinkedList.java
/**
* @author someone
*
* Implements a double-linked list with four errors
*/
public class LinkedList<E>
{
// The first and last nodes in the list
private Node<E> head, tail;
// Number of items stored in the list
private int size;
// Create an empty linked list
public LinkedList()
{
// Create empty head and tail nodes to mark boundaries of
list
head = new Node<E>(null);
tail = new Node<E>(null, null, head);
head.setNext(tail);
size = 0;
}
// Return whether there are any items in the list
public boolean isEmpty()
{
return size == 0;
}
// Return the number of items in the list
public int size()
{
return size;
}
// Empties the list
public void clear()
{
head.setNext(tail);
tail.setPrev(head);
size = 0;
}
// Adds a new item to the end of the list
public void add(E item)
{
// Create a new node between tail and the node before tail
Node<E> node = new Node<E>(item, tail,
tail.getPrev());
tail.getPrev().setNext(node);
tail.setPrev(node);
// Update the size
size++;
}
// Return the item at the given index
public E get(int index)
{
// Return null if the index is out of bounds
if (index < 0 || index >= size)
{
return null;
}
Node<E> node = head.getNext();
// Traverse the list until we reach the given index
for (int i = 0; i < index; i++)
{
node = node.getNext();
}
// Return the item at the given index
return node.getItem();
}
// Removes an item from the list and returns whether an item was
removed
public boolean remove(E item)
{
Node<E> node = head.getNext();
// Traverse the list until we reach the end or find item
for (int i = 0; i < size; i++)
{
// Remove the item when we find it
if (node.getItem().equals(item))
{
// Remove any references to the removed node
node.setNext(null);
node.setPrev(null);
// Update the size
size--;
return true;
}
// Move to the next node
node = node.getNext();
}
return false;
}
// Returns the index of item, or -1 if it's not in the
list
public int indexOf(E item)
{
Node<E> node = head.getNext();
// Traverse the list looking for our item
for (int i = 0; i < size; i++)
{
if (node.getItem().equals(item)) return i;
}
return -1;
}
// Add a node to the list at the given index
public boolean insert(int index, E item)
{
// Return false if the index is out of bounds, but allow insertions
at the end
// of the list (index == size)
if (index < 0 || index > size)
{
return false;
}
Node<E> newNode = new Node<E>(item);
// Find the node before index i
Node<E> node = head;
for (int i = 0; i < size; i++)
{
node = node.getNext();
}
// Insert after the node we just found
newNode.setNext(node.getNext());
newNode.setPrev(node);
node.getNext().setPrev(newNode);
node.setNext(newNode);
// Update the size
size++;
return true;
}
// Reverse the order of the list
public void reverse()
{
// If there are fewer than 2 items, we don't need to do
anything
if (size >= 2)
{
// Two node pointers, will move through the list and swap
items
Node<E> front = head.getNext(), back = tail.getPrev();
// Go halfway through the list from each end, swapping items in
each pair of nodes
for (int i = 0; i < size / 2; i++)
{
// Swap items in front/back nodes
// ERROR: temp = back.getItem(), will swap incorrectly and
overwrite list
E temp = back.getItem();
front.setItem(back.getItem());
back.setItem(temp);
// Move front/back pointers
front = front.getNext();
back = back.getPrev();
}
}
}
}
Node.java
/**
* @author Zach
*
* Simple node class with a next and previous pointer
*/
public class Node<T>
{
// The item stored in the node
// This cannot be modified once set
private T item;
// The nodes linked before and after this node
private Node<T> next, prev;
// Create a node connected to no other nodes
public Node(T item)
{
this(item, null, null);
}
// Create a node with the give next and prev
nodes
public Node(T item, Node<T> next, Node<T>
prev)
{
this.item = item;
this.next = next;
this.prev = prev;
}
public T getItem()
{
return item;
}
public Node<T> getNext()
{
return next;
}
public Node<T> getPrev()
{
return prev;
}
public void setItem(T item)
{
this.item = item;
}
public void setNext(Node<T> next)
{
this.next = next;
}
public void setPrev(Node<T> prev)
{
this.prev = prev;
}
}
TestLinkedList.java (Starter Code which should be the only code being changed)
/**
* @author
*/
public class TestLinkedList
{
public static void main(String[] args)
{
System.out.println("Test IndexOf: " + (testIndexOf() ? "pass" :
"fail"));
System.out.println("Test Insert: " + (testInsert() ? "pass" :
"fail"));
System.out.println("Test Remove: " + (testRemove() ? "pass" :
"fail"));
System.out.println("Test Reverse: " + (testReverse() ? "pass" :
"fail"));
}
public static boolean testIndexOf()
{
return false;
}
public static boolean testInsert()
{
return false;
}
public static boolean testRemove()
{
return false;
}
public static boolean testReverse()
{
return false;
}
}
NOTE
#######
The below program works fine and tested . Please go through the function to understand the change. The test class is changed for better understanding . Please comment in case of any issue in the LinkedList functions
//######################### PGM START #########################################
class Node<T>
{
// The item stored in the node
// This cannot be modified once set
private T item;
// The nodes linked before and after this node
private Node<T> next, prev;
// Create a node connected to no other nodes
public Node(T item){
this(item, null, null);
}
// Create a node with the give next and prev
nodes
public Node(T item, Node<T> next, Node<T>
prev){
this.item = item;
this.next = next;
this.prev = prev;
}
public T getItem(){
return item;
}
public Node<T> getNext(){
return next;
}
public Node<T> getPrev(){
return prev;
}
public void setItem(T item){
this.item = item;
}
public void setNext(Node<T> next) {
this.next = next;
}
public void setPrev(Node<T> prev){
this.prev = prev;
}
}
class LinkedList<E>{
// The first and last nodes in the list
private Node<E> head, tail;
// Number of items stored in the list
private int size;
// Create an empty linked list
public LinkedList(){
// Create empty head and tail nodes
to mark boundaries of list
head = new
Node<E>(null);
tail = new Node<E>(null,
null, head);
head.setNext(tail);
size = 0;
}
// Return whether there are any items in the
list
public boolean isEmpty(){
return size == 0;
}
// Return the number of items in the list
public int size(){
return size;
}
// Empties the list
public void clear(){
head.setNext(tail);
tail.setPrev(head);
size = 0;
}
// Adds a new item to the end of the list
public void add(E item){
// Create a new node between tail
and the node before tail
Node<E> node = new
Node<E>(item, tail, tail.getPrev());
tail.getPrev().setNext(node);
tail.setPrev(node);
// Update the size
size++;
}
// Return the item at the given index
public E get(int index){
// Return null if the index is out
of bounds
if (index < 0 || index >=
size){
return
null;
}
Node<E> node = head.getNext();
// Traverse the list until we
reach the given index
for (int i = 0; i < index;
i++){
node =
node.getNext();
}
// Return the item at the given
index
return node.getItem();
}
// Removes an item from the list and returns
whether an item was removed
public boolean remove(E item){
Node<E> node =
head.getNext();
// Traverse the list until we
reach the end or find item
for (int i = 0; i < size;
i++){
// Remove the
item when we find it
if
(node.getItem().equals(item)){
// Remove any references to the removed
node
node.getNext().setPrev(node.getPrev());
node.getPrev().setNext(node.getNext());
node.setNext(null);
node.setPrev(null);
// Update the size
size--;
return true;
}
// Move to
the next node
node =
node.getNext();
}
return false;
}
// Returns the index of item, or -1 if it's not in
the list
public int indexOf(E item){
Node<E> node =
head.getNext();
// Traverse the list looking for
our item
for (int i = 0; i < size;
i++){
if
(node.getItem().equals(item)) return i;
node=node.getNext();
}
return -1;
}
// Add a node to the list at the given index
public boolean insert(int index, E item){
// Return false if the index is out
of bounds, but allow insertions at the end
// of the list (index ==
size)
if (index < 0 || index >
size){
return
false;
}
Node<E> newNode = new
Node<E>(item);
// Find the node before index
i
Node<E> node = head;
for (int i = 0; i < index;
i++){
node =
node.getNext();
}
// Insert after the node we just
found
newNode.setNext(node.getNext());
newNode.setPrev(node);
node.getNext().setPrev(newNode);
node.setNext(newNode);
// Update the size
size++;
return true;
}
// Reverse the order of the list
public void reverse(){
// If there are fewer than 2 items,
we don't need to do anything
if (size >= 2){
// Two node
pointers, will move through the list and swap items
Node<E>
front = head.getNext(), back = tail.getPrev();
// Go halfway
through the list from each end, swapping items in each pair of
nodes
for (int i = 0;
i < size / 2; i++){
// Swap items in front/back nodes
// ERROR: temp = back.getItem(), will swap
incorrectly and overwrite list
E temp = back.getItem();
back.setItem(front.getItem());
front.setItem(temp);
// Move front/back pointers
front = front.getNext();
back = back.getPrev();
}
}
}
public void display() {
Node<E> node =
head.getNext();
for(int i=0;i<size;i++) {
System.out.print(node.getItem()+" ");
node=node.getNext();
}
System.out.println();
}
}
public class TestLinkedList
{
public static void main(String[] args){
LinkedList<Integer> l1=new
LinkedList<Integer>();
l1.add(10);
l1.add(20);
l1.add(30);
System.out.println("Initial
list.......");
l1.display();
System.out.println("\nReverse of
the list.....");
l1.reverse();
l1.display();
System.out.println("\nRemoving 20
from list");
System.out.println(l1.remove(20));
l1.display();
l1.insert(1, 70);
System.out.println("\nAfter
insertion of 70 at index 1");
l1.display();
/*
System.out.println("Test IndexOf: "
+ (testIndexOf() ? "pass" : "fail"));
System.out.println("Test Insert: "
+ (testInsert() ? "pass" : "fail"));
System.out.println("Test Remove: "
+ (testRemove() ? "pass" : "fail"));
System.out.println("Test Reverse: "
+ (testReverse() ? "pass" : "fail"));
*/
}
public static boolean testIndexOf(){
return false;
}
public static boolean testInsert(){
return false;
}
public static boolean testRemove(){
return false;
}
public static boolean testReverse(){
return false;
}
}
//############################ PGM END ##############################
OUTPUT
##############
![<terminated> TestLinkedList Java Application] CAProgram Files Javaljdk-11 Initial list.... 10 20 30 Reverse of the list.....](http://img.homeworklib.com/images/b76de358-6af4-4343-8b50-beefb2ff3403.png?x-oss-process=image/resize,w_560)
starter code To write a program using the starter code which is TestLinkedList to see if...
Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...
Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...
Java Programming: The following is my code: public class KWSingleLinkedList<E> { public void setSize(int size) { this.size = size; } /** Reference to list head. */ private Node<E> head = null; /** The number of items in the list */ private int size = 0; /** Add an item to the front of the list. @param item The item to be added */ public void addFirst(E...
Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that swaps 2 nodes (no matter where they are in the list) and am having some difficulty. I can't seem to figure out why this swap method is throwing an error. Code: (SWAPPING METHOD AND TEST LINE BOLDED) package linkedlists; public class SinglyLinkedList<E> implements Cloneable { //---------------- nested Node class ---------------- /** * Node of a singly linked list, which stores a reference to its...
Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> { private static class Node<T> { public Node<T> prev, next;...
Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 { private Node head, tail; private int size; public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...
C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...
Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> { /** * Append an item to the end of the list * * @param item – item to be appended */ public void append(E item); /** * Insert an item at a specified index position * * @param item – item to be...
C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes: -head: first node in the list -tail: last node in the list #include "circDLLNode.h" template class { public: //Default constructor: creates an empty list (); //Destructor: deallocate memory ~(); ...
Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable { // ---------------- nested Node class...