public class myLinkedList<E extends
Comparable<E>>{
private Node<E> head =
null;
private Node<E> tail =
null;
public myLinkedList() {
head = null;
}
public void concatenateList (myLinkedList<E> M) {//attach
another linkedList refered by M to the end of this linkedList
}
public int searchElement (E
targetElement){//test if a target element can be found on the list,
return the occurrences of the target element
}
public void removeElement(E
targetElement){//remove all target element that can be found on the
list
}
public E middleElement(){//find and
return the element that stored at the middle node of a
linkedList
}
=====================================================================================
Node class
public class Node<E extends Comparable<E>> {
private E
item;
// reference to the element stored at this node
private Node<E>
next; // reference
to the subsequent node in the list
/**
* Creates a node with the given element
and next node.
*
* @param e the element to be stored
* @param n reference to a node that should
follow the new node
*/
public Node(E e, Node<E> n) {
item = e;
next = n;
}
public Node(E e) {
item = e;
next = null;
}
// Accessor methods
public E getItem() { return item; }
public Node<E> getNext() { return next;
}
// Modifier methods
public void setNext(Node<E> n) { next = n;
}
} //----------- end of nested Node class -----------
//attach another linkedList refered by M to the end of this linkedList
public void concatenateList (myLinkedList<E> M) {
//To concatenate two linked list we have to make the
last node of first list to point to first node of second list
if (head == null) {
head = tail;
tail.setNext(head);
} else {
Node temp = head;
while (temp.next != null)
temp =
temp.next;
temp.next = tail;
tail.setNext(temp.next);
}
}
//test if a target element can be found on the list, return the
occurrences of the target element
public int searchElement(E targetElement){
int index=list.indexOf(targetElement);
return index;
}
//remove all target element that can be found on the list
public void removeElement(E targetElement){
int index=list.indexOf(targetElement);
if(index==0)
{
head=head.next;
}
else
{
Node n=head;
Node n1=null;
for(int i=0;i<index-1;i++)
{
n=n.next;
}
n1=n.next;
n.next=n1.next;
}
}
//find and return the element that stored at the middle node of
a linkedList
public E middleElement(){
//take two nodes pointing to the first node
//Increment first with two nodes and second with one,
at a time.
//Loop until the p1 loop reaches the end. At this
point, the p2 will be at the middle.
Node p1=head;
Node p2=head;
while ( p2.next != null ) {
p2 = p2.next;
if (p2.next != null) {
p2 = p2.next;
p1 = p1.next;
}
}
return (E) p1;
}
public class myLinkedList<E extends Comparable<E>>{ private Node<E> head = null; private Node<E>...
For some reason I am getting errors in this code. It is saying that the variables are not visible. I will be providing one of the methods as well as the Node class. This is for a Linked list in Java. Here is the method giving me errors saying "the field Node<E>.item is not visible" as well as "Node<E>.next is not visible. public void addFirst(E e) { // adds element e to the front of the list ...
Complete the implementation of the method replace: public class SinglyLinkedList private Node head, public SinglyLinkedListo this(null) public SinglyLinkedList(Node head) [ this.head -head public Node getHeado return head public void setHead(Node head) [ head: this. head Method that creates a Node containing 'item' @param item Value to be added this.headnew Node(item, this.head) * and adds it as the first element in the list *I public void add(int item) Method that finds the node at the given index d replaces its value...
could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...
Complete two of the List methods: SinglyLinkedList::size() and SinglyLinkedList::get(). import java.util.AbstractList; public class SinglyLinkedList<E> extends AbstractList<E> { /** Reference to the first node in our linked list. This will be null if the list is empty. */ private Entry head; /** * Creates an empty list. */ public SinglyLinkedList() { reset(); } /** * This method, which is only used within the SinglyLinkedList class, returns the instance to its initial state. This * call will reset the head to be...
Here is the IntegerLinkedList_incomplete
class:
public class IntegerLinkedList {
static class Node {
/** The element stored at this node */
private int element; // reference to the element stored at this node
/** A reference to the subsequent node in the list */
private Node next; // reference to the subsequent node in the list
/**
* Creates a node with the given element and next node.
*
* @param e the element to be stored
* @param n...
I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...
public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private int tail; Private int count; } public void enqueue(E item) { if(isFull()){ return; } count++; elements[tail] = item; tail = (tail + 1) % size; } public E dequeue() { if(isEmpty()) return null; int ct = count-1; E cur = elements[head]; int index = 0; for(i=1;ct-->0;i++) { if(cur.compareTo(elements[head+i)%size])<0) cur = elements[(head+i)%size]; index = i; } } return remove((head+index%size); public E remove(int index) { E...
C++ Given this linked list class: class LinkedList { private: Node * head; public: LinkedList(); void prepend(int val); int sum(); }; Implement a sum method which will return the sum of the values stored in the linked list.
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) { this.head = new Node(gdata); }...
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...