I asked my friends about this and they said you can never take the current to the previous node and when I asked why they didn't give me a clear reason can anyone help me?
//here is the signature of the method;
public void remove (){
Node<T> tmp = null;
tmp.next = head;
// I want to delete the current by the way!;
while (tmp.next != current)
tmp = tmp.next;
tmp.next = current.next;
//now am taking the current to the node before it so that it only becomes null if the linkedlist is empty;
current=tmp;
}
The basic idea is sound if you are looking to mutate an existing linked list and remove an element. Although there is a lack of curly braces after the while and a NullPointException that will occur at the first tmp.next.
Generally a remove method would be passed the data in the node to remove. It's unclear in your question what current is supposed to point to. Your code does not support removing the head of the list or empty lists (null head).
Here's a potential implementation:
public boolean remove(T value) {
if (head == null) {
return false;
} else if (head.value.equals(value)) {
head = head.next;
return true;
} else {
Node<T> current = head;
while (current.next != null) {
if (current.next.value.equals(value)) {
current.next = current.next.next;
return true;
}
current = current.next;
}
return false;
}
}
If the current variable is supposed to end up pointing to the node before the removed node then you have a problem: what happens if you are removing the head of the list?
Related
public void insertAfter(String after, String newName, int newPunkte)
{
ListNode newNode = new ListNode(newName, newPunkte, null);
if(head == null)
{
System.out.println("'InsertAfter' is not possible.");
return;
}
else
{
current = head;
while(current != null)
{
if(current.getName().equals(after))
{
System.out.println(after+" was found. "+newName+" was created.");
//Here is my problem...
current.setNext(newNode);
return;
}
previous = current;
current = current.getNext();
}
System.out.println(after+" was not found.");
return;
}
}
Hey guys, Ive got a little problem with my code. When I insert a new Node after
a (if so) found Node, the following Nodes (after the new one) are disappearing..
I pretty sure that the problem is that i didnt set "previous" after inserting.
Im pretty inexperienced with implementing Linked Lists.
I hope you can help me:)
By the way for understanding my code: The parameter "newPunkte" means "newPoints".
You need to set the next node on newNode:
ListNode next = current.getNext();
current.setNext(newNode);
newNode.setNext(next);
I am really struggling in adding a node at the end of a double linked list, adding to the head was easy, now I really can't find a way to add to the end. It seems stupid but I'm losing a lot of time, so if anyone can help that would be appreciated. Here's my partial implementation in Java.
public class DoublyLinkedList implements Iterable<Node>, Iterator<Node> {
private Node head = new Node();
private Node tail = head;
private Node current;
private Node previous;
#Override
public Iterator<Node> iterator() {
current = head;
previous = null;
return this;
}
public void addToHead(Node node) {
Node next = head.getNext(null);
Node previous = head.getNext(next);
head.setNext(previous, node);
node.setNext(null, head);
head = node;
}
#Override
public boolean hasNext() {
return current != tail;
}
#Override
public Node next() {
Node tmp = previous;
previous = current;
current = current.getNext(tmp);
return previous;
}
And here's Node Class
public class Node {
Node next = null;
Node previous = null;
Node getNext(Node node){
if(node == next){
return previous;
}
if(node == previous){
return next;
}
throw new IllegalStateException("something went wrong");
}
void setNext(Node node, Node next){
if(node == previous){
previous = next;
}
else if(node == this.next){
this.next = next;
}
else{
throw new IllegalStateException("something went wrong");
}
}
}
I hope you will understand the code above, basically I will need the following :
public void addToEnd(Node node) {
// implementation
}
What interface are you aiming to implement on this double-linked list? The way you've implemented getNext and setNext is not a clear way of doing it. If you only have one node, and you attempt to setNext with a null value for node, there is no way of telling unless you look at the code where your new node is going to end up. Along with that, I'm not sure if there is a nice way for you to set the previous node of a new tail node with the way you've implemented it. It's very ambiguous.
Can I recommend that you instead implement getNext(), setNext(Node), getPrevious() and setPrevious(Node) methods in your Node class? This would greatly simplify and clear up your code.
You would then be able to implement your addToEnd(Node) method very simply.
public void addToEnd(Node node) {
node.setPrevious(tail);
tail.setNext(node);
tail = node;
}
If you want to be able to iterate through the list in both directions using 'getNext()' and 'hasNext()', you could provide a forward or reverse Iterator implementation. There are examples of how to create Iterators floating around. The first answer on this question has an example of a reverse iterator.
The basic idea is just the same as adding at the beginning of the list.
We can split it up into two parts:
adding the item to the list
updating the head/tail-node of the list
In pseudocode:
node to_insert
//step 1
tail.setNext(to_insert)
to_insert.setPrevious(tail)
//step 2
tail = to_insert
I need to remove() from a (custom) LinkedList, setting last iterated item to null. There are two cases, one where prev is head, one where it is not. The nodes are next, prev and head. I was wondering if you spot any problems in the following code? Do I need to set the iterator variables in between? (or will emptying prev do the trick?) Thanks guys!
public void remove() {
if(prev == null) {
throw new IllegalStateException();
}
else {
if(prev == head){
head = head.next;
prev = null;
sizeOfList--;
}
else {
prev = null;
sizeOfList--;
}
}
}
In case your previous is not the head(second case in your code) you should fix the next link of the previous node before prev. Otherwise it will be left pointing to prev.
I have a project for computer science class and have everything done except for one method. The delete method. Basically I am making a linked list from user input and I need to be able to delete all nodes (which is done) and delete a single specified node. So I need to search through the list of nodes find the one to delete and delete it. Anything that can help is appreciated. If you have a solution please offer an explanation as I am trying to learn and just solve the problem.
I'm not going to give you the GUI because I don't think it is necessary but here is the node class.
public class MagazineList {
private MagazineNode list;
public MagazineList(){
list = null;
}
public void add(Magazine mag){
MagazineNode node = new MagazineNode(mag);
MagazineNode current;
if(list == null) {
list = node;
}
else {
current = list;
while(current.next != null)
current = current.next;
current.next = node;
}
}
public void insert(Magazine mag) {
MagazineNode node = new MagazineNode (mag);
// make the new first node point to the current root
node.next=list;
// update the root to the new first node
list=node;
}
public void deleteAll() {
if(list == null) {
}
else {
list = null;
}
}
public void delete(Magazine mag) {
//Delete Method Goes Here
}
public String toString(){
String result = " ";
MagazineNode current = list;
while (current != null){
result += current.magazine + "\n";
current = current.next;
}
return result;
}
private class MagazineNode {
public Magazine magazine;
public MagazineNode next;
public MagazineNode(Magazine mag){
magazine = mag;
next = null;
}
}
}
UPDATE
Here is the method I put together and it goes through the first part into the while loop and never recognizes the same item in the list. I used the exact same thing for the input and delete methods yet it will not recognize it. Any help is appreciated.
public void delete (Magazine mag) {
MagazineNode current = list;
MagazineNode before;
before = current;
if(current.equals(mag)){
before.next = current;
System.out.println("Hello");
}
while ((current = current.next)!= null){
before = current.next;
System.out.println("Hello Red");
if(current.equals(mag)){
current = before;
System.out.println("Hello Blue");
}
}
}
Without spoon feeding you the answer. deletion is a bit like removing one link of a chain - you cut out the link and join up the two (new) ends.
So, deleting "B" would mean changing
A --> B --> C --> D
to this
A --> C --> D
In pseudo code, the algorithm would be:
start the algorithm with the first node
check if it is the one you want to delete
if not, go to the next node and check again (go back to the previous step)
if so, make the next node of the previous node the next node of this node
remove the reference from this node to the next node
public void delete (Magazine mag) {
MagazineNode current = this.list;
MagazineNode before;
//if is the first element
if (current.equals(mag)) {
this.list = current.next;
return; //ending the method
}
before = current;
//while there are elements in the list
while ((current = current.next) != null) {
//if is the current element
if (current.equals(mag)) {
before.next = current.next;
return; //endind the method
}
before = current;
}
//it isnt in the list
}
the comments should explains whats happening
All you need to do is search through the list, keeping track of where you are. When the node you want to delete is in front of you, set the current node's "next" to the one after the one you want to delete:
for(Node current = list; current.next() != null; current = current.next()){
if(current.next().magazine().equals(toDelete)) current.setNext(current.next().next());
}
Something like that.
Hope that helps!
Here's the linked list code
public class LList
{
protected int size;
protected DNode tail, header;
public LList()
{
size = 0;
tail = null;
header = tail;
}
public void addDNode(DNode v)
{
// means list is empty, so add first element
if (header == null)
{
header = v;
tail = header; // first element so (head == tail)
}
else
{
tail.setNext(v);
v.setPrev(tail);
v.setNext(null);
tail = v;
}
size++;
}
and the remove node method that's the problem
public DNode removeDnode(DNode current)
{
if(current.nextNode() == null)
{
DNode previous = current.prevNode();
previous.setNext(null);
current.setPrev(null);
}
else if (current.prevNode() == null)
{
DNode next = current.nextNode();
next.setPrev(null);
current.setNext(null);
}
else
{
DNode next = current.nextNode();
DNode previous = current.prevNode();
previous.setNext(next);
next.setPrev(previous);
current.setPrev(null);
current.setNext(null);
}
size = size - 1;
return current;
}
The problem is that when I use previous.setNext(null); it won't let me add a node again which I think it has something to do with the header and tail.
however when i use previous.setNext(tail); it doesn't seem to remove it from the list??
Aren't you forgetting to point the tail of the list to the previous element when you remove the last?
if(current.nextNode() == null) {
DNode previous = current.prevNode();
previous.setNext(null);
current.setPrev(null);
tail = previous; //isn't this missing?!
}
Problem is, if you remove tail, you must move it. So try
DNode previous = current.prevNode();
previous.setNext(null);
current.setPrev(null);
tail = previous;
You have the same problem with the next—but I believe once you're done with tail, you'll fix it easily.
Handling remove from a doubly-linked list is VERY tricky. When you see a bug while testing, don't just make a "quick fix" -- go back and understand precisely why it didn't work, and understand how your intended fix will modify all behaviors, not simply the one that's currently misbehaving.
If you think about it carefully you can make it work with a minimum of problems. If you don't think carefully you'll be chasing your tail for hours.