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 16 of 16

Thread: I need help with LinkList: Removing and adding

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default I need help with LinkList: Removing and adding

     
    public class LinkList
    {
     
            public class Node
            {
     
                 public Node next;
                 public  Object data;
     
     
                   public Node(Object data)
                   {
                     next = null;
                     this.data = data;
                   }
     
                   public Node(Object data, Node next)
                   {
                     this.next = next;
                     this.data = data;
                   }
     
     
                   public Object getData()
                   {
                     return data;
                   }
     
                   public void setData(Object data)
                   {
                     this.data = data;
                   }
     
                   public Node getNext()
                   {
                    return next;
                   }
     
                   public void setNext(Node next)
                   {
                     this.next = next;
                   }
     
     
            }    
     
     
     
    }


    I'm getting very frustrated with the assignment I'm trying to do. If some one can help me better understand LinkList I would really appreciated. What's going on is that I created a LinkList class. I was going to add methods to it as I go. The first two methods I wanted to focus on is add and remove method. I looked up numerous tutorials I understand what's going but I see I'm not implementing right. Below is my add method that I created. It will be added in the LinkList once I know if complete and right.


     public void addFrist(String newplant, int i)
            {
                Node start,tail,next;
                start = null;
                tail = null;
     
                if( i < 0)
                { 
                    start = new Node(newplant,start) ;
                    tail = start;
                }
     
     
                if( i > 0)
                {
                    next = new Node(newplant,null);
                    tail.setNext(next);
                    tail = next;
                }   
     
     
            }

    This is what I have so far. Before I continue writing code. I wanted to make sure I was iterator through the LinkList right. For example when I add the next node will first node point to my new node and will the new node point to null.(The end of the list)


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: I need help with LinkList: Removing and adding

    First of all, the start node of a linked list should be an instance variable (for example, in your Node class, next and data are instance variables).

    What is the i variable supposed to be?

    An addFirst() method should do 3 things:
    1. Store the current value of the start node in a new variable
    2. Set the start node to the new value
    3. Set next node for the new start node to the old start node
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I need help with LinkList: Removing and adding

    The best advice I can give you is to draw out a ton of pictures showing how the data structure works. Each node (draw a circle) contains a value (write the value inside the circle) and points to some other nodes (draw lines to other nodes). Now how do you have to change the picture to add or delete a node?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need help with LinkList: Removing and adding

    Hello Everyone,

    Thanks for the quick reply. KevinWorkman...I been drawing pictures and diagrams. I know what my end result is but I just don't know what to write in code correctly.

    int I = scan.nextInt();


    StartNode
    [ I ]-NULL


    StartNode
    [ I ]- NULL
    TempNode

    int I = scan.nextInt();

    StartNode nextNode
    [I] -NULL [I]-NULL
    TempNode


    StartNode nextNode
    [I] ------| ------------[I]-NULL
    TempNode

    TempNode.setNext(nextNode)


    StartNode nextNode
    [I] ------| ------------[I]-NULL
    TempNode

    TempNode = nextNode

    That is my pseudo code. The only reason I have an index in my addFirst method is because if the index is greater then 0 I can create a new node. From creating my new node I can link the previous node to the new node and just keep the new node link to null.(The end of the list) Is there a better approach?

    --- Update ---

    Wow I don't know what happen to my format. If you don't understand please let me know.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: I need help with LinkList: Removing and adding

    You should not need the index. You can store a null value.

    If you have never set the start node, it will be set to null. You can create a new node, replace the start node with the new node, and set the new node's next to the old start node. It doesn't matter if that old value was null. node.setNext(null); is perfectly valid and legal code, so it doesn't matter if the previous start node was null.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need help with LinkList: Removing and adding

    Quote Originally Posted by aussiemcgr View Post
    You should not need the index. You can store a null value.

    If you have never set the start node, it will be set to null. You can create a new node, replace the start node with the new node, and set the new node's next to the old start node. It doesn't matter if that old value was null. node.setNext(null); is perfectly valid and legal code, so it doesn't matter if the previous start node was null.

    Well if I do that will it be possible to iterator through my null list? I want to eventually add a remove method and some other methods. I just want to make sure I'm able to access the content of the nulllist.

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: I need help with LinkList: Removing and adding

    The whole list wouldn't be null. A linked list only has a reference to the root node. All other elements of the list need to be accessed by starting at the root and going through each "next" node. If the root node is null, the list is empty. If any given node's next value is null, you have reached the end of the list.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #8
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need help with LinkList: Removing and adding

    Quote Originally Posted by aussiemcgr View Post
    If any given node's next value is null, you have reached the end of the list.
    So what your saying is if I create null1 assigned to null and then create null2 and assign it to null. I can then use the "next" method to reference to my new null1 value to null2 value. Which will create my LinkList? Is that right?

    --- Update ---

    I think this is what your looking for. I wrote this earlier. There is probably some code I don't even need. But the add method looks right. As soon as the newnull is created it goes to next nullobject or the end of the list if there is only one null object.

    public class LinkList
    {
     
     
     
        public Node add(Object data)
        {
            return new Node(data);
     
        }
     
        public void print(Node start)
        {
                Node current = start.getNext(); 
     
                while(current != null)
                {
                    System.out.println(current.getData().toString());
                    current = current.getNext();
     
                }
        }
     
     
     
     
     
            public class Node
            {
     
                 public Node next;
                 public  Object data;
     
     
                   public <Plantclass> Node(Object data)
                   {
                     next = null;
                     this.data = data;
                   }
     
                   public <Plantclass> Node(Object data, Node next)
                   {
                     this.next = next;
                     this.data = data;
                   }
     
     
                   public <Plantclass> Object getData()
                   {
                     return data;
                   }
     
                   public void setData(Object data)
                   {
                     this.data = data;
                   }
     
                   public Node getNext()
                   {
                    return next;
                   }
     
                   public void setNext(Node next)
                   {
                     this.next = next;
                   }
     
     
            }    
     
            public void addFrist(Plantclass newplant, Node next )
            {
                Node newnull;
     
                newnull = new <Plantclass>Node(newplant,null) ;
                newnull = newnull.next;  
            }    
     
     
     
     
    }

  9. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: I need help with LinkList: Removing and adding

    Your print method looks mostly correct, although you not printing the starting node.
    Your add does not. Add should add the item to the end of the list (correct?). So you want to loop through the linked list (like you do with your while loop in your print method) until current.getNext() is equal to null. When you reach that, you know it is the end. So you want to create your new Node and set it to be current's next. By default, the new node's next will be set to null. Obviously, you will need a check to see if start is null (which you should also do in print) before you try to get its next.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  10. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Rain_Maker (December 18th, 2013)

  11. #10
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need help with LinkList: Removing and adding

    Hey I'm back. I made some progress. I know you told me not to use any type of counter but I'm just really stuck on the whole linklist concepts. I guess I'm thinking about arrays and array lists to much. Here is what I have so far.


    import java.io.*;
    import java.util.*;
    import javax.swing.*;
     
    public class CSCFinal
    {
       static LinkList newList = new LinkList();
     
     
        public static void main(String[] args) throws IOException
        {
     
     
        Scanner scan = new Scanner(System.in);
     
         while(true)
         {
            System.out.println(" Hello user, welcome to the student application");
            System.out.println(" Please make a selection below.");        
            System.out.println(" Option 1: Add a plant");        
            System.out.println(" Option 2: Remove a plant");        
            System.out.println(" Option 3: Sort plant"); 
            System.out.println(" Option 4: filter plants"); 
            System.out.println(" Option 5: Save plant file");         
            System.out.println(" Option 6: Load plant file");  
            System.out.println(" Option 7: Analyze plant");                 
            System.out.println(" Option 8: Display plant");
            System.out.println(" Option 0: Exit application");
            System.out.print(" Ans:");
            int ans = scan.nextInt();
     
     
             switch(ans)
             {
     
                case 1://add plant 
               {
                         String iD,name,color,smell,thorns,poisonous,edible,medicinal, flavor, seasonal;
     
                         System.out.println("Option 1: Add Flower?");
                         System.out.println("Option 2: Add Fungus?");
                         System.out.println("Option 3: Add Weed?");
                         System.out.println("Option 4: Add Herb?");
                         System.out.print("Ans:");
                         int answer = scan.nextInt();
     
     
                         switch(answer)
                         {
     
                             case 1:// add flower
                             {
                                 boolean flag = true; 
     
                                 while(flag)
                                 {
     
                                       System.out.print(" Please enter the flower id: ");
                                        iD = scan.next();
     
                                       System.out.print(" Please enter the name of your flower? ");
                                        name = scan.next();
     
                                       System.out.print(" What's the color of your flower?: ");
                                        color = scan.next();
     
                                       System.out.print(" Does your flower have thorns? ");
                                        thorns = scan.next();
     
                                       System.out.print(" Does your flower have a smell? ");
                                        smell = scan.next();
     
                                       Flower newFlower = new Flower(iD,name,color,thorns,smell);
                                       newList.add(newFlower);
                                       flag = false;
                                 }
                                 break;//end of flower
                             }
     
     
                             case 2:// add Fungus
                             {
                                 boolean flag = true; 
     
                                 while(flag)
                                 {
     
     
                                       System.out.print(" Please enter the fungus id: ");
                                       iD = scan.next();
     
                                       System.out.print(" Please enter the name of your fungus? ");
                                       name = scan.next();
     
                                       System.out.print(" What's the color of your fungus?: ");
                                       color = scan.next();
     
                                       System.out.print(" Is your fungus poisonous? ");
                                       poisonous = scan.next();       
     
                                       Fungus newFungus = new Fungus(iD,name,color,poisonous);
                                       newList.add(newFungus);
                                       flag = false;
                                 }
                                 break;
                             } //end of Fungus                
     
     
                             case 3:// add weed
                             {
                                 boolean flag = true; 
     
                                 while(flag)
                                 {
     
                                       System.out.print(" Please enter the weed id: ");
                                       iD = scan.next();
     
                                       System.out.print(" Please enter the name of your weed? ");
                                       name = scan.next();
     
                                       System.out.print(" What's the color of your weed?: ");
                                       color = scan.next();
     
                                       System.out.print(" Is your weed poisonous? ");
                                       poisonous = scan.next();                                         
     
                                       System.out.print(" Is your weed edidble? ");
                                       edible = scan.next();                                        
     
                                       System.out.print(" Is your weed medicinal? ");
                                       medicinal = scan.next();  
     
                                       Weed newWeed = new Weed(iD,name,color,poisonous,edible,medicinal);
                                       newList.add(newWeed); 
                                       flag = false;
                                 }
                                 break;
                             }  //end of weed plant                               
     
     
                             case 4:// add herb
                             {
                                 boolean flag = true; 
     
                                 while(flag)
                                 {
     
                                       System.out.print(" Please enter the herb id: ");
                                       iD = scan.next();
     
                                       System.out.print(" Please enter the name of your herb? ");
                                       name = scan.next();
     
                                       System.out.print(" What's the color of your herb?: ");
                                       color = scan.next();
     
                                       System.out.print(" Does your herb have flavor? ");
                                       flavor = scan.next();                                         
     
                                       System.out.print(" Is your herb medicinal? ");
                                       medicinal = scan.next();                                        
     
                                       System.out.print(" Is your herb seasonal? ");
                                       seasonal = scan.next();                                    
     
                                       Herb newHerb = new Herb(iD,name,color,flavor,medicinal,seasonal);
                                       newList.add(newHerb); 
                                       flag = false;
                                 }
                                 break;
                             }  //end of herb plant                         
     
                         }
                         break;
                }   
     
     
     
              case 2: //remove plant
              {
     
                  System.out.println( " Please enter the plant name you would like to remove.");
                  System.out.print(" Ans:");
                  String key = scan.next();
     
                  for(int i = 0; i < newList.size(); i++)
                  {
                    String y = newList.get(i).toString();
     
                    //if(removePlant.getName().equalsIgnore(s))
                    {
     
                        // newList.get(i).remove();
                        System.out.println(" Plant has been removed");
                     break;
                    }
     
                  }
     
               }
               break;
     
              case 3: //sort student from list
              {
     
     
                  int i, j;
     
                    for(i = 0; i < newList.size(); i++ )
                    {
                        //String currentMinimum = flowerPack[i];
                        int currentMinIndex = i;
     
                        for( j = i +1; j < newList.size()-1; j++) 
                        {
     
                           // int ans = flowerPack[i].compareToIgnoreCase(flowerPack[j]);
     
                            if( ans > 0)
                            {
                              // currentMinimum = flowerPack[j];
                               currentMinIndex = j;
                            }
     
                            if(currentMinIndex != i)
                            {
                           // flowerPack[currentMinIndex] = flowerPack[i];
                           // flowerPack[i] = currentMinimum;
                            }              
     
                        }        
     
     
     
     
                       }
            }
              break;
     
     
     
              case 4: //filter student from list
              {
     
     
                String keyWord;
                boolean checkAns = false;
     
                System.out.println(" Please enter the character or word your would like to search for.");
                System.out.println(" This application will search through the list for your result. ");  
                System.out.print(" Ans:");        
                keyWord = scan.nextLine();
     
     
                for(int i = 0;  i < newList.size(); i++)
                {
                    //Plantclass newPlantPack = plantPack.get(i);
     
                  //  if(newPlantPack.getName().contains(keyWord))
                    {
                        System.out.println();
                     //   System.out.println(" " +newPlantPack.getName());
                        checkAns = true;
                    }
     
                }
     
                if(checkAns == false)
                {
                  System.out.println( " Sorry user, but are database could not any results."); 
                }
     
                if(checkAns == true)
                {
                  System.out.println( " Above is the queer of results we located.");
                }
                System.out.println("");              
     
     
     
     
              }
              break;               
     
     
     
              case 5: //create file list 
              {
     
                    Plantclass testjr = new Plantclass();
                    File file = new File("C:/Users/gshavers/Desktop/plantFile.txt ");
                    FileOutputStream outFileStream  = new FileOutputStream(file);
                    PrintWriter outStream = new PrintWriter(outFileStream);
     
                    for(int i = 1; i <= newList.size(); i++ )
                    {
                    outStream.println(newList.get(i).toString());           
                    System.out.println();
                    }        
     
                    System.out.println(" File created.....PLEASE check your desktop for plantFile.txt.");
                    System.out.println(); 
                    outStream.close();
     
              }
              break;                    
     
     
     
              case 6: //Load list
              {
     
              String iD,name,color,smell,thorns,poisonous,edible,medicinal,flavor,seasonal;  
     
              JFileChooser fileChooser = new JFileChooser();
     
              if( fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
               {
                   File file = fileChooser.getSelectedFile();
                   Scanner input = new Scanner(file);
     
     
     
                   while(input.hasNext())
                   {
                       String test = input.nextLine();
                       StringTokenizer st = new StringTokenizer(test);   
                       int count = st.countTokens();
     
                      if(count == 4)//fungus object
                      {
     
                          iD= st.nextToken();
                          name = st.nextToken();
                          color = st.nextToken();
                          poisonous = st.nextToken();    
     
                          Fungus newFungus = new Fungus( iD, name, color, poisonous);
     
                          newList.add(newFungus);
     
                      }
     
                        if(count == 5)//flower object
                      {
                          iD = st.nextToken();
                          name = st.nextToken();
                          color = st.nextToken();
                          thorns = st.nextToken();
                          smell = st.nextToken();
     
                          Flower newFlower = new Flower(iD, name, color, thorns, smell);
                          newList.add(newFlower);
                      }
     
                      if(count == 6){//weed object
     
                          iD = st.nextToken();
                          name = st.nextToken();
                          color = st.nextToken();
                          poisonous = st.nextToken();
                          edible = st.nextToken();
                          medicinal = st.nextToken();
     
                          Weed newWeed = new Weed(iD,name,color,poisonous,edible,medicinal);
                          newList.add(newWeed);
                      }
     
                      if(count == 6){//herb object
     
                          iD = st.nextToken();
                          name = st.nextToken();
                          color = st.nextToken();
                          flavor = st.nextToken();
                          medicinal = st.nextToken();
                          seasonal = st.nextToken();
     
                          Herb newHerb = new Herb(iD,name,color,flavor,medicinal,seasonal);
                          newList.add(newHerb); 
                      }                  
     
     
     
     
                   }
     
                   System.out.println(" File has beeen uploaded.");
                   System.out.println();                    
     
     
                    }          
     
              }
                break;    
     
     
     
     
     
              case 7: //analyze list
              {
                    System.out.println("Please enter a character or set of characters for analyzing"); 
                    System.out.print(" input:");
                    String s = scan.next();
     
                    for(int x = 1; x < newList.size(); x++)
                    {
                        // Plantclass newplantjr = plantPack.get(x);
                        // String in = newplantjr.getName();
                        //  substrings(0,1, s, in);
                    }
     
     
     
              }
              break;
     
     
              case 8:  //display student list
             {
               newList.print();
              }
                break;
     
              case 0: //Exit application
             {
              System.out.println(" Good Bye!");
              System.exit(0);
              }   
     
            }   
             System.out.println();
     
        } 
     
     
     
       }
    }
    public class LinkList 
    {
     
        public Node start;
        public static int listCount;
     
     
        public LinkList()
        {
            start = new Node(null);
            listCount = 0;
        }
     
        public void add(Object data)
        {
            Node temp = new Node(data);
            Node current = start;
     
            while( current.getNext() != null )
            {
                current = current.getNext();
            }
     
            current.setNext(temp);
     
     
           listCount++;
        }
     
     
        public Object get(int index)
     
        {
     
            if(index <= 0)
                return null;
     
            Node current = start.getNext();
            for(int i = 1; i < index; i++)
            {
                if(current.getNext() == null)
                    return null;
     
                current = current.getNext();
            }
            return current.getData();
        }
     
        public void remove(String firstLastName)
        {
          Node prev, del;
          String name =  firstLastName;
          del = start.getNext();
          prev = start.getNext();
     
            while (del != null ) 
            {
                if (del.getData().toString().contains(firstLastName)) break;
     
                prev = del;
                del = del.getNext(); 
     
                    if (del == null) 
                    {
                    System.out.println( "String Not Found" );
                    }     
                    else 
                    {
     
                            if (del == start) 
                            {
                            start = start.getNext();
                            } 
                            else 
                            {
                               System.out.println(prev); 
                               prev.setNext(del.getNext());
                            }
     
                    }
            }
     
     
     
        }
     
     
         public void print()
            {
                Node current = start.getNext(); 
     
                while(current != null)
                {
                    String output = "";
                    output += current.getData().toString();
                    System.out.println(output);
                    current = current.getNext();
     
                }
            }
     
     
     
     
     
     
     
         public static int size()
         {
            return listCount;
         }
     
     
        public class Node
        {
     
            Node next;
            Object data;
     
     
            public Node(Object data)
            {
                next = null;
                this.data = data;
            }
     
            public Node(Object data, Node next)
            {
                this.next = next;
                this.data = data;
            }
     
     
            public Object getData()
            {
                return data;
            }
     
            public void setData(Object data)
            {
                this.data = data;
            }
     
            public Node getNext()
            {
                return next;
            }
     
            public void setNext(Node next)
            {
                this.next = next;
            }
     
     
     
        }
     
     
     
     
    }
     
     
     
    public class Plantclass
    {
       public String ID;
       public String Name;
     
     
       public Plantclass()
       {
     
       }
     
     
       public Plantclass(String SetID, String SetName)
       {
           ID = SetID;
           Name = SetName;
     
       }
     
       public void setId(String setId)
       {
           ID = setId;
     
       }
     
       public String getId()
       {
           return ID;
     
       }
     
       public void setName(String setName)
       {
           Name = setName;
     
       }
     
       public String getName()
       {
           return Name;
     
       }
     
     
    }
     
     
     
     
     
    public class Flower extends Plantclass
    {
        public String color;
        public String thorns;
        public String smell;
        public String setIdJr;
        public String setNameJr;
     
       public Flower()
       {
     
       }
     
     
        public Flower(String SetID, String SetName, String color, String thorns, String smell)
        {
           super(SetID,SetName);  
           this.color = color;
           this.thorns  = thorns;
           this.smell = smell;
           setIdJr = SetID;
           setNameJr = SetName;
     
        }
     
        public void setColor(String color)
        {
            this.color = color;
        }
     
     
        public void setThorns(String thorns)
        {
          thorns  = thorns;
        }
     
     
        public void setSmell(String smell)
        {
            this.smell = smell;
        }
     
        public String getColor()
        {
            return color;
        }
     
        public String getThorns()
        {
            return thorns;
        }
     
         public String getSmell()
        {
            return smell;
        }
     
     
         public String toString()
        {
            return setIdJr + " " + setNameJr + " " +  color + " "  + thorns + " " + smell;
     
        }
     
     
    }
     
     
     
     
     
    public class Fungus extends Plantclass
    {
        public String color;
        public String poisonous;
        public String setIdJr;
        public String setNameJr;
     
        public Fungus()
       {
     
       }    
     
        public Fungus(String SetID, String SetName, String color, String poisonous)
        {
           super(SetID,SetName);  
           this.color = color;
           this.poisonous = poisonous;
           setIdJr = SetID;
           setNameJr = SetName;
        }
     
        public void setColor(String color)
        {
            this.color = color;
        }
     
     
        public void setPoisonous(String poisonous)
        {
            this.poisonous = poisonous;
        }
     
     
        public String getColor()
        {
            return color;
        }
     
        public String getPoisonous()
        {
            return poisonous;
        }
     
     
         public String toString()
        {
            return setIdJr + " " + setNameJr + " " +  color + " "  + poisonous;
     
        }
     
     
    }
     
     
     
     
     
     
    public class Weed extends Plantclass
    {
     
        public String color;
        public String poisonous;
        public String edible;
        public String medicinal;
        public String setIdJr;
        public String setNameJr;
     
        public Weed()
        {
     
        }
     
        public Weed(String SetID, String SetName, String color, String poisonous, String edible, String medicinal)
        {
           super(SetID,SetName);  
           this.color = color;
           this.poisonous = poisonous;
           this.edible = edible;
           this.medicinal = medicinal;
           setIdJr = SetID;
           setNameJr = SetName;
        }
     
        public void setColor(String color)
        {
            this.color = color;
        }
     
        public void setPoisonous(String poisonous)
        {
            this.poisonous = poisonous;
        }
     
         public void setEdible(String edible)
        {
            this.edible = edible;
        }
     
        public void setmedicinal(String medicinal)
        {
            this.medicinal = medicinal;
        }
     
     
        public String getColor()
        {
            return color;
        }
     
        public String getPoisonous()
        {
            return poisonous;
        }
     
         public String setEdible()
        {
            return edible;
        }
     
          public String setMedicinal()
        {
            return medicinal;
        }
     
        public String toString()
        {
            return setIdJr + " " + setNameJr + " " +  color + " "  + poisonous + " " + edible + " " +  medicinal;
     
        }
    }
     
     
     
     
    public class Herb extends Plantclass
    {
     
        public String color;
        public String flavor;
        public String seasonal;
        public String medicinal;
        public String setIdJr;
        public String setNameJr;
     
        public Herb()
        {
     
        }
     
        public Herb(String SetID, String SetName, String color, String flavor, String medicinal , String seasonal)
        {
           super(SetID,SetName);  
           setIdJr = SetID;
           setNameJr = SetName;
           this.color = color;
           this.flavor = flavor;
           this.medicinal = medicinal;
           this.seasonal = seasonal;
        }
     
        public void setColor(String color)
        {
            this.color = color;
        }
     
        public void setFlavor(String flavor)
        {
            this.flavor = flavor;
        }
     
         public void setMedicinal(String medicinal)
        {
            this.flavor = flavor;
        }
     
     
        public void setSeasonal(String seasonal)
        {
            this.seasonal = seasonal;
        }
     
     
        public String getColor()
        {
            return color;
        }
     
        public String getSeasonal()
        {
            return seasonal;
        }
     
         public String setFlavor()
        {
            return flavor;
        }
     
          public String medicinal()
        {
            return medicinal;
        }
     
        public String toString()
        {
            return setIdJr + " " + setNameJr + " " +  color + " "  + seasonal + " " + flavor + " " +  medicinal + " " + seasonal;
     
        }
     
     
     
     
    }
    I actually never told you what my program is supposed to do. So here are the instructions from my professor.


    CSC 275 Final Project
    Instructor: Joshua L. Smith
    Question Subject Line: CSC 275 Online Final Project Question
    Answer Subject Line: CSC 275 Online Final Project Solution
    Now that we've managed to weave our way through the North Woods safely and are one step closer to Thanto, our destination. After a night spent on the far side of the woods we wake up to the sound of rushing water. We quickly open our eyes and notice the Fraus River not 20 feet from where we lay. The river must have been expanding throughout the night in an attempt to swallow us whole. As we are standing on our ever shrinking piece of land we notice a homemade raft with a single passenger. After screaming and waving for a short amount of time we get the captain of this raft to paddle towards us.
    After some quick deliberations the captain agrees that he can take us on his trip across the ever widening river. As always, there is a bit of a snag in our free ride. Our ride isn't exactly free, so we've got to do some work to earn the ride. What we need to do in order to earn our ride is increase the storage capacity and efficiency of our flower pack.
    This new version of flower pack should share some of the traits of our old one, with a minor adjustment.
    You must use a LinkedList for storage, and you CANNOT use the LinkedList that is built into Java. You absolutely MUST use your own LinkedList created from the notes given in Lecture 8. Your code should not contain any Arrays or ArrayLists.
    You must be able to add, remove, sort, filter, save, load and analyze information from our flower pack.
    The flower pack should hold flowers, weeds, fungus, and herbs. All should be a subclass of a plant class.
    Each subclass shares certain qualities (ID, Name, and Color)
    Flower traits include (Thorns, and Smell)
    Fungus traits include (Poisonous)
    Weed traits include (Poisonous, Edible and Medicinal)
    Herb traits include (Flavor, Medicinal, Seasonal)

    There is ABSOLUTELY NO GUI required for your final project.


    --------------------------------------------------------------------------

    What I'm having issues is actually retrieving my data from my linklist. Creating the LinkList seems easy, but actually retrieving my data or iterating them seems the most difficult for me. Any direction would be help full.

  12. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I need help with LinkList: Removing and adding

    If you've drawn it out and understand how nodes and links work, why do you think you still need an index?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #12
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need help with LinkList: Removing and adding

    One of the main reason I'm having trouble understanding about this LinkList concept is iteration through the LinkList. It just a little hard for me to grasp. I know each node has reference to the next node until that final node is reference to null. That's when you know your at the end of your list. That's the only thing I grasp at this moment. Even we creating my add method I just kept paying around with the code and until I finally got something feasible. This is really frustrating.

  14. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I need help with LinkList: Removing and adding

    Yeah, programming is frustrating. But you can't just add code without understanding the concepts and expect it to work. That's why I suggested drawing it out. If you draw out a linked list with nodes, values, and links, then inserting or removing a node should be relatively straightfoward. When inserting a node, what are you doing with the links? What about when you remove a node?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #14
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need help with LinkList: Removing and adding

    Quote Originally Posted by KevinWorkman View Post
    When inserting a node, what are you doing with the links? What about when you remove a node?
    Well,

    I know you need to insert an object into the node lets say Node1. Node1 will have my data and a reference point which will point to null. Sense there is only node in the list. When I create Node2 it will also have data and a reference to null. From there I should either reference the first node to the second or vice verse.

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help with LinkList: Removing and adding

    I should either reference the first node to the second or vice verse.
    In a simple, singly linked list, the root always points to the first node. To get to any other node in the list you have to follow the links from one node to the next.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: I need help with LinkList: Removing and adding

    I know you need to insert an object into the node lets say Node1. Node1 will have my data and a reference point which will point to null. Sense there is only node in the list. When I create Node2 it will also have data and a reference to null. From there I should either reference the first node to the second or vice verse.
    I'm not sure if this will make it better or worse because this isn't a great example, but imagine a cargo train. A cargo train consists of cars. Each car of the train holds cargo, and each car is connected to the one before it. The first car is the locomotive, which is where the engineer sits, but let's just treat that like any other car for this analogy. Ok, so before you can even have a train, you need to have the locomotive. Once you have that, you can add other cars behind it, but you can only attach one car to the back of any given car. So, whenever you are required to add an additional car to the train, your only option is to attach it the back of the last car. Similarly, if you want to remove a single car in the middle of the train, you can't just detach it from the car in front of it, because you would also lose all the cars behind the one you are removing. So after you remove the car, you need to grab the car behind the one you are removing and reattach it to the train (by attaching it to the car in front of the one you removed).

    Now, assuming that all wasn't confusing, in that analogy:
    1. the LinkedList is the Train
    2. the Node is the train Car
    3. the Object in the Node is the cargo in the train car
    4. the locomotive is your Root Node

    Toss that thought around in your head for a bit, and see if anything is more clear (or it could be worse, but I tried, lol).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. adding and removing from an array while conditions are met.
    By ddnpresident in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 4th, 2013, 06:00 AM
  2. Best way of adding and removing?
    By J-moges in forum Java Theory & Questions
    Replies: 3
    Last Post: May 23rd, 2013, 01:43 PM
  3. Having trouble writing insert method for Ordered LinkList.
    By orbin in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 28th, 2012, 05:25 PM
  4. adding or removing panels to panels
    By luisp88 in forum AWT / Java Swing
    Replies: 3
    Last Post: November 1st, 2011, 04:37 PM
  5. Linklist Question
    By NeedzABetterSN in forum Java Theory & Questions
    Replies: 2
    Last Post: December 18th, 2010, 08:11 PM