Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8992581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:59:19+00:00 2026-06-15T22:59:19+00:00

Hey I’m currently stuck on the reverse method of my DoublyLinkedList. Everything is working

  • 0

Hey I’m currently stuck on the reverse method of my DoublyLinkedList. Everything is working fine (somehow) except for the reverse method. I’m not receiving any errors – System.out.println(list.reverse()) simply has no output.

Any suggestions? Thank you very much in advance. 🙂

Okay: I have edited my code now. So far everyhing is working correctly. However, the recursive method simply prints the list in the same order, instead of actually reversing it.

Updated Code:

public class DoublyLinkedStringList {

private String content;
private DoublyLinkedStringList prev;
private DoublyLinkedStringList next;

public DoublyLinkedStringList(String info) {
    content = info;
    prev = null;
    next = null;
}

private DoublyLinkedStringList(String content, DoublyLinkedStringList prev, DoublyLinkedStringList next) {
    this.content = content;
    this.prev = prev;
    this.next = next;
}

public DoublyLinkedStringList prepend(String info) {
    DoublyLinkedStringList newNode = new DoublyLinkedStringList(info);
    prev = newNode;
    newNode.next = this;

    return newNode;
}

public DoublyLinkedStringList delete(int index) {
    DoublyLinkedStringList curr = this;

    if (index == 0) {
        next.prev = null;
        return next;
    }

    for (int i = 0; i < index; i++) {
        curr = curr.next;
    }

    curr.prev.next = curr.next;
    if (curr.prev.next != null) {              
        curr.prev.next.prev = curr.prev;
    }
    return this;
}

public DoublyLinkedStringList reverse() {
    DoublyLinkedStringList currNode = this;

    while (currNode != null) {
        DoublyLinkedStringList temp = currNode.next;
        currNode.next = currNode.prev;
        currNode.prev = temp;

        if (currNode.prev != null) {
            currNode = currNode.prev;
        }
    }

    return this;
}

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();

    for (DoublyLinkedStringList currNode = this; currNode != null; currNode = currNode.next) {
        sb.append(currNode.content);
        if (currNode.next != null) {
            sb.append(", ");
        }
    }
    return sb.toString();
}

public static void main(String argv[]) {
    DoublyLinkedStringList list = new DoublyLinkedStringList("Testliste");
    list = list.prepend("6");
    list = list.prepend("5");
    list = list.prepend("4");
    list = list.prepend("3");
    list = list.prepend("2");
    list = list.prepend("1");
    list = list.prepend("0");

    list = list.delete(1);
    System.out.println(list);

    list = list.reverse();
    System.out.println(list);
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:59:20+00:00Added an answer on June 15, 2026 at 10:59 pm

    Normally I would implement the interface Iteratable and use an Iterator to reverse the list but I kept my revision in line with your current model. I changed the return types of the Node‘s getNext() and getPrev() methods to be dependent on the forward variable. Now the list never changes linkage when “reversed” but it is traversed in reverse order via the variable getNext() and getPrev() behavior.

    IDEONE link to code

    Consider this edit:

    class DoublyLinkedStringList {
    
    private Node head, tail;
    boolean forward;
    
    /**
     * Diese Klasse repraesentiert einen Knoten in der Doubly Linked List der
     * Klasse
     * <code>DoublyLinkedStringList</code>
     *
     */
    private class Node {
        private String content;
        private Node next;
        private Node prev;
    
        public Node(String content) { this.content = content; }
    
        public Node(String content, Node next) {
            this.content = content;
            if(forward) { this.next = next; }                     //EDITED
            else        { this.prev = next; }                     //EDITED
        }
    
        public Node getNext() { return (forward) ? next : prev; } //EDITED
        public Node getPrev() { return (forward) ? prev : next; } //EDITED
    
        public void setNext(Node next) {
            if(forward) { this.next = next; }                     //EDITED
            else        { this.prev = next; }                     //EDITED
        }
    
        public void setPrev(Node prev) {
            if(forward) { this.prev = prev; }                     //EDITED
            else        { this.next = prev; }                     //EDITED
        }
    }
    
    public DoublyLinkedStringList() {
        this.head = null;
        this.tail = null;
    }
    
    public Node prepend(String info) {
        Node newNode = new Node(info);
        newNode.setPrev(null);
        newNode.setNext(getHead());
        if(newNode.getNext()!=null) { 
          newNode.getNext().setPrev(newNode);                     //EDITED
        } 
        if(forward) { head = newNode; }                           //EDITED
        else        { tail = newNode; }                           //EDITED
        if(getTail() == null) {                                   //EDITED
          if(forward) { tail = newNode; }                         //EDITED
          else        { head = newNode; }                         //EDITED
        }
        return head;
    }
    
    public Node delete(int index) {
        Node currNode = getHead();
        int count = 0;
    
        if (index == 0) {
            if(forward) { head = head.next; }                     //EDITED
            else        { tail = tail.prev; }                     //EDITED
            return head;
        }
    
        while (currNode != null) {
            if (count + 1 == index) {
                currNode.next.prev = currNode.prev; 
                currNode.prev.next = currNode.next;               //EDITED
                break;
            }
            currNode = currNode.getNext();                        //EDITED
            count++;
        }
        return currNode;
    }
    
    private Node next() {
        Node currNode = head;
    
        if (forward) {
            return currNode.getNext();
        } else {
            return currNode.getPrev();
        }
    }
    
    public Node getHead() { return (forward) ? head : tail; }     //EDITED
    public Node getTail() { return (forward) ? tail : head; }     //EDITED
    public DoublyLinkedStringList reverse() { forward = !forward; return this; }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        //EDITED LOOP STRUCTURE
        for (Node currNode = getHead(); currNode != null; currNode = currNode.getNext()) {
            sb.append(currNode.content);
            if (currNode.getNext() != null) {
                sb.append(", ");
            }
        }
        return sb.toString();
    }
    
    public static void main(String argv[]) {
        DoublyLinkedStringList list = new DoublyLinkedStringList();
        list.prepend("6");
        list.prepend("5");
        list.prepend("4");
        list.prepend("3");
        list.prepend("2");
        list.prepend("1");
        list.prepend("0");
        list.delete(3);
        System.out.println(list);
        System.out.println(list.reverse());
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

* *Hey i was working on an application which converts any basenumber like (2,8,10,16,etc)
Hey all i have a JSFiddle that i have been working on with trying
Hey there, Now im working this base on dmitko tutorial on extending django-registration post
Hey, I want to know how to connect databases with C++? Any cross-platform solution
Hey, I'm currently trying to send an image file to a web server using
Hey guys I'm working with this forum, I want to hide the categories list,
Hey all. I'm working on a project for school where I need to pass
Hey all, I was working on a recursive generator to create the fixed integer
Hey I have stuck on how to convert my int value to NSString and
hey guys i know this may sound stupid, but i am stuck with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.