Ordered insertion into linked list - java

I have to write a program that inserts strings in order, such as when i insert dog and cat, regardless of the order I insert them, it should return cat, dog.
As of now when I do it, it isn't inserting in order, it's inserting just like normal. I'm pretty sure my method for switching the head and current works because earlier, it would flip my input, whether it was supposed to or not So if it was supposed to be cat dog, it would return dog cat. For whatever reason it gets to my if statement, it's almost like it skips it. Any tips would be greatly appreciated.
public void insert(String s){
head= new node(s,head);
node current=head.getNext();
if(current == null){
current=head;
while(current.getNext() != null){
if(current.getData().compareTo(s)>0){
current.setNext(head);
head.setNext(current.getNext().getNext());
head=current;
current=head;
}
current= current.getNext();
}
}else{
while(current.getNext() != null){
if(current.getData().compareTo(s)>0){
current.setNext(head);
head.setNext(current.getNext().getNext());
head=current;
current=head;
}
current=current.getNext();
}
}
}

You can sort your list using java.util.Collections class
Ex:
Collections.sort(your_list);

There are a few issues with your code and the logic. I will give hints for fixing below
Everytime you call insert you create a new head for the list (I presume a field of your class). that's not how linkedlists work. You should only create a new head when head is null (empty list)
You are setting current to the next node after the newly created head. Therefore it will have whatever value the constructor of node assigned to it. If it assigns a default value of null, you will never get into the else part of the if statement.
According to the above you will inevitably get in the first of the if statement where you reassign current from null to head. Then you are basically comparing the data (String) of the same node (head) and you never get in the next if.
So basically the function you wrote is equivalent to this (try it out)
public void insert(String s) {
head = new node(s, head);
node current = head.getNext();
}
Which is probably not what you intended. Start changing your code by only creating head when it is null and then return after that (if list only has one element no need to swap). Then insert a new node after head and swap if needed.

Related

Remove All Occurrences of a Given Value from a Doubly Linked List

Alright, so cut a long story short, what I'm trying to do here is remove all instances of value e from a doubly linked list. As far as I know, my logic is at least mostly right, but for some off reason it isn't actually removing any of the nodes in my test cases.
public boolean removeAll(int e) {
DIntNode dummy = head,next = null;
if (head == null)
return false;
while (dummy != null) {
if (dummy.getData() == e) {
next = dummy.getNext();
dummy.getNext().setPrev(null);
dummy = next;
return true;
}
else
dummy = dummy.getNext();
}
return false;
}
This is what I currently have for my code of the metho. My logic here was to use a dummy DIntNode that starts at the head and a "next" node to help me shrink the list, so to speak. In other words, if the list was something like "1<-> 1 <-> 2 <-> 3", the function would change it to "2<->3", in theory. The reason this is a boolean function is because I'm required to return true if the given value is removed form the list.
Is there just another step in the logic that I'm missing, or is the methodology itself just unreliable? I'm very unsure at this point, so any and all help would be greatly appreciated.
You set
dummy.getNext().setPrev(null);
But previous node also have reference to next node you try to remove. You should set this reference to next active value.
That because when you want to get all linked list previous value still know about node you remove, because of next node reference
You can try with the following code:
if (dummy.getData() == e) {
DIntNode temp = dummy.getPrevious();
temp.next = dummy.getNext();
temp = dummy.getNext();
temp.previous = dummy.getPrevious();
return true;
}
This used the previous reference. So the previous node will now have reference to the next node of your dummy node (node to be deleted). And similarly, the next node of dummy node will have reference of previous node of your dummy node. So, the dummy node will loose its connection/link from its doubly link list and that's what we want!
Please try.
Two issues with the code:
When relinking a doubly linked list, where removing B from A - B - C, you need to set the next node for A to be C as well as the previous node for C to be A. With trying to keep you method names:
A.setNext(current.getNext());
C.setNext(current.getPrev());
With your code, if you find an occurrence, you return, which means that no other instances will be removed since you jump out of that method. You will probably need a new boolean removed variable, that is set to false, return true changed to removed = true and return false changed to return removed.
The method exits after the first encounter of 'e'.
If you want to remove all instances of 'e', then you should have something like this:
boolean listChanged = false;
while (dummy != null) {
if (dummy.getData() == e) {
// update list
...
listChanged = true;
}
...
}
return listChanged;
Also, you should not write your code like this:
dummy.getNext().setPrev(...); // throws NPE if next is null

java remove node linked list

I have a linked list and I want to remove a node from it based on the data inside of it.
public Node deleteNode(String a){
Node<String> temp = findNode(head, a);
temp = temp.previous;
System.out.println(temp.data);
temp = temp.getNext().getNext();
return temp;
}
This is the code I have for it, which in theory should work but it's doing nothing.
If I remove the "temp = temp.previous;" line the code works but removes the node after the one I want removed. If I run it as is then it just doesn't remove anything.
The print statement shows that I'm currently working with the node previous to the one found using the findNode(head, a) method but somehow something just gets screwed up.
If you want to remove a node, you need to alter the next and previous fields of neighbouring nodes.
if (temp.next!=null) {
temp.next.previous = temp.previous;
}
if (temp.previous!=null) {
temp.previous.next = temp.next;
}
That will link temp's two neighbouring nodes to each other, bypassing temp.
Then it would probably make sense to remove temp's references to its neighbours so it doesn't look like it is still part of the list.
temp.next = null;
temp.previous = null;
If you have separate references to the head and/or tail of your list, you need to reassign them in the case where the node you removed lay at the beginning or end of the list.

