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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:44:33+00:00 2026-05-26T15:44:33+00:00

The next() and remove() i’m having trouble with. For the next() i want to

  • 0

The next() and remove() i’m having trouble with.
For the next() i want to return the next element in the list.
For the remove() Removes from the underlying collection the last element returned by the iterator (optional operation).

I understand what i’m suppose to do, but i’m having trouble writing the code. Could someone give me some hints? or explain to me what i’m suppose to do.

Here’s my code, its a big piece of mess.

    class DoublyLinkedList12Iterator implements Iterator
    {
    private Node cursor;
    private Node lastNodeReturned;
    private Node cursorNext = cursor._next;
    private int nextIndex = 0;
    // private int prevIndex = -1;
    private boolean _hasNextBeenCalled = false;
    private int _currentIndex = -1;


    //Returns true if the iteration has more elements
        public boolean hasNext() {
            return _currentIndex < (_size -1);
        }


    //returns the next element in the iteration
        public Object next() 
        {


        _currentIndex++;
        _hasNextBeenCalled = true;

        /*if(nextIndex == 0)
        {
           nextIndex++;
           return _head._next;
        }*/


        if(cursor != null)
        {
           cursor = cursor._next;
        }
        else
        {
           throw new NoSuchElementException();
        }


        //cursor = cursor._next;
        lastNodeReturned = cursor;
        return cursor._data;




        /*prevIndex--;
        nextIndex++;
        return cursor;

        this._prev = this._next;
        if(this._next != null);
        return the first node  

                Node cursor = _head;
                    for(int i = _currentIndex; i < _size ; i++)
                    {
                    cursor = cursor._next;
                    }
                    return cursor._data;
        */
            }


        public void remove() 
        {


        if(!_hasNextBeenCalled)
        {
           throw new IllegalStateException();
        }

        _hasNextBeenCalled = false;

        if(cursor == lastNodeReturned)
        {
           cursor = cursor._next;
        }
        else
        {
           nextIndex--;
        }

        lastNodeReturned._prev = lastNodeReturned._next;

        _size--;



            }

    }
  • 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-26T15:44:33+00:00Added an answer on May 26, 2026 at 3:44 pm
    public T next() {
    if (nextnode == null)
        throw new NoSuchElementException();
    currentnode = nextnode;
    previousnode = currentnode.previous;
    nextnode = currentnode.next;
    return currentnode.element;
    }
    
    public void remove() {
    if (previousnode != null)
        previousnode.next = nextnode;
    if (nextnode != null)
        nextnode.previous = previousnode;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why is my remove method removing every element from my Doubly Linked List? If
I remove some files from project and now then project builds i see next
I want to select the next element of this in each() function. check out
Need to remove all the code from the start tag to the next I've
I would like remove next element of a selected element. Here, I would like
So i have a linked list that I want to be able to remove
errorPlacement: function(error, element) { if (element.next().attr(id) == errordiv) { element.next().remove(); } var msg =
Trying to remove the last child of an element only if the class =
I have next package structure webService.ear -- dependency.jar -- some.class Can I remove some.class
Next to file menu i want to add a search menu that will have

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.