Question

In: Computer Science

Please solve this problem in java. (simple linked list) public class MyLinkedList implements MiniList{ /* Private...

Please solve this problem in java. (simple linked list)

public class MyLinkedList implements MiniList{
/* Private member variables that you need to declare:
** The head pointer
** The tail pointer
*/
   private Node head;
   private Node tail;
  

  
public class Node {
// declare member variables (data and next)
   Integer data;
   Node next;

// finish these constructors
   public Node(int data, Node next) {
      
       this.data=data;
       this.next=next;
   }
public Node(int data) {
   this.data = data;
   this.next = null;
} // HINT: use this() with next = null
}


// Initialize the linked list (set head and tail pointers)
public MyLinkedList() {
head = null;
tail = null;
}

@Override
public boolean add(Integer item) {
Node newNode = new Node(item);
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
return true;
}

@Override
public void add(int index, Integer element) {
   Node newNode = new Node(element);
if(index == 0) {

newNode.next = head;
head=newNode;
} else {
Node curr = head;
for(int i=0;i curr = curr.next;
}
if(curr.next!=null) {
  
newNode.next = curr.next;
curr.next = newNode;
  
} else {
tail.next = newNode;
  
tail = newNode;
}
}
}

@Override
public Integer remove() {
Node current = head;
Integer ele = null;
ele = head.data;
head = head.next;

return ele;
}

@Override
public Integer remove(int index) {
Node current = head;
Integer ele = null;
int count = 0;
if (head == null) {
return null;
}
while (current != null) {
if (index - 1 == count && current.next != null) {

ele = current.next.data;
if (current.next.next == null) {
// t.next = current.next;
tail = current;
}
current.next = current.next.next;
//current.next.next = null;
break;
}
current = current.next;
count++;
}
return ele;
}

@Override
public boolean remove(Integer item) {
Node current = head;

if (head == null) {
return false;
}
if (current.data == item) {
head = current.next;
return true;
}
while (current != null) {
if (current.next != null && current.next.data == item) {
if (current.next.next == null) {
// t.next = current.next;
tail = current;
}
current.next = current.next.next;
//current.next.next = null;
break;
}
current = current.next;
}
return true;
}

@Override
public void clear() {
head = null;
}

@Override
public boolean contains(Integer item) {

Node current = head;

if (head == null) {
return false;
}
while (current != null) {
if (current.data == item) {
return true;
}
current = current.next;
}
return false;
}

@Override
public Integer get(int index) {
Node current = head;
int count = 0;
if (head == null) {
return null;
}
while (current != null) {
if (index == count) {
return current.data;
}
current = current.next;
count++;

}
return null;
}

@Override
public int indexOf(Integer item) {
Node current = head;
int count = 0;
if (head == null) {
return -1;
}
while (current != null) {
if (current.data == item) {
return count;
}
current = current.next;
count++;
}
return -1;
}

@Override
public boolean isEmpty() {
if (head == null) {
return true;
}
return false;
}

@Override
public int size() {
Node current = head;
int count = 0;
if (head == null) {
return 0;
}
while (current != null) {
current = current.next;
count++;
}
return count;
}
  
// Uncomment when ready to test
@Override
public String toString() {
String ret = "";
Node curr = head;
while (curr != null) {
ret += curr.data + " ";
curr = curr.next;
}
return ret;
}

}
------------------------------------------------------------------------------------------------------------

public class LLTest {
public static void main(String args[]) {
MyLinkedList ll = new MyLinkedList();

// Testing add methods
System.out.println("Testing Add");
ll.add(40);
ll.add(10);
ll.add(20);
ll.add(1, 30);
ll.add(3, 100);
ll.add(65);
ll.add(2);
System.out.println("Expected: 40 30 10 100 20 65 2");
System.out.println("Actual: " + ll);
System.out.println();

// Testing remove methods
System.out.println("Testing Remove");
ll.remove();
ll.remove(2);
ll.remove(3);
ll.remove((Integer)2);
System.out.println("Expected: 30 10 20");
System.out.println("Actual: " + ll);
System.out.println("Size should be 3 -> " + ll.size());
System.out.println();

// Testing Contains
System.out.println("Testing Contains");
ll.add(2); // to make it a little bigger
System.out.println("Should be true -> " + ll.contains(2));
System.out.println("Should be false -> " + ll.contains(65));
System.out.println("Should be true -> " + ll.contains(30));
System.out.println();

// Testing Get
System.out.println("Testing Get");
System.out.println("Actual list: " + ll);
System.out.print("List using get: ");
for (int i = 0; i < ll.size(); i++) {
System.out.print(ll.get(i) + " ");
}
System.out.println("\n");

// Testing indexOf
System.out.println("Testing indexOf");
System.out.println("Should be 2 -> " + ll.indexOf(20));
System.out.println("Should be 3 -> " + ll.indexOf(2));
System.out.println("Should be -1 -> " + ll.indexOf(65));

// You can write more tests (these are not all encompassing)
// When should these methods fail/throw exceptions?
// Have all of the edge cases been tested?
// Not quite all of the methods have been tested here (e.g. clear())

}
}
------------------------------------------------------------------------------------------------------------