Implementation of removeFirst() method in SLinkedList in java

I got the following code from one book for implementing a singly linked list. And I don't understand some lines of code in the removeFirst() method, which removes the first node from the LinkedList.
class ListNode{
private String element;
private ListNode next;
public ListNode(){
element = null;
next = null;
}
public ListNode(String s, ListNode n){
element = s;
next = n;
}
//Access method
public String getElement(){
return element;
}
public ListNode getNext(){
return next;
}
//Modify method
public void setNext(ListNode n){
next = n;
}
}
public String removeFirst(){
if(head == null)
return null;
else{
ListNode temp = head;
head = head.getNext();
temp.setNext(null); //Which I don't understand, is it necessary?
size --;
return temp.getElement();
}
}
It seems that the statement temp.setNext(null); can be omitted. So why it is here, does it has anything to do with the garbage colletion in java. Since I am new to Java, any suggestions or ideas?
It depends on the entire implementation of the linked list, which you have not included in your question. However if it is possible for objects to hold a reference to a node even after if has been removed from the list, then the line is necessary.
Suppose we have a long chain of nodes A -> B -> C -> .... Suppose all of these nodes have been removed from the list, but that we still hold onto a reference to A. If all the nodes still held a reference to the next, this would prevent all of the nodes from being garbage collected. Simply setting the next node to be null ensures that only A cannot be garbage collected.
It is likely that implementations of a linked list do mean that references to nodes can be retained. For example, many implementations of Iterator hold a reference to the current node.
Consider this code:
Iterator<String> iterator = list.iterator();
while (i.hasNext()) {
if ("foo".equals(i.next())) {
i.remove();
break;
}
}
// lots more code
This code searches a list for the first occurrence of the String "foo". If it is found, it removes the "foo" from the list and breaks from the loop. The trouble with this is that the Iterator i is still in scope for the remaining code and still holds a reference to a node. This node may be in the middle of the list if the break occurred. Without setting next to be null, this would prevent all subsequent nodes from being garbage collected while i is still in scope, even if the list is cleared.
Note that you should generally make an iterator local to a loop anyway, like this
for (Iterator<String> i = list.iterator();;i.hasNext())
Let us suppose, single linked list of nodes is : A --> B --> C --> D with head node as A. Now, lets go though your removeFirst() method. When it get called, if(head == null) condition doesnt satisfy because our head node "A" is not NULL. Then it execute else statement,
temp = head //take head reference into temporary variable
head = head.getNext(); //As we want to delete first Node which is head so we are setting head to next node (i.e. head --> next) which is Node "B"
Now, to delete A (first Node) we need to break connection between A (which is previous head)--> B (current head) and that will done by
temp.setNext(null); //so out linked list became A B-->C-->D
and then as one node is deleted so in next statement it decreases size of link by size--.
I think we also set the temp reference to NULL as temp=NULL to make deleted node eligible for garbage collection.

returning an object from a method dealing with Linked List nodes

i'm writing a method using linked list to delete even number indexes from the list (i'm not using List<object> list = new LinkedList<object>() <--- i know this is kinda easier.., i'm implementing node class in this problem (which i'm really confused at right now.)
Well, the problem tells me to delete the even number indexes from a List, and return a new List. But i don't know what to do here (this method is in LinkedIntList class that also contains ListNode class)? And please check my code if it's right or what i can do to improve. Thanks.
public ????????? removeEvens() {
    ListNode current = front;
    while(current.next!= null) {
        current = current.next.next;
    }
    return ???????????;
}
EDIT: i've tried NodeList but it still give me an error so i guess i'll post a pic.
Since you want to return a new List, the return type should be a ListNode or a LinkedList.
public ListNode removeEvens(){
ListNode current = front;
//Since we are setting current's next to current.next.next, we need to make
//sure that we don't get a null point exception.
while(current.next!=null){
//removes all event nodes.
current.next = current.next.next;
current = current.next;
}
return current;
}
A LinkedList can be represented by a ListNode. If you want to return a LinkedList, simply change the return type of the function and return a new LinkedList with the current list node passed into the constructor.

Setting myself to null - Java

I came across the following problem:
Delete a node in the middle of a singly linked list, given only access to that node. (head is not given)
Now there are a lot of solutions and they all do not work when the element to be deleted is the last node.
Why wouldn't this work?
public static void removeNode (Node n){
if(n.next == null){ //n is the last node
n= null;
return;
}
//handling general case here
}
Java passes parameters by value, so setting n to null has no effect outside of the method. This means the method essentially does nothing when passed the last node of a list.
You need to set null the reference in the previous node, not the variable that references to your last node, something like this:
if(n.next == null) {
prev.next = null;
return;
}
n is local to the method, so changing its value won't affect the list itself. You need to modify the next of the previous node, which you do not have access to.

Categories