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 3354396
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:16:04+00:00 2026-05-18T02:16:04+00:00

yes this is one of my homework projects – to implement a Circular Linked

  • 0

yes this is one of my homework projects – to implement a Circular Linked List based on Singly Linked List. It’s a pretty simple and the code is easy to read. All my attributes are public to avoid getters and setters and private business. For the purposes of this project public will suffice.

I initialized my nItems counter (# of items in the list) and Link head right in the attribute field, but I will change it later by initializing it inside of the constructor.

My step() method doesn’t seem to work at all. My compiler just freezes for a moment and then nothing shows up. This is how step() method should work if I call it 4 times:

List: 40 80 30 20 10 70 50 
List: 80 30 20 10 70 50 40 
List: 30 20 10 70 50 40 80 
List: 20 10 70 50 40 80 30 

Find() method works fine, that is as long as the value we are searching for is inside the Linked List. If it’s not, it will keep on going forever. If this is my list, this what happens to find() method if you search for a value that isn’t there (I debugged it step by step):

70 60 50 40 30 20 10 10 10 10 10 10 10 10...and 10 forever

Cause: If they key value we are searching for isn’t there, it will never get out from the while loop, hence forever repeating current = current.next.

while(current.dData != key) {
current = current.next;

My delete method says it deleted value 60 just as I wanted to, but this what I get:

Deleted link with key 60
70 60 40 30 20 10       // lie! it deletes 50 instead of 60

Please also take a look at my display() and my insert() methods. They look fine to me, but I could be wrong and it could be the source of all problems I am having with find() and delete() methods.

Thank you so much in advance!!!

class Link {
    public int dData;
    public Link next;

    public Link(int dd) {
        dData = dd;
    }

    public void displayLink() {
        System.out.print(dData + " ");
    }
}

class CircList {
    public Link head = null;
    int nItems = 0;

    public boolean isEmpty() {
        return (nItems == 0);
    }
    // INSERT
    public void insert(int key) { 

        Link current = new Link(key);
        if(isEmpty())
            head = current;

        current.next = head;
        head = current;
        nItems++;
    }
    // STEP
    public void step() {
        Link current = head;
        do {
            current = current.next;
        } while(current != head);
    }
    // FIND
    public Link find (int key) {
        Link current = head;
        while(current.dData != key) {
            current = current.next;
        }
        return current;
    }
     // DELETE
    public Link delete(int key) {
    Link current = head;
    while(current.dData != key) {
        current = current.next;
    }
    if(current == head)
        head = head.next;
    else {
        current.next = current.next.next;
        nItems--;
    }
    return current;
}
    // DISPLAY
    public void displayList() {
        Link current = head; // start at beginning
        int counter = nItems;
        while(true) {
            if(counter != 0) {
                current.displayLink();
                current = current.next; // move to next link
                counter--;
            }
            else 
                break;
        }
    }
}

class CircListApp {
    public static void main(String[] args) {
    Link f, d;
    CircList theList = new CircList();


    theList.insert(10);      // insert items
    theList.insert(20);
    theList.insert(30);
    theList.insert(40);
    theList.insert(50);
    theList.insert(60);
    theList.insert(70);

    //System.out.println(theList.nItems + " ");
    theList.displayList();              // display list

    System.out.println("");

    f = theList.find(30);               // find item
    if( f != null)
       System.out.println("Found link with key " + f.dData);
    else
       System.out.println("Can't find link with key 30");

//    theList.step();

//
//    System.out.println("Inserting link with key 80");
//    theList.insert(80);
//    theList.displayList();              // display list
//
    d = theList.delete(60);             // delete item

    if( d != null )
       System.out.println("Deleted link with key " + d.dData);
    else
       System.out.println("Can't delete link with key 60");
    theList.displayList();              // display list
//
//    f = theList.find(99);               // find item
//    if( f != null)
//       System.out.println("Found link with key " + f.dData);
//    else
//       System.out.println("Can't find link with key 99" );
//    theList.displayList();              // display list
//
//    d = theList.delete(13);             // delete item
//    if( d != null )
//       System.out.println("Deleted link with key " + d.dData);
//    else
//       System.out.println("Can't delete link with key 13");
//    theList.displayList();              // display list
//
//    System.out.println("Stepping through list");
//    for(int j=0; j<theList.getSize; j++)
//       {
//       theList.step();
//       theList.displayList();
//       }
//
//    System.out.println("Will delete and step one by one");
//    while(theList.isEmpty() == false)
//       {
//       theList.delete();
//       theList.step();
//       theList.displayList();
//       }
    }  // end main()

}
  • 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-18T02:16:05+00:00Added an answer on May 18, 2026 at 2:16 am

    To your “step” method:
    You create a local reference called current, which initially points to head. After

    current = current.next
    

    it references the successor of head. And in the next step, it references the successor of that, and so on. But it’s a local reference, so you don’t change anything in the list.

    What you have to do, as I deduce from your desired output, is as simple as

    if( head != null && head.next != null )
        head = head.next
    

    Next: For your find-method:
    Well, from your loop condition, it’s quite obvious that it will run forever if the key is not in the list, because this is your only breaking condition. You should think of a mechanism that makes it stop after you’ve looked at every item in the list. Hint: Look at what you’ve done in your version of the step() method.

    To your delete-method:
    The first part is (partially) okay (it has the same infinite-loop problem as your find-method).
    What you do is iterate through the list until the data of current is equal to the key. That’s fine.

    But now, current is a reference to the element you want to delete. But what you do is setting current.next to current.next.next, which means that you are deleting the successor of current, instead of current itself! In your loop where you search for the key, you must keep track of the predecessor, so that you can change predecessor.next to current.next, which is what you really want to do.

    And then, of course, you must check if it’s the head you’re deleting, and if that’s the case, you must set it to either the predecessor or the successor of it.

    Finally, I see a problem with your insert() method! I don’t see how this can produce a circular linked list, because you let the newly inserted element point to the head and make it the new head, but nothing points to it. So you only create a singly linked list? You insert a new item “before” the head ( current.next = head ) but then you should also have the former predecessor of head now point to the new item, current!

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

Sidebar

Related Questions

I have a generic method with this (dummy) code (yes I'm aware IList has
First yes, this is a homework project for my Perl class. I am not
this one is not homework, it just the fact that i've been out of
yes, this is a homework-type question but could you please help me out? In
I need a quick yes/no answer on this... Is it possible to get a
When I asked this question I got almost always a definite yes you should
rbnResubmission.Items.FindByValue(Yes).Attributes.Add(onclick, getCheckedRadioFieldResubmission(this)); rbnResubmission.Items.FindByValue(No).Attributes.Add(onclick, getCheckedRadioFieldResubmission(this)); So I have these click events for showing rows in
First, yes I know about this question , but I'm looking for a bit
I'm currently displaying a UIViewController like this: [[self navigationController] presentModalViewController:modalViewController animated:YES]; and hiding it
I'm trying to display a table full of twitter statuses (yes, this is the

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.