public interface MiniList {
public boolean add(E item);
public void add(int index, E element);

public E remove();
public E remove(int index);
public boolean remove(E item);

public void clear();

public boolean contains(E item);

public boolean equals(Object o);

public E get(int index);
public int indexOf(E item);

public boolean isEmpty();

public int size();
}

Solutions

Expert Solution

Working code implemented in Java and appropriate comments provided for better understanding.

Here I am attaching code for all files:

MyLinkedList.java:

public class MyLinkedList implements MiniList<Integer>{
/* Private member variables that you need to declare:
** The head pointer
** The tail pointer
*/
   Node m_pHead;
   Node m_pTail;
   int m_iSize;

public class Node {
// declare member variables (data and next)
   int data;
   Node pNext;

// finish these constructors
public Node(int data, Node pNext) {
   this.data = data;
   this.pNext = pNext;
}
public Node(int data) {
   this.data = data;
   this.pNext = null;
} // HINT: use this() with next = null
}

// Initialize the linked list (set head and tail pointers)
public MyLinkedList() {
   m_pHead = null;
   m_pTail = null;
   m_iSize = 0;
}

@Override
public boolean add(Integer item) {
   Node newNode = new Node(item);
  
   if (null == m_pHead)
       m_pHead = newNode;
   else
       m_pTail.pNext = newNode;
      
m_pTail = newNode;
  
m_iSize++;
return true;
}

@Override
public void add(int index, Integer element) {
   Node pCur = m_pHead;
   Node newNode = new Node(element);
  
   if (index == 0) {
       newNode.pNext = pCur.pNext;
       pCur.pNext = null;
       m_pHead = newNode;
   }
  
   for (int i = 1; i < index; i++) {
       pCur = pCur.pNext;
   }
  
   newNode.pNext = pCur.pNext;
   pCur.pNext = newNode;
  
   m_iSize++;
}

@Override
public Integer remove() {
Node pCur = m_pHead;
m_pHead = pCur.pNext;
pCur.pNext = null;
  
m_iSize--;
return pCur.data;
}

@Override
public Integer remove(int index) {
   Node pPrev = m_pHead;
   Node pCur = m_pHead.pNext;
  
   if (null == m_pHead)
       return null;
   // head case
   else if (index == 0) {
   m_pHead = m_pHead.pNext;
   pPrev.pNext = null;
  
   m_iSize--;
   return pCur.data;
}
  
   // inside case
if (index > 0 && index < m_iSize - 1) {
   for (int i = 1; i < index; i++) {
       pPrev = pCur;
       pCur = pCur.pNext;
   }
  
   pPrev.pNext = pCur.pNext;
   pCur.pNext = null;
  
   m_iSize--;
   return pCur.data;
}
  
// tail case
if (index == m_iSize - 1) {
   pPrev.pNext = null;
  
   m_iSize--;
   return pCur.data;
}
  
return null;
}

@Override
public boolean remove(Integer item) {
   Node pPrev = m_pHead;
Node pCur = m_pHead.pNext;
  
if (m_pHead.data == item) {
   m_pHead = m_pHead.pNext;
   pPrev.pNext = null;
   m_iSize--;
   return true;
}
  
while (null != pCur) {
   if (pCur.data == item) {
       pPrev.pNext = pCur.pNext;
       pCur.pNext = null;
      
if (null == pPrev.pNext)
   m_pTail = pPrev;

       m_iSize--;
       return true;
   }

   pPrev = pCur;
   pCur = pCur.pNext;
}
  
return false;
}

@Override
public void clear() {
   m_pHead = null;
   m_pTail = null;
}

@Override
public boolean contains(Integer item) {
Node pCur = m_pHead;
  
while (null != pCur) {
   if (pCur.data == item) {
       return true;
   }
  
   pCur = pCur.pNext;
}
  
return false;
}

@Override
public Integer get(int index) {
   Node pCur = m_pHead;
  
if (index < 0 || index >= m_iSize)
   return null;
  
for (int i = 0; i < index; i++)
   pCur = pCur.pNext;
  
return pCur.data;
}

@Override
public int indexOf(Integer item) {
Node pCur = m_pHead;
int index = 0;
  
while (null != pCur.pNext) {
   if (pCur.data == item) {
       return index;
   }
  
   index++;
   pCur = pCur.pNext;
}
  
return (pCur.data == item) ? index : -1;
}

@Override
public boolean isEmpty() {
   return (m_pHead == null) ? true : false;
}

@Override
public int size() {
return m_iSize;
}

// Uncomment when ready to test
@Override
public String toString() {
String ret = "";
Node curr = m_pHead;
while (curr != null) {
ret += curr.data + " ";
curr = curr.pNext;
}
return ret;
}

}

