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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:30:38+00:00 2026-05-28T19:30:38+00:00

Hi I am making a linked list data structure and within the list I

  • 0

Hi I am making a linked list data structure and within the list I define an iterator inner class. I am currently having trouble with the remove method. The functionality that I want is that it cannot be called on the if the next has not been called or the current element in the list has been removed already. Here’s what I have.

private class ListItr implements java.util.Iterator<E>{
private Node<E> currentNode;
private Node<E> nextNode;
private Node<E> previousNode;


public ListItr(List<E> theList){
  previousNode = new Node<E>(null);
  currentNode = new Node<E>(null);
  nextNode = theList.head;
  currentNode.setSuccessor(nextNode);
}

public boolean hasNext(){        
  return nextNode != null;
}

public E next(){
  if(nextNode == null)
    throw new NoSuchElementException();

  previousNode = currentNode;
  currentNode = nextNode;
  nextNode = nextNode.getSuccessor();

  return currentNode.getElement();
}

public void remove(){
  if(currentNode == null)
    throw new IllegalStateException();
  nextNode = currentNode.getSuccessor();
  previousNode.setSuccessor(nextNode);
  currentNode = null;

  size--;
}

}

As you can see this will successfully remove the node in the list by splicing around it, setting the current node to null. However if it is called on the first without calling next, it will still run when I do not want it to. I can hack around it by adding a flag nextNotCalled, setting it to true in the constructor, then setting it false when next is called however I feel that that is not the way to go about it…

  • 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-28T19:30:40+00:00Added an answer on May 28, 2026 at 7:30 pm

    If the question is in general how to do it, I’d look at how Josh Bloch and Neil Gafter did it. Look at the class definition for Itr (line 330).

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

Sidebar

Related Questions

I'm making a linked list class and am trying to implement this contains() method.
I am tasked with making a queue data structure in C, as a linked
I want to give a certain linked list to a class I am making.
Im trying to remove a single node from a doubly threaded linked list. What
I'm making a linked list (of structs) in C, but I want to be
I am currently in the thinking process of the data structure I might use
Lets say I have a linked list with a bunch of different data in
I am making a linked list of objects. For some reason when i go
I coded for doubly linked list implementation in C. In that, after making insertion
I store pointers to all instances of class Cell in a linked list. 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.