Results 1 to 14 of 14
  1. #1
    WSUVanc is offline Member
    Join Date
    Mar 2013
    Posts
    14
    Rep Power
    0

    Default Strings into nodes

    I am having issues with takings Strings and using them as the arguments going into a queue or a push method in a singly linked list. Could anyone provided and example of how this is done. 2 or 3 lines of code should be sufficient. Consider this your daily code challange lol. Shouldn't be much of one though

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Strings into nodes

    No that's not how it works as this is not a code factory. Please show any code you may be having trouble with and ask questions regarding that code, but please don't ask for other's to produce code for you. As for examples, there are plenty to be found, so why not do a decent search?

  3. #3
    WSUVanc is offline Member
    Join Date
    Mar 2013
    Posts
    14
    Rep Power
    0

    Default Re: Strings into nodes

    Quick example of what not to do, i guess. At least it was my first thought...

    Java Code:
    public void push(String data){
    		head.next=head;
    		head.data=data;
    		
    	}
    I still fail to see the error in it though.

  4. #4
    WSUVanc is offline Member
    Join Date
    Mar 2013
    Posts
    14
    Rep Power
    0

    Default Re: Strings into nodes

    Im not scouring the internet for code to rip off. i was hoping for a quick line or two of code as an example, nothing useable, i guess i may not have made that clear.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Strings into nodes

    Think about it logically on paper. Use boxes to represent variables that point to objects using arrows.

  6. #6
    WSUVanc is offline Member
    Join Date
    Mar 2013
    Posts
    14
    Rep Power
    0

    Default Re: Strings into nodes

    It is on the white board next to me, as drawing nodes out is the first step in writing any linked list program, at least from what i have been told. Does
    Java Code:
    head.data=String someString
    not overwrite the data currently located in that node

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  8. #8
    WSUVanc is offline Member
    Join Date
    Mar 2013
    Posts
    14
    Rep Power
    0

    Default Re: Strings into nodes

    Thats were im confused so far i have always pushed or popped only nodes. If you want to push a String, do you put it in a node then push that, or can you just use the first node that the list was constru.... hmmm... that would only work the first time then... that would be why i can only do one... so

    public void push(String data){
    Node temp= new Node(data)
    head.next=head;
    head=temp;

    }

    assuming Node constructor takes a string and assigns it to data

  9. #9
    WSUVanc is offline Member
    Join Date
    Mar 2013
    Posts
    14
    Rep Power
    0

    Default Re: Strings into nodes

    Complete code....

    Java Code:
    public class LinkedList {
    
    	private Node head;
    	private Node tail;
    	
    
    	
    	
    	public LinkedList(Node firstelement) {
    		head = firstelement;
    		tail = firstelement;
    	}
    	
    	public Node first() {
    		return head;
    	}
    	
    	public void appendToFront(Node another) {
    		another.next = head;
    		head = another;
    		
    	}
    	public void append(Node another) {
    		tail.next = another;
    		tail = tail.next;
    	}
    	
    
    	public static class Node {
    
    		public String data;
    		private Node next;
    		
    		public Node(String s) {
    			data = s;
    		}
    	}
    	
    	public static class LinkedListIterator {
    
    		private Node current;
    		public LinkedListIterator(LinkedList l) {
    			current = l.head;
    		}
    		
    		public boolean hasNext() {
    			// return true iff there is another node in the list
    			if (current != null) return true;
    			return false;
    			
    		}
    		public Node next() {
    			// return the 'next' node and advance
    			Node thisone = current;
    			current = current.next;
    			return thisone;
    			
    		}
    		public void push(String data){
    			Node temp= new Node(data);
    			temp.next=current;
    			current=temp;
    
    			}
    		
    		public String pop(){
    			if(hasNext()){
    			String out=current.data;
    			current=current.next;
    			
    			return out;
    			}
    			
    		return "Empty";
    		}
    	}
    	public static void main(String []args) {
    		LinkedList l = new LinkedList(new Node("Hello"));
    		LinkedListIterator i = new LinkedListIterator(l);
    		i.push("Friends!");
    		i.push("I've");
    		i.push("been to");
    		i.push("Hoth");
    
    		while(i.hasNext()){
    			System.out.println(i.pop());
    			}
    		
    		System.out.println(i.pop());
    		
    		i.push("So");
    		i.push("has");
    		i.push("leia");
    		while(i.hasNext()){
    			System.out.println(i.pop());
    			}
    		System.out.println(i.pop());
    	}
    }
    Last edited by WSUVanc; 03-15-2013 at 04:59 AM. Reason: Completed running Code

  10. #10
    WSUVanc is offline Member
    Join Date
    Mar 2013
    Posts
    14
    Rep Power
    0

    Default Re: Strings into nodes

    less broke...hmmmm
    Last edited by WSUVanc; 03-15-2013 at 04:50 AM. Reason: fixed some stuff

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Strings into nodes

    You need to analyze your code as it's running. Consider either using a debugger or generously sprinkling your code with println statements so as to allow you to check the state of the class fields as it runs.

  12. #12
    WSUVanc is offline Member
    Join Date
    Mar 2013
    Posts
    14
    Rep Power
    0

    Default Re: Strings into nodes

    Honestly the debugger in eclipse scares the hell out of me, i love using C debuggers. I am suppose to learn how to use test cases or something along those lines very soon. I believe the only problem i currently have is the case where the stack is depleted and then being able to refill it with out any issues. I never figured this issue out when i did a very similiar program for queue and dequeue.

  13. #13
    WSUVanc is offline Member
    Join Date
    Mar 2013
    Posts
    14
    Rep Power
    0

    Default Re: Strings into nodes

    Fubarable- Thanks for the help it looks like it runs fine now. I don't understand while i didn't end up with the same error i have on my queue dequeue code that is also posted on here.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. An app that toggles nodes on a map
    By BipolarBear in forum Android
    Replies: 0
    Last Post: 12-04-2012, 05:48 PM
  2. Queuing Nodes
    By m4773rz in forum New To Java
    Replies: 1
    Last Post: 01-05-2012, 12:03 AM
  3. Inserting Nodes
    By myst in forum New To Java
    Replies: 2
    Last Post: 07-12-2010, 05:26 PM
  4. nodes
    By Dr Gonzo in forum New To Java
    Replies: 1
    Last Post: 12-08-2008, 05:22 PM
  5. nodes in java
    By ahsan in forum New To Java
    Replies: 0
    Last Post: 12-26-2007, 04:09 PM

Posting Permissions

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