MiniList.java:

public interface MiniList<E> {
public boolean add(E item);
public void add(int index, E element);

public E remove();
public E remove(int index);
public boolean remove(E item);

public void clear();

public boolean contains(E item);

public boolean equals(Object o);

public E get(int index);
public int indexOf(E item);

public boolean isEmpty();

public int size();
}

LLTest.java:

public class LLTest {
public static void main(String args[]) {
MyLinkedList ll = new MyLinkedList();

// Testing add methods
System.out.println("Testing Add");
ll.add(40);
ll.add(10);
ll.add(20);
ll.add(1, 30);
ll.add(3, 100);
ll.add(65);
ll.add(2);
System.out.println("Expected: 40 30 10 100 20 65 2");
System.out.println("Actual: " + ll);
System.out.println("Size should be 7 -> " + ll.size());
System.out.println();

// Testing remove methods
System.out.println("Testing Remove");
ll.remove();
ll.remove(2);
ll.remove(3);
ll.remove((Integer)2);
ll.remove(100);
System.out.println("Expected: 30 10 20");
System.out.println("Actual: " + ll);
System.out.println("Size should be 3 -> " + ll.size());
System.out.println();

// Testing Contains
System.out.println("Testing Contains");
ll.add(2); // to make it a little bigger
System.out.println("Should be true -> " + ll.contains(2));
System.out.println("Should be false -> " + ll.contains(65));
System.out.println("Should be true -> " + ll.contains(30));
System.out.println();

// Testing Get
System.out.println("Testing Get");
System.out.println("Actual list: " + ll);
System.out.print("List using get: ");
for (int i = 0; i < ll.size(); i++) {
System.out.print(ll.get(i) + " ");
}
System.out.println("\n");

// Testing indexOf
System.out.println("Testing indexOf");
System.out.println("Should be 2 -> " + ll.indexOf(20));
System.out.println("Should be 3 -> " + ll.indexOf(2));
System.out.println("Should be -1 -> " + ll.indexOf(65));
System.out.println();
  
// You can write more tests (these are not all encompassing)
// When should these methods fail/throw exceptions?
// Have all of the edge cases been tested?
// Not quite all of the methods have been tested here (e.g. clear())
  
System.out.println("Testing isEmpty and clear.");
System.out.println("Should be false -> " + ll.isEmpty());
ll.clear();
System.out.println("Should be true -> " + ll.isEmpty());
}
}

Sample Output Screenshots:


Related Solutions

Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType>...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType> { /** * Construct an empty LinkedList. */ public MyLinkedList( ) { doClear( ); } private void clear( ) { doClear( ); } /** * Change the size of this collection to zero. */ public void doClear( ) { beginMarker = new Node<>( null, null, null ); endMarker = new Node<>( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; modCount++; } /**...
myLinkedList.java>>>>>>>> public class MyLinkedList implements MiniList<Integer>{ /* Private member variables that you need to declare: **...
myLinkedList.java>>>>>>>> public class MyLinkedList implements MiniList<Integer>{ /* Private member variables that you need to declare: ** The head pointer ** The tail pointer */ public class Node { // declare member variables (data and next) // finish these constructors public Node(int data, Node next) {} public Node(int data) {} // HINT: use this() with next = null } // Initialize the linked list (set head and tail pointers) public MyLinkedList() {} @Override public boolean add(Integer item) { return false; }...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
The language is java package hw; public class MyLinkedList<E> { SLLNode<E> head = null; public MyLinkedList()...
The language is java package hw; public class MyLinkedList<E> { SLLNode<E> head = null; public MyLinkedList() {} // O(1) public MyLinkedList(E[] elements) { // O(elements.length) for(int i=elements.length-1;i>=0;i--) add(elements[i]); } public void printLinkedList() { // T(N) = O(N) System.out.print("printLinkedList(): "); SLLNode<E> node = head; while(node != null) { System.out.print(node.info + " "); node = node.next; // move to the next node } System.out.println(); } public void add(E e) { // T(N) = O(1) SLLNode<E> newNode = new SLLNode<E>(e); newNode.next = head;...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
The MyLinkedList class is a one-way directional linked list that enables one-way traversal of the list....
The MyLinkedList class is a one-way directional linked list that enables one-way traversal of the list. Modify the Node class to add the new field name previous to refer to the previous node in the list, as follows:     public class Node {       E element; Node next;       Node previous;       public Node(E o) {         element = o;       }     } Implement a new class named MyTwoWayLinkedList that uses a doubly linked list to store elements. Hint....
Remove the minimum element from the linked list in Java public class LinkedList {      ...
Remove the minimum element from the linked list in Java public class LinkedList {       // The LinkedList Node class    private class Node{               int data;        Node next;               Node(int gdata)        {            this.data = gdata;            this.next = null;        }           }       // The LinkedList fields    Node head;       // Constructor    LinkedList(int gdata)   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT