I have a Java Data Structures project and we're creating a linked list and an iterator for that linked list. The add method for the linked list has to be O(1). This is my code
public class MyLinkedList extends CS20bLinkedList implements
Iterable<Integer> {
@Override
public void add(int value){
Node tail = head;
if(head == null){
head = new Node(value);
//System.out.println("Very Fun! "+head.value);
return;
}else{
tail.next = new Node(value);
//System.out.println("Very Fun! "+head.value);
//System.out.println("Very Fun! "+tail.value);
//System.out.println("Very Funny "+tail.next.value);
tail = tail.next;
}
public class MyIterator extends MyLinkedList implements
Iterator<Integer> {
Node temp;
MyIterator(Node head){
temp = head;
}
@Override
public boolean hasNext() {
return temp ==
null;
}
@Override
public Integer next() {
Integer item;
item = temp.value;
temp = temp.next;
return item;
}
}
}
@Override
public
Iterator<Integer> iterator() {
return new MyIterator(head);
}
}
I'm pretty sure the problem with my linked list is that every time the add method gets called it the tail node gets reset and it becomes equal to the head node. I need the tail node to make the add method O(1) instead of O(N). How do I give a value to tail without having it reset every time the add method gets called?
Hi I have added some comments apart from that your code is fine. In case of any doubts please comment
public class MyLinkedList extends CS20bLinkedList implements
Iterable<Integer> {
@Override
public void add(int value){
//Node tail = head;//this statement is wrong here tail is also for
whole linked list so like head, tail also to be declared where head
has been declared
if(head == null){
head = new Node(value);
//System.out.println("Very Fun! "+head.value);
//here since head is null so there is no no node in the list and
now we are adding one node so head and tail points to same node
here following statement has to be added
tail=head;
return;
}else{
tail.next = new Node(value);
//System.out.println("Very Fun! "+head.value);
//System.out.println("Very Fun! "+tail.value);
//System.out.println("Very Funny "+tail.next.value);
tail = tail.next;
}
public class MyIterator extends MyLinkedList implements
Iterator<Integer> {
Node temp;
MyIterator(Node head){
temp = head;
}
@Override
public boolean hasNext() {
return temp == null;
}
@Override
public Integer next() {
Integer item;
item = temp.value;
temp = temp.next;
return item;
}
}
}
@Override
public Iterator<Integer> iterator() {
return new MyIterator(head);
}
}
I have a Java Data Structures project and we're creating a linked list and an iterator...
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;...
// 1. Add methods to get and set, Data and Link. Data should be any Comparable object. class Node { Integer data; // Integer is Comparable Node link; public Node(Integer data, Node link) { this.data = data; this.link = link; } public Integer getData() { return data; } public void setData(Integer data) { this.data = data; } public Node getLink() { return link; } public void setLink(Node link) { this.link = link; } } // b. Create MyLinkedList class and...
iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains a loop. Print true if yes, false if not. Test by using the following code: LL<Integer> L = new LL<>(); for (int i = 1000; i > 0; i-=3) sl.add(i); try { L.insert(122, L.getNode(70), L.getNode(21)); if (L.detectLoop()) System.out.println("True"); else System.out.println("False."); } catch(Exception e){ e.printStackTrace(); } class Linkedlist<E>{ private static class Node<E>{ private E element; private Node<E> next; public Node(E e, Node<E> n){ element =...
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 =...
Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...
Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...
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...
Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...
Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...
JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1. You are given a list of phrases each ending with a pound sign: ‘#’. 2. Create a single String object from this list. 3. Then, split the String of phrases into an array of phrases...