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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:49:14+00:00 2026-05-31T08:49:14+00:00

Here is my code…. package calendar; import java.util.*; /** * This class represents a

  • 0

Here is my code….

 package calendar;

    import java.util.*;

    /**
     * This class represents a simple implementation of a sorted singly-linked list.
     * Elements added to the list are inserted in sorted order based on an specified
     * Comparator object.
     * <br><br>
     * 
     * This class relies on two classes: MyIterator and MyListNode. We have implemented
     * the MyListNode class for you, but you are responsible for the implementation
     * of the MyIterator class.  Notice these are inner classes defined within the 
     * MySortedLinkedList class.  As a result they may access each other's fields even
     * if they are private.  You are required to use the MyListNode class to implement
     * the nodes of your linked list.
     * <br><br>
     * 
     * Feel free to add any methods to the MySortedLinkedList class you consider are 
     * necessary to implement your project.  <b>However, keep in mind that there are two 
     * methods you are required to implement: an add method returning void (adds an element 
     * to the list keeping the list sorted) and a remove method returning void (removes 
     * any element(s) from the list that are equal to the parameter).</b>  
     * 
     * The JUnit public tests provide an example of using the MySortedLinkedList class.
     * <br><br>
     * 
     * You may not use the Java API LinkedList or ArrayList classes during the implementation
     * of the MySortedLinkedList class.
     *  
     * @author Dept of Computer Science, UMCP
     */

    public class MySortedLinkedList<T> implements Iterable<T> {
        private final Comparator<T> comparator; 
        MyListNode<T> head;
        public int counter = 0;
        /**
         * Creates an empty list that is associated with the specified comparator.
         * @param comparator
         */
        public MySortedLinkedList(Comparator<T> comparator) {
            //throw new UnsupportedOperationException("You must implement this method.");
            head = null;
            this.comparator = comparator;
        }

        /**
         * This class represents a linked list node. 
         * You should not modify this class. 
         * 
         * Because MyListNode is an inner class of MySortedLinkedList,
         * methods in MySortedLinkedList may access fields of MyListNode
         * directly even though they are private.
         * 
         * You may use the default constructor for MyListNode, which
         * initializes both val and next to null.
         * 
         * @param <V>
         */
        private class MyListNode<V> {
            private V val;
            private MyListNode<V> next;
        }



        /**
         * Inserts the specified element at the correct position in the sorted list.
         */
        public void add(T element) {
            MyListNode<T> n = new MyListNode<T>();
            n.val = element;
            n.next = head;
            head = n;

        }

        /**
         * Returns the element at the specified position in this list.
         */
        public T get(int index) {
            //throw new UnsupportedOperationException("You must implement this method.");
            int place = 1;
            MyListNode x = head;
            if(x==null){
                return null;
            }
            while(place<index){
                x=x.next;
            }
            return (T) x.val;
        }

        /**
         * Removes any elements matching given element
         */
        public void remove(T v) { 
            //throw new UnsupportedOperationException("You must implement this method.");
            MyListNode prev = null, curr = head;
            while (curr != null) {
                if (curr.val.equals(v)) {
                    if (curr == head)
                        head = head.next;
                    else{
                        prev.next = curr.next;
                        prev = curr;
                        curr = curr.next;
                    }

                } else {
                    prev = curr;
                    curr = curr.next;
                }
            }
        } 

        public int size() {
            //throw new UnsupportedOperationException("You must implement this method.");
            MyListNode x = head;
            if(x==null){
                return 0;
            }
            while(x.next!= null){
                counter = counter + 1;
                x = x.next;
            }
            return counter;
        }

        public boolean isEmpty() {
            return head==null;
        }

        /**
         * Returns an iterator over the elements in this list (in proper sequence).  
         * @return iterator over the list
         */
        public Iterator<T> iterator() {
            MyIterator<T> something = new MyIterator<T>(head);
            return something;
        }

        /**
         * This class implements an iterator over the list.
         * You must implement this class. 
         * @param <E>
         */
        private class MyIterator<E> implements Iterator<E> {
            //private int counter1 = size();
            //@SuppressWarnings("unchecked")
            MyListNode<E> newHead = (MyListNode<E>) head;
            private Iterator<E> myIt;

            /**
             * Defines an iterator based on the start node provided.
             * @param start
             */
            private MyIterator(MyListNode<E> start) {
                //throw new UnsupportedOperationException("You must implement this method.");
                myIt = new MyIterator<E>(start);
            }

            /**
             * Returns true if there is another element available
             * @return true if there is another element and false otherwise
             */
            public boolean hasNext() {
                //throw new UnsupportedOperationException("You must implement this method.");
                if(head==null){
                    throw new NoSuchElementException();
                }else{
                    if(myIt.hasNext()||counter>1){
                        return true;
                    }else{
                        return false;
                    }
                }
            }

            /**
             * Returns the next element
             * @return next element
             */
            public E next() {
                //throw new UnsupportedOperationException("You must implement this method.");
                if(hasNext()){
                    newHead = newHead.next;
                    return (E) newHead;
                }
                return null;
            }

            /**
             * Removes an element from the list.  You do not need to implement this method.
             */
            public void remove() {
                throw new UnsupportedOperationException("You do NOT need to implement this method");
            }
        }
    }

The error comes at myIt = new MyIterator<E>(start); in the private MyIterator constructor. I have no idea why this is causing a stack overflow. Any ideas? All the sections have comments saying what the goal is for each method. A few days ago I was just getting a NullPointerException, but I changed something and now get a StackOverflow with the error repeating on the line I stated.

The error comes from this test:

public void testListEmpty() {
        MySortedLinkedList<Activity> myList = new MySortedLinkedList<Activity>(new ActivityComparator());
        Iterator<Activity> iterator = myList.iterator();
        assertTrue(iterator.hasNext() == false);
    }
  • 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-31T08:49:16+00:00Added an answer on May 31, 2026 at 8:49 am
    private MyIterator(MyListNode<E> start) {
           //throw new UnsupportedOperationException("You must implement this method.");
           myIt = new MyIterator<E>(start);
    }
    

    A constructor that recursively creates a new instance of itself will of course never cleanly terminate and will quickly overrun either the heap or the stack.

    Why do you think MyIterator needs to be backed by another MyIterator? Why not just use this instead of myIt?

    Perhaps you are confused as to what a constructor does. The constructor is called when you use the new operator to do any custom initialization. It doesn’t need to create the instance itself.

    See also

    Java Tutorials – Providing Constructors for your Classes

    Edit

    If you want to initialize MyIterator with start, you just need to create a field for start:

    private final MyListNode<E> start;
    
    private MyIterator(MyListNode<E> start) {
           this.start = start;
    }
    

    Then you can use start in your other methods of MyIterator.

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

Sidebar

Related Questions

I'm trying to match this block here /code\ int foo(string $bar, int $bleh); Simple
Ok the error is showing up somewhere in this here code if($error==false) { $query
Here my code: $(document).ready(function() { $('#mid_select').live('click', function(e){ $('#middle').load( $(this).attr('href') + ' #middle'); var page
Here is code from a tutorial in A Byte of Python: import sys filename
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
Here is code example class A{ int i; public: A(int i) : i(i) {}
enter code here I've created a simple code for mount virtual disk into system
Here the code on this concern: while (true) { Console.WriteLine(start + DateTime.Now); ParallelOptions options
Here my code <a href=http://linkurl class=link title=sometitle> text link <span class=hidden-tooltip-data style=display: none;> <a
Here is code from MSDN . I don't understand why the work isn't just

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.