Results 1 to 1 of 1
  1. #1
    needHelp12345 is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default Linked list help!! Notepad Program.

    Hi, I need to create a notepad program, which is essentially a linked list of nodes, where each node is a linked list of nodes. The "inner" list is for storing a "note" (each line of the note will be stored in a node of this list and can be either a String or an Integer). The "outer" is a list of these notes. I have to make a generic class, so I have the G<T> class for the Object data, GNode<T> class for the node, GList<T> class for the list, and my MyMain class for the main method.
    Below are the classes. I am not sure how to go about this!! How do I store a string or integer as a node and keep it as a linked list within a linked list? I know I have a printList and setEntry compiling error in my main method (the only errors so far), and I would like help with that, but more with the actual concept and how to go about it. I may not need to fix those errors if they are not needed the way I wrote them. This is as far as I've gotten.

    ---------------------------------------------------------The MyMain Class
    Java Code:
    import java.util.*;
    public class MyMain {
    
        public static void main(String[] args) {
           
    		 String choice, choiceTwo, choiceThree, stringLine;
    		 Integer integerChoice;
    		 Scanner keyboard = new Scanner(System.in);		 
    		
    		 do {
    			 System.out.println("add a new note (a)");
    	 		 System.out.println("display all notes (d)");
    	  		 System.out.println("quit (q)");
    			 choice=keyboard.next();
    		    while (!keyboard.hasNext("[adq]")) {
    		 	   System.out.println("Please enter a valid selection.");
    			   keyboard.next();
    	       }
    			if(choice.equals("a")) {
    			System.out.println("new line (y/n)?");
    			choiceTwo=keyboard.next();
    			if (choiceTwo.equals("y")) {
    				System.out.println("string or integer (s/i)?");
    		 		choiceThree=keyboard.next();
    					if (choiceThree.equals("s")) {
                            System.out.println("string:");
                            stringLine=keyboard.nextLine();
                            G<Object> tst = new G<Object>(stringLine);
                            GNode<Object> newNode = new GNode<Object>(tst);
                            newNode.setEntry(tst);
    					} else if (choiceThree.equals("i")) {
    						System.out.println("integer:");
    		 				integerChoice=keyboard.nextInt();
    						GList<Object> newNode = new GList<Object>();
    						newNode.setEntry(integerChoice);
    					}
    			 }
    			 if (choiceTwo.equals("n")) 
    			 		System.out.println("Please pick another option.");
    			 if (choice.equals("d")) {
    					GList<GNode<Object>> newList = new GList<GNode<Object>>;
    					newList.printList;
    		 }else if (choice.equals("q"))
    					System.exit(0);
    	}
    	}while (true);
    }
    }

    ---------------------------------------------------------The GList<T> Class
    Java Code:
    import java.util.*;
    
    public class GList<T> {
    	 private GNode<T> head, tail, cursor;
    	 
      // default constructor 
      public GList()
      {
        head.setPrev(null);
      }
      
       //prints the list
      public void printList(){
      		int count = 0;
          GNode<T> current = head.getNext();
          while(current != null) {
    		    System.out.println(count);
              System.out.println(current.getEntry().getData());
              current = current.getNext();
    			 count++;
          }
      }
    }

    ---------------------------------------------------------The GNode<T> Class
    Java Code:
    import java.util.*;
    
    public class GNode<T> {
    
         private G<T> entry;
         private GNode<T> next, head;
    	  private GNode<T> prev;
    
    
        public GNode (G<T> entry)
        {
            this.entry = entry;
        }
    
        public GNode (G<T> e, GNode<T> next)
         {
            this.entry = e;
            this.next = next;
         }
        public void setEntry(G<T> e)
        {
            this.entry = e;
        }
      	 public void setPrev(GNode<T> prev)
        {
            this.prev = prev;
        }
        public void setNext(GNode<T> next)
        {
            this.next = next;
        }
    	 public GNode<T> getPrev()
        {
            return this.prev;
        }
        public GNode<T> getNext()
        {
            return this.next;
        }
    
        public G<T> getEntry()
        {
            return this.entry;
        }
    	  //prints the list
        public void printList(){
      		int count = 0;
          GNode<T> current = head.getNext();
          while(current != null) {
    		    System.out.println(count);
              System.out.println(current.getEntry().getData());
              current = current.getNext();
    			 count++;
          }
      }
    }

    ---------------------------------------------------------The G<T> Class
    Java Code:
    import java.util.*;
    
    public class G<T> {
      private T data; 
      private G<T> entry;
      private G<T> next, head;  
      private G<T> prev;
    
      //constructor
       public G(T initialData)
      {
         data = initialData;       
      }
    
      // Retrieve the data 
      public Object getData()
      {
        return data;
      }
    
       public G<T> getNext()
       {
            return this.next;
       }
       public void setData(T newData)
       {
    	  data = newData;
       }
    	public void setPrev(G<T> prev)
       {
            this.prev = prev;
       }
    	public G<T> getEntry()
       {
            return this.entry;
       }
    	public void setEntry(G<T> e)
       {
           this.entry = e;
       }
    	
    	//prints the list
       public void printList(){
      		int count = 0;
          G<T> current = head.getNext();
          while(current != null) {
    		    System.out.println(count);
              System.out.println(current.getEntry().getData());
              current = current.getNext();
    			 count++;
          }
       }
    }
    Last edited by Fubarable; 02-26-2011 at 05:19 PM. Reason: Moderator Edit: Code tags added

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  2. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  3. Notepad program
    By sridevisala in forum Java Software
    Replies: 1
    Last Post: 04-26-2010, 09:56 AM
  4. Save PolygonLinked list in Notepad.txt
    By cassysumandak in forum New To Java
    Replies: 3
    Last Post: 10-01-2009, 09:15 AM
  5. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 08:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •