I am trying to implement the set method where you pass in the position in a linked list that you want and the value and the set function adds that value into the position specified in the linked list. I have implemented the set function but for some reason The last element disappears in my implementation. I would greatly appreciate any help. Thanks in advance. I would appreciate any expert eyes that will see what I am missing.
/**
* A basic singly linked list implementation.
*/
public class SinglyLinkedList<E> implements Cloneable, Iterable<E>, List<E> {
//---------------- nested Node class ----------------
/**
* Node of a singly linked list, which stores a reference to its
* element and to the subsequent node in the list (or null if this
* is the last node).
*/
private static class Node<E> {
E value;
Node<E> next;
public Node(E e)
{
value = e;
next = null;
}
}
//----------- end of nested Node class -----------
// instance variables of the SinglyLinkedList
private Node<E> head = null; // head node of the list (or null if empty)
private int size = 0; // number of nodes in the list
public SinglyLinkedList() {
} // constructs an initially empty list
// access methods
/**
* Returns the number of elements in the linked list.
*
* #return number of elements in the linked list
*/
public int size() {
return size;
}
/**
* Adds an element to the end of the list.
*
* #param e the new element to add
*/
public void addLast(E e) {
// TODO
}
/**
* Tests whether the linked list is empty.
*
* #return true if the linked list is empty, false otherwise
*/
public boolean isEmpty() {
return size == 0;
}
#Override
public E get(int i) throws IndexOutOfBoundsException {
Node<E> a = head;
if(i<=this.size()) {
int count = 0;
while(count < i) {
count ++;
a = a.next;
}
return a.value;
}
return null;
}
#Override
public E set(int i, E e) throws IndexOutOfBoundsException {
Node<E> current = head;
Node<E> setNode = new Node<E>(e);
if(i==0) {
this.addFirst(e);
}
else if(i==this.size){
this.addLast(e);
}
else {
for(int j=0; current != null && j < (i-1);j++) {
current = current.next;
}
Node<E> temp = current.next;
current.next = setNode;
setNode.next = temp;
}
return setNode.value;
}
// update methods
/**
* Adds an element to the front of the list.
*
* #param e the new element to add
*/
public void addFirst(E e) {
Node<E> first = new Node<>(e);
first.next = this.head;
this.head = first;
this.size++;
}
#SuppressWarnings({"unchecked"})
public boolean equals(Object o) {
// TODO
return false; // if we reach this, everything matched successfully
}
#SuppressWarnings({"unchecked"})
public SinglyLinkedList<E> clone() throws CloneNotSupportedException {
// TODO
return null;
}
/**
* Produces a string representation of the contents of the list.
* This exists for debugging purposes only.
* #return
*/
public String toString() {
for(int i=0;i<this.size();i++) {
System.out.println(this.get(i));
}
return "end of Linked List";
}
public static void main(String[] args) {
SinglyLinkedList <Integer> ll =new SinglyLinkedList <Integer>();
ll.addFirst(5);
ll.addFirst(4);
ll.addFirst(3);
ll.addFirst(2);
ll.set(1,0);
System.out.println(ll);
}
}
Assumptions
addLast method is missing in the code
The error could be there too
Known cause
This code is not incrementing the size inside the set method's for loop.The size is incremented in addLast(possibly) and addFirst and not incremented in other case (final else part)
It is not clear what is planned to do with set method. The way it is implemented now, it behaves more like insert, meaning that it will add new node, bit it does not increase the total count of elements (size).
It the set method is changing the value of the node, which name indicates, then this part is wrong:
else {
for(int j=0; current != null && j < (i-1);j++) {
current = current.next;
}
Node<E> temp = current.next;
current.next = setNode;
setNode.next = temp;
}
It should replace the value instead of adding the new one:
else {
for(int j=0; current != null && j < (i-1);j++) {
current = current.next;
}
current.value=e
}
Also, it looks like the index i is 1-based, while everything else is 0 based. I didn't check the code above, but the concept should be like shown.
How to remove a specific value from a linked list java?
I tried to make it in my implementation, but it wasn't easy..
Here is what I'm trying to make:
//How to do this...;<..
int remove(Item item) {
Node cur = first.next;
Node prev = first;
while (cur !=null) {
if (cur.item.equals(item)) {
item = dequeue();
}
cur = cur.next;
// TODO
}
return 0;
}
These are the pre-setup:
public class LinkedQueue<Item> implements Iterable<Item> {
private int N; // number of elements on queue
private Node first; // beginning of queue
private Node last; // end of queue
// helper linked list class
private class Node {
private Item item;
private Node next;
}
/**
* Initializes an empty queue.
*/
public LinkedQueue() {
first = null;
last = null;
N = 0;
assert check();
}
public Item dequeue() {
if (isEmpty()) throw new NoSuchElementException("Queue
underflow");
Item item = first.item;
first = first.next;
N--;
if (isEmpty()) last = null; // to avoid loitering
assert check();
return item;
}
And the main function:
public static void main(String[] args) {
LinkedQueue<String> q = new LinkedQueue<String>();
q.enqueue("a");
q.enqueue("b");
q.enqueue("c");
q.enqueue("a");
q.enqueue("b");
q.enqueue("d");
q.enqueue("b");
q.enqueue("abba");
q.enqueue("a");
q.enqueue("z");
q.enqueue("a");
System.out.println(q);
System.out.println("Remove some of elements.");
q.remove("a");
q.remove("f");
q.remove("c");
System.out.println(q);
}
}
And I have a result like this. It doesn't change at all..
a b c a b d b abba a z a
Remove some of elements.
a b d b abba a z a
It only erase value c. I don't know why.
Since Java 8 there is removeIf(Predicate<? super E> filter) method where you can put your own condition.
list.removeIf(cur -> cur.item.equals(item));
As per the details of question,i assume you are fairly new in Java.
What you are asking and the details you are showing are totally different.
LinkedQueue<String> q = new LinkedQueue<String>();
is only applicable if LinkedQueue is a genreic class not a specific impl for Item type class. i.e you are not creating object of LinkedQueue<Item> class. LinkedQueue<String> and LinkedQueue<Item> is different.
cur.equals(item) lack of knowledge of equal contract and difference in == vs equal. i.e you are comparing a two totally different object. One is a Node and other is Item class object.
Suggestion: clear basics, Read book by cathy Sierra.Scjp Sun Certified Programmer for Java 6
As for answer, you are literally not calling remove from main (test it via a print
statement in remove method). That is why you keep getting same answer.
Note: Your really not can't digest real solution even if we tell.
Following code snippet contains various remove() methods, taken from one of my LinkedList implementation, written in Java.
Code
LinkedList.java (partly)
private int size; // node count,
private LinkedListNode<T> head; // first node,
private LinkedListNode<T> end; // last node,
/**
* Remove by index.
*
* #param k index, start from 0,
* #return value of removed node, or null if not removed,
*/
#Override
public T remove(int k) {
checkElementIndex(k);
// find target node, and remember previous node,
LinkedListNode<T> preNode = null;
LinkedListNode<T> node = head;
while (k-- > 0) {
preNode = node;
node = node.next;
}
T result = (T) node.value; // keep return value,
removeNode(node, preNode); // remove
return result;
}
/**
* Remove by value, only remove the first occurrence, if any.
*
* #param v
* #return whether removed,
*/
#Override
public boolean removeValue(T v) {
// find target node, and remember previous node,
LinkedListNode<T> preNode = null;
LinkedListNode<T> node = head;
while (true) {
if (node == null) return false;// not found,
if (node.getValue().compareTo(v) == 0) break; // value found,
preNode = node;
node = node.next;
}
removeNode(node, preNode); // remove
return true;
}
/**
* Remove by value, remove all occurrences.
*
* #param v
* #return count of nodes removed,
*/
#Override
public int removeAllValue(T v) {
int rc = 0;
// find target node, and remember previous node,
LinkedListNode<T> preNode = null;
LinkedListNode<T> node = head;
while (true) {
if (node == null) return rc; // reach end,
if (node.getValue().compareTo(v) == 0) { // value found,
rc++;
if (removeNode(node, preNode)) break; // remove, break if it's end,
continue; // recheck this node, since it become the next node,
}
preNode = node;
node = node.next;
}
return rc;
}
/**
* Remove given node, which guarantee to exists. Also reduce the size by 1.
*
* #param node node to delete,
* #param preNode previous node, could be null,
* #return indicate whether removed node is end,
*/
protected boolean removeNode(LinkedListNode node, LinkedListNode preNode) {
LinkedListNode nextNode = node.next; // next node,
boolean isEnd = (nextNode == null);
if (isEnd) { // target is end,
if (preNode == null) { // target is also head,
head = null;
} else { // target is not head, thus preNode is not null,
preNode.next = null;
}
end = preNode;
} else { // target is not end,
// replace target with next node,
node.next = nextNode.next;
node.value = nextNode.value;
}
size--; // reduce size by 1,
return isEnd;
}
/**
* Remove head node,
*
* #return
*/
#Override
public T removeHead() {
return remove(0);
}
/**
* Remove end node,
*
* #return
*/
#Override
public T removeEnd() {
return remove(size - 1);
}
LinkedListTest.java (partly)
(unit test, via TestNG)
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* LinkedList test.
*
* #author eric
* #date 1/28/19 6:03 PM
*/
public class LinkedListTest {
private int n = 10;
private LinkedList<Integer> llist; // linked list,
private LinkedList<Integer> dupEvenLlist; // linked list, with duplicated even values,
#BeforeMethod
public void init() {
// init llist,
llist = new LinkedList(); // create linked list,
Assert.assertTrue(llist.isEmpty());
LinkedList.appendRangeNum(llist, 0, n); // append range,
// init dupEvenLlist,
dupEvenLlist = new LinkedList(); // create linked list,
LinkedList.appendRangeNum(dupEvenLlist, 0, n); // append range,
LinkedList.appendRangeNum(dupEvenLlist, 0, n, 2); // append range, again, with step as 2 (only even numbers),
Assert.assertEquals(dupEvenLlist.size(), n + n / 2);
}
// non-remove related test cases ... are deleted,
// remove(k) - remove by index,
#Test
public void testRemoveByIndex() {
for (int i = 0; i < n; i++) {
Assert.assertEquals(llist.removeEnd().intValue(), n - 1 - i); // remove by end, in turn it remove by index,
Assert.assertEquals(llist.size(), n - 1 - i);
}
Assert.assertTrue(llist.isEmpty());
}
// remove(v) - remove by value,
#Test
public void testRemoveByValue() {
Assert.assertFalse(llist.removeValue(n)); // not exists,
for (int i = n - 1; i >= 0; i--) {
Assert.assertTrue(llist.removeValue(i)); // remove by value,
Assert.assertEquals(llist.size(), i);
}
Assert.assertTrue(llist.isEmpty());
Assert.assertFalse(llist.removeValue(0)); // empty,
// remove from list with duplicated value,
for (int i = 0; i < n; i++) {
Assert.assertTrue(dupEvenLlist.removeValue(i));
}
Assert.assertFalse(dupEvenLlist.isEmpty());
Assert.assertEquals(dupEvenLlist.size(), n / 2);
}
// removeAll(v) - remove all occurrences by value,
#Test
public void testRemoveAllByValue() {
Assert.assertEquals(dupEvenLlist.removeAllValue(n), 0); // not exists,
int remainSize = dupEvenLlist.size();
for (int i = 0; i < n; i++) {
int rc = dupEvenLlist.removeAllValue(i); // remove all by value,
Assert.assertEquals(rc, i % 2 == 0 ? 2 : 1);
remainSize -= rc;
Assert.assertEquals(dupEvenLlist.size(), remainSize);
}
Assert.assertTrue(dupEvenLlist.isEmpty());
Assert.assertEquals(dupEvenLlist.removeAllValue(0), 0); // empty,
}
}
All test cases would pass.
Explanation
Methods:
T remove(int k), remove by index.
Steps:
* loop to target node,
* for each step,
record:
* previous node,
* this node,
* get next node, of target node,
* get value of target node,
as return value later,
* if target is end,
* if also head,
head = null;
* if not head,
preNode.next = null;
* end = preNode;
* if targe is not end,
replace it with its next node,
logic:
* node.value = nextNode.value;
* node.next = nextNode.next;
* return previously tracked value of target node,
boolean removeValue(T v), remove by value, only remove first occurrence, if any.
The logic is similar as remove by index.
The differences are:
At initial search, compare element instead of loop to index, to find target.
Return boolean that indicate whether removed, instead of removed value,
int removeAllValue(T v), remove all by value, remove all occurrences.
This is similar as remove by value.
Differences:
[inside while()]
It will search all occurrence till end.
After removing an occurrence, it "continue" to recheck the current node.
Because the current node has actual replaced by it's next.
If removed node is end, then return.
This relay on the return value of removeNode().
It record count of removed occurrence.
[return value]
It return count of removed occurrence, instead of boolean.
boolean removeNode(LinkedListNode node, LinkedListNode preNode), remove by node, with preNode given.
Remove given node, which is guaranteed to exists, with previous node given, which might be null.
Return value indicate whether removed node is end, it's mainly used to support removeAllValue().
T removeHead(), T removeEnd(), remove head / end.
Simply calls remove by index, with corresponding index 0 and size - 1 passed.
Tips:
LinkedList represent linkedlist, with fields size, head, end, and generic type T (for value type in node), it's not thread-safe.
checkElementIndex() method, check given index, and throw exception if out of range.
LinkedListNode, represent the node in linked list. with fields value, next.
Complexity
Remove single: O(k)
Remove all by value: O(n)
Where:
k, is the index of target.
n, is size of linkedlist.
In your if statement you are checking if the cur Node is equal to the Item passed in: if (cur.equals(item)).
I think you should be checking if the Item stored in the cur Node is equal to the Item passed into your function: if (cur.item.equals(item)).
In creating a manual linked list using recursive methods, I cant figure out why my Insert Before method is cutting off the list after a new node is inserted. The list is not sorted. I am new to this and any help would be appreciated as to why this is happening.
My node class
class Csc2001Node
{
protected char ch;
protected Csc2001Node next;
/*
* Construct a Csc2001Node with the given character value
* #param c - The character
*/
public Csc2001Node (char c)
{
this.ch = c;
this.next = null;
}
}
My methods from the Linked List class
/*
* Recursively prints characters in a list
* #param head The had of current list
* #return Current list
*/
private String recursePrintList(Csc2001Node head){
if(head == null)
return "List is empty\n";
else
while(head.next!=null)
{
return head.ch + "\n" + recursePrintList(head.next);
}
return head.ch + "\n";
}
/*
* Wrapper method for printing list
* #return the list as a string
*/
public void recursePrintList(){
System.out.print(recursePrintList(head));
}
/*
* Inserts a character before first occurrence of another specified
* character in the list.
*
*/
public Csc2001Node insertBefore(char key, Csc2001Node head, char toInsert)
{
if(head==null){
return head = new Csc2001Node(toInsert);
}
else if(head.ch == key)
return new Csc2001Node(toInsert);
else
head.next = insertBefore (key, head.next, toInsert);
return head;
}
/*
* Wrapper method for inserting a character before another
*/
public void insertBefore(char target, char toInsert){
head = insertBefore(target, head, toInsert);
}
Output of what is happening
Adding the characters a, s, t, e, r to the list and printing out the list
e
a
s
t
e
r
Testing if the character r is in the list, print out Yes if it is and No otherwise
Yes
Printing out the value of size for this list
6
Trying to insert Y before s in the list and printing out the list
e
a
Y
Printing out the value of size for this list
3
Trying to insert V before e into the front of the list and printing out the list
V
else if(head.ch == key)
return new Csc2001Node(toInsert);
is putting the new character in as the last element in the list and so your list is terminated. You want something like
else if (head.next.ch.equals(key)) {
nextNode = head.next;
head.next = new Csc2001Node(toInsert);
head.next.next = nextNode;
}
My program is meant to take a list of words and store each word under a letter reference in an array in ascending order. For example array of A-Z words apple, ape under a linked list under A referenced by 0, Zebra under Z referenced by 25. But when I use the standard first = new Node(word) I am not adding anything. I'm hopelessly lost.
import java.util.LinkedList;
public class ArrayLinkedList
{
/**
The Node class is used to implement the
linked list.
*/
private class Node
{
String value;
Node next;
/**
* Constructor
* #param val The element to store in the node
* #param n The reference to the successor node
*/
Node(String val, Node n)
{
value = val;
next = n;
}
Node(String val)
{
this(val, null);
}
}
private final int MAX = 26; // Number of nodes for letters
private Node first; // List head
private Node last; // Last element in the list
private LinkedList[] alpha; // Linked list of letter references
/**
* Constructor to construct empty array list
*/
public ArrayLinkedList()
{
first = null;
last = null;
alpha = new LinkedList[MAX];
for (int i = 0; i < MAX; i++)
{
alpha[i] = new LinkedList();
}
}
/**
* arrayIsEmpty method
* To check if a specified element is empty
*/
public boolean arrayIsEmpty(int index)
{
return (alpha[index].size() == 0);
}
/**
* The size method returns the length of the list
* #return The number of elements in the list
*/
public int size()
{
int count = 0;
Node p = first;
while (p != null)
{
// There is an element at p
count++;
p = p.next;
}
return count;
}
/**
* add method
* Adds the word to the first position in the linked list
*/
public void add(String e)
{
String word = e.toLowerCase(); // Put String to lowercase
char c = word.charAt(0); // to get first letter of string
int number = c - 'a'; // Index value of letter
// Find position of word and add it to list
if (arrayIsEmpty(number))
{
first = new Node(word);
first = last;
}
else
{
first = sort(first, word, number);
}
}
/**
* nodeSort method
* To sort lists
*/
private Node sort(Node node, String value, int number) {
if (node == null) // End of list
{
return getNode(value, number);
}
int comparison = node.value.compareTo(value);
if (comparison >= 0) // Or > 0 for stable sort.
{
Node newNode = getNode(value, number); // Insert in front.
newNode.next = node;
return newNode;
}
node.next = sort(node.next, value, number); // Insert in the rest.
return node;
}
private Node getNode(String value, int number)
{
return first.next;
}
/**
* get method
* to get each word value from the linked list and return it
* #return value
*/
public LinkedList get(int index)
{
return alpha[index];
}
public String toString()
{
StringBuilder sBuilder = new StringBuilder();
sBuilder.append("Word and occurrence in ascending order\n\n");
Node p = first;
while (p != null)
{
sBuilder.append(p.value + "\n");
p = p.next;
}
return sBuilder.toString();
}
}
Is there a reason you are doing it this way.
I can think of an easier way: use Map which will map a character (e.g. "A") to a LinkedList of Words.
hello im trying to implement a Linked list in java.
As this is a homework assignment I am not allowed to use the built in LinkedList from java.
Currently I have implemented my Node class
public class WordNode
{
private String word;
private int freq;
private WordNode next;
/**
* Constructor for objects of class WordNode
*/
public WordNode(String word, WordNode next )
{
this.word = word;
this.next = next;
freq = 1;
}
/**
* Constructor for objects of class WordNode
*/
public WordNode(String word)
{
this(word, null);
}
/**
*
*/
public String getWord()
{
return word;
}
/**
*
*/
public int getFreq(String word)
{
return freq;
}
/**
*
*/
public WordNode getNext()
{
return next;
}
/**
*
*/
public void setNext(WordNode n)
{
next = n;
}
/**
*
*/
public void increment()
{
freq++;
}
}
and my "LinkedList"
public class Dictionary
{
private WordNode Link;
private int size;
/**
* Constructor for objects of class Dictionary
*/
public Dictionary(String L)
{
Link = new WordNode(L);
size = 1;
}
/**
* Return true if the list is empty, otherwise false
*
*
* #return
*/
public boolean isEmpty()
{
return Link == null;
}
/**
* Return the length of the list
*
*
* #return
*/
public int getSize()
{
return size;
}
/**
* Add a word to the list if it isn't already present. Otherwise
* increase the frequency of the occurrence of the word.
* #param word The word to add to the dictionary.
*/
public void add(String word)
{
Link.setNext(new WordNode(word, Link.getNext()) );
size++;
}
Im having trouble with implementing my add method correctly, as it has to check the list, for wether the word allready exists and if do not exists, add it to the list and store it alphabetic.
Ive been working on a while loop, but cant seem to get it to work.
Edit: Ive been trying to print the list but it wont print more than the first added word
public void print()
{
WordNode wn = Link;
int size = 0;
while(wn != null && size <= getSize()){
System.out.println(wn.getWord()+","+wn.getFreq(wn.getWord()));
size++;
}
}
Any help is appreciated
Your add method is wrong. You're taking the root node and setting its next value to the new node. So you will never have any more than 2 nodes. And if you ever have 0, it will probably crash due to a null pointer.
What you want to do is set a current value to the root node, then continue getting the next node until that node is null. Then set the node.
WordNode current = Link;
// Check if there's no root node
if (current == null) {
Link = new WordNode(word);
} else {
// Now that the edge case is gone, move to the rest of the list
while (current.getNext() != null) {
/* Additional checking of the current node would go here... */
current = current.getNext();
}
// At the last element, at then new word to the end of this node
current.setNext(new WordNode(word));
}
You need to keep the instance of the previous node so you can set the next value. Since this would cause a problem if there are no nodes to begin with, there needs to be some extra logic to handle a root node differently. If you'll never have 0 nodes, then you can remove that portion.
If you also need to check the values of the variables in order to see if it's there, you can add something to the while loop that looks at the current value and sees if it is equal to the current word you're looking for.
WordNode current = Link;
while (current != null) {
//check stuff on current word
current = current.getNext();
}
Without your loop code, it will be hard to help you with your specific problem. However, just to give you a bit of a hint, based on the instructions you don't actually have to search every node to find the word. This will give you the opportunity to optimize your code a bit because as soon as you hit a word that should come after the current word alphabetically, then you can stop looking and add the word to the list immediately before the current word.
For instance, if the word you're adding is "badger" and your words list are
apple-->avocado-->beehive-->...
You know that beehive should come after badger so you don't have to keep looking. What you will need to implement is a method that can do alphabetical comparison letter-by-letter, but I'll leave that to you ;-)
Assuming that you always insert in alphabetical order it should look something like this:
public void add(String word){
WordNode wn = Link;
WordNode prevNode = null;
while(wn != null){
if(word.equals(wn.getWord())){
//match found, increment frequency and finish
wn.increment();
return;
}else if(word.compareTo(wn.getWord) < 0){
//the word to add should come before the
//word in the current WordNode.
//Create new link for the new word,
//with current WordNode set as the next link
WordNode newNode = new WordNode(word, wn)
//Fix next link of the previous node to point
//to the new node
if(prevNode != null){
prevNode.setNext(newNode);
}else{
Link = newNode;
}
//increase list size and we are finished
size++;
return;
}else{
//try next node
prevNode = wn;
wn = wn.getNext();
}
}
//If we got here it means that the new word
//should be added to the end of the list.
WordNode newNode = new WordNode(word);
if(prevNode == null){
//if list was originally empty
Link = newNode;
}else{
//else append it to the end of existing list
prevNode.setNext(newNode);
}
//increment size and finish
size++;
}
Use this code :
class NodeClass {
static Node head;
static class Node {
int data;
Node next;
//constractor
Node(int d) {
data=d;
next=null;
}
}
static void printList(Node node ) {
System.out.println("Printing list ");
while(node!=null){
System.out.println(node.data);
node=node.next;
}
}
static void push(int data) {
Node node = new Node(data);
node.next = head;
head = node;
}
static void addInLast(int data) {
Node node = new Node(data);
Node n = head;
while(n.next!=null){
n= n.next;
}
n.next = node;
node.next = null;
}
public static void main (String[] args) {
NodeClass linklistShow = new NodeClass();
linklistShow.head = new Node(1);
Node f2 = new Node(2);
Node f3 = new Node(3);
linklistShow.head.next = f2;
f2.next =f3;
printList(linklistShow.head);
push(6);
addInLast(11);
printList(linklistShow.head);
}
}