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

  • Home
  • SEARCH
  • 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 7027961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:19:21+00:00 2026-05-28T00:19:21+00:00

I need circular list of objects. And each one should know which is previous

  • 0

I need circular list of objects. And each one should know which is previous or next. I did this:

class Bus {

    private Bus previous;
    private Bus next;

    public Bus() {
      //anything
    }

    public void setPrevious(Bus bus) {
      this.previous = bus;
    }

    public void setNext(Bus bus) {
      this.next = bus;
    }

    private void someMethod() {
     // if (previous.xxx() && next.xxx()) {
     //   do something
     // } 
    }

}

And I created an array of Bus. After I add all buses into it, I set next and previous of each element. And I feel it’s ugly:D. May you suggest me better way?

  • 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-05-28T00:19:22+00:00Added an answer on May 28, 2026 at 12:19 am

    If you adjust your setNext and setPrevious methods to not only update their instance but also the instance that is set as next and as previous you want have to depend on an external mechanism.

    So let’s say you have initially created Bus A and B. When you call A.setNext( B ), it should also update the previous node of B, without having to call B.setPrevious( A ). Similar as when you add something to a LinkedList in Java, you do not have to manually set the link between the last object and the object you just added. Something like

    public void setPrevious(Bus bus) {
      this.previous = bus;
      if ( bus.next != this ){
        bus.next = this;
      }
    }
    

    Of course then you still have to consider the scenario where the bus is already contained in another List that you have to update that List as well.

    Therefore, the suggestion to separate the nodes from the actual bus instances as suggested in one of the other responses is a better idea. This allows you to add busses to multiple lists, and makes it probably easier to write your circular list (or just use an available implementation for the list). This is also better OO design as you can reuse the list you wrote.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help on implementing a circular progress bar like this: How should I
I need a circular linked list, so I am wondering if LinkedList is a
I want a simple class that implements a fixed-size circular buffer . It should
I need to display a tree bound to this kind of hierarchical objects: public
For a uni assignment I need to create a circular list of up to
is it possible to make a circular JFrame without the need to the Class
Given a list of classes inheriting from this base: class Plugin(object): run_after_plugins = ()
Is there something similar to a circular linked list available in Cocoa? I know
Why exactly do we need a Circular Linked List (singly or doubly) data structure?
I'm implementing an audio track class and I'm in need of a good circular

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.