Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: iterators & linked lists in java question

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default iterators & linked lists in java question

    Hello, I am trying to get used to working with iterators, and I've lost my way once again. My code is below and I've included my compiler errors first. I know there are other issues with my code which I will clear up later (I lumped in the node class as you see yet unedited) BUT, I am mainly interested in getting this program to compile first. Thank you in advance for any helpful suggestions.

    Compiler errors:

    >>> javac PrincessListDemo.java
    PrincessListDemo.java:8: cannot find symbol
    symbol : method iterator()
    location: class PrincessList
    PrincessList.ListIterator itr = list.iterator();
    ^
    .\PrincessList.java:103: cannot find symbol
    symbol : constructor PrincessNode(java.lang.String,<nulltype>)
    location: class PrincessList.PrincessNode
    previous.link = new PrincessNode(newData, null);
    ^
    .\PrincessList.java:105: cannot find symbol
    symbol : method addToStartOfList(java.lang.String)
    location: class PrincessList
    PrincessList.this.addToStartOfList(newData);
    ^
    .\PrincessList.java:108: cannot find symbol
    symbol : constructor PrincessNode(java.lang.String,PrincessList.Princes sNode)
    location: class PrincessList.PrincessNode
    PrincessNode temp = new PrincessNode(newData, position);
    ^
    4 errors

    import java.util.NoSuchElementException;
    import java.io.Console;
    import java.util.*;
     
    public class PrincessList				//here is the linked list class
    {
    	private class PrincessNode			//here is the self-contained (no separate file) inner node class
    	{
    			private String name;		//suitor's name
    			private int count;			//
    			private PrincessNode link;	//Instance Variable of type PrincessNode1 that is named link
     
    			public PrincessNode()
    			{
    				link = null;
    				name = null;
    				count = 0;
    			}
    			public PrincessNode (String newName, int newCount, PrincessNode linkValue)
    			{
    				setData(newName, newCount);
    				link = linkValue;
    			}
     
    			//below here to:
    			public void setData(String newName, int newCount)
    			{
    				name = newName;
    				count = newCount;
    			}
    			public void setLink(PrincessNode newLink)
    			{
    				link = newLink;
    			}
    			public String getName()
    			{
    				return name;
    			}
    			public int getCount()
    			{
    				return count;
    			}
    			public PrincessNode getLink()
    			{
    				return link;
    			}
    			//to above here eliminate mutator and accessor methods
    		}
     
     
     
    	public class ListIterator		//an inner class for iterators for PrincessList
    	{
    		private PrincessNode position;
    		private PrincessNode previous;		//previous value of position
     
    		public ListIterator()
    		{
    			position = head; 		//Instance variable head of outer class
    			previous = null;
    		}
     
    		public void restart()
    		{
    			position = head;		//Instance variable head of outer class
    			previous = null;
    		}
     
    		public String next()
    		{
    			if (!hasNext())
    				throw new NoSuchElementException();
     
    			String toReturn = position.name;
    			previous = position;
    			position = position.link;
    			return toReturn;
    		}
     
    		public boolean hasNext()
    		{
    			return (position != null);
    		}
     
    		public String peek()		//Returns the next value to be returned by next(); if false, throws exception
    		{
    			if (!hasNext())
    				throw new IllegalStateException();
    			return position.name;
    		}
     
    		public void addHere(String newData)
    		{
    			if(position == null && previous != null)
    			previous.link = new PrincessNode(newData, null);
    			else if (position == null || previous == null)
    				PrincessList.this.addToStartOfList(newData);
    			else
    			{
    				PrincessNode temp = new PrincessNode(newData, position);
    				previous.link = temp;
    				previous = temp;
    			}
    		}
     
    		public void changeHere(String newData)
    		{
    		    if (position == null)
    		        throw new IllegalStateException( );
    		    else
    		        position.name = newData;
            }
     
            public void delete()
            {
    			if(position == null)
    				throw new IllegalStateException();
    			else if (previous == null)
    			{
    				head = head.link;
    				position = head;
    			}
    			else
    			{
    				previous.link = position.link;
    				position = position.link;
    			}
    		}
     
    	private PrincessNode head;		//this head here private to public
     
    	public ListIterator iterator()
    	{
    		return new ListIterator();
    	}
     
     
     
     
     
     
     
    	public void PrincessList()
    	{
    		head = null;
    	}
     
    	public void addToStartOfList(String suitorName, int suitorCount)	//adds new node to start of the list
    	{
    		head = new PrincessNode(suitorName, suitorCount, head);
    		//above parameters include head because head used to point to the old first node (now adding a new node) so if we use the name head
    		//on the right-hand side of the assignment operator, head will denote a reference to the old first node.
    		//adding and deleting nodes is easiest at the start of the linked list.
    	}
     
    	public boolean deleteHeadNode()		//deletes the first node connecting head with the old second node.
    	{
    		if (head != null)
    		{
    			head = head.getLink();
    			return true;
    		}
    		else
    		return false;
    	}
     
    	public int NumberOfNodesInList()
    	{
    		int count = 0;
    		PrincessNode position = head;
    		while (position != null)
    		{
    			count++;
    			position = position.getLink();
    		}
    		return count;
    	}
     
    	public boolean contains(String name)
    	{
    		return(find(name) != null);
    	}
    	private PrincessNode find(String target)
    	{
    		PrincessNode position = head;
    		String nameAtPosition;
    		while (position != null)
    		{
    			nameAtPosition = position.getName();
    			if(nameAtPosition.equals(target))
    			return position;
    			position = position.getLink();
    		}
    		return null;
    	}
    	public void outputPrincessList()
    	{
    		PrincessNode position = head;
    		while (position != null)
    		{
    			System.out.println(position.getName() + " \t " + position.getCount());
    			position = position.getLink();
    		}
    	}
    }
    }
     
     
     
    import java.io.Console;
     
     
    public class PrincessListDemo
    {
    	public static void main(String[] args) {
    		PrincessList list = new PrincessList();
    		PrincessList.ListIterator itr = list.iterator();
     
    		Console console = System.console();
     
    			System.out.print("\nHello Eve...\n ");
    		    System.out.print("\nHow many suitors are pursuing your hand?\n ");
     
    		String input = console.readLine();
    			int numSuitors = Integer.valueOf(input);
     
    			System.out.print("\nSo there are " + numSuitors + " suitors who have asked for your hand.");
     
    			for (int i=1; i<=numSuitors; i++) {
    				int j=i;
    				System.out.print("\nPlease enter the name of suitor #" + i + ".");
    				String SuitorName = console.readLine();
    				itr.addToStartOfList(SuitorName, j);
    			}
     
    			System.out.print("\nPrincess Eve's list of suitors: ");
    			System.out.print("\n------------------------------:\n");
     
    			itr.restart();
    			while(itr.hasNext())
    				System.out.println(itr.next());
    			System.out.println();
     
     
    			//list.outputPrincessList();
    		}
    	}
    Last edited by helloworld922; June 4th, 2010 at 07:05 PM. Reason: please use [code] tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: iterators & linked lists in java question

    Please use code tags when posting to preserver source code formatting.

    Make sure you have defined the methods and variables in the correct classes so the compiler can find them when it looks for them.
    Last edited by Norm; June 4th, 2010 at 11:28 AM.

Similar Threads

  1. Lists of Sets and Sets of Lists
    By Newoor in forum Collections and Generics
    Replies: 2
    Last Post: December 8th, 2009, 08:13 PM
  2. Constructors, Hash Tables, & Linked Lists
    By illusion887 in forum Collections and Generics
    Replies: 2
    Last Post: December 3rd, 2009, 03:46 AM
  3. Simple linked list
    By Koren3 in forum Collections and Generics
    Replies: 10
    Last Post: November 2nd, 2009, 03:33 AM
  4. circular linked list
    By student123xyz in forum Collections and Generics
    Replies: 4
    Last Post: August 19th, 2009, 10:40 AM
  5. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM

Tags for this Thread