help with singly linklists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yolkman
    New Member
    • Sep 2006
    • 14

    #1

    help with singly linklists

    We were giving a sample link list to play around with for class. When running the program, the last line of the print out says:

    List After adding a Node: 7 6 5 4 3 2 1 0

    However I added a method called stepOne that is suppose to swap positions of the 5 and the 4 in order that it would print out

    List After adding a Node: 7 6 4 5 3 2 1 0

    However I don't get this print out.

    Code:
     public void stepOne(){
       
       Node position=first;
       
       
       while(position.data!=5 && position!=null){
    	
    	position=position.next;
    	
    	position.next=position;
    	
    	
    	}
    	
    	
    	position=first.next.next;
    	
    	position.next.next=position;
    	
    	}
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by yolkman
    We were giving a sample link list to play around with for class. When running the program, the last line of the print out says:

    List After adding a Node: 7 6 5 4 3 2 1 0

    However I added a method called stepOne that is suppose to swap positions of the 5 and the 4 in order that it would print out

    List After adding a Node: 7 6 4 5 3 2 1 0

    However I don't get this print out.

    Code:
     public void stepOne(){
       
       Node position=first;
       
       
       while(position.data!=5 && position!=null){
    	
    	position=position.next;
    	
    	position.next=position;
    	
    	
    	}
    	
    	
    	position=first.next.next;
    	
    	position.next.next=position;
    	
    	}
    I can see one problem in your while(...) loop.

    First, you set position to position.next. This basically moves position up one place in your linked list. However, your next statement stores position into position.next. Now, the next time the loop executes, you will set position equal to itself - since position.next IS position! Your while loop will execute infinitely, since the data is never 5 and position is never null.

    Comment

    • yolkman
      New Member
      • Sep 2006
      • 14

      #3
      I've got it to the point now where instead of displaying: 7 6 5 4 3 2 1 0
      it says 7 6 5 3 2 1 0. However the 4 doesn't show up between the 6 and the 5.

      Code:
      while(position.data!=5 && position!=null){
      	
      	position=position.next;
      	
      	previous=position;
      	
      	}
      	
      	
      	position.next=position.next.next;
      	
      	
      	
      	
      	
      	}

      Comment

      • yolkman
        New Member
        • Sep 2006
        • 14

        #4
        I've modified the code some more and now instead of printing out 7 6 5 4 3 2 1 0
        i get 7 6 4 5. How do I get the 3 2 1 and 0 to show after the 5?

        Code:
        Node previous=first;
        Node current=first.next;
        
        while(current.data!=5 and current!=null){
        previous=current;
        current=previous.next;
        }
        
        previous.next=current.next;
        previous.next.next=new Node(5);

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          From your code, I'm not sure where to go...so let me suggest the following code:

          Code:
          Node current = first;
          Node prev = null;
          
          while (current.data != 6 && current != null) {
             prev = current;
             current = current.next;
          }
          
          if (current == null) System.out.println("Error; expected value not found!");
          else {
             Node temp = prev;
             prev = current;
             current = temp;
          }
          See if this will work for you.
          Last edited by Ganon11; Oct 29 '06, 12:24 PM. Reason: type O.o

          Comment

          • yolkman
            New Member
            • Sep 2006
            • 14

            #6
            Originally posted by Ganon11
            From your code, I'm not sure where to go...so let me suggest the following code:

            Code:
            Node current = first;
            Node prev = null;
            
            while (current.data != 6 && current != null) {
               prev = current;
               current = current.next;
            }
            
            if (current == null) System.out.println("Error; expected value not found!");
            else {
               Node temp = prev;
               prev = current;
               current = temp;
            }
            See if this will work for you.

            It didn't give me the printout 7 6 4 5 3 2 1 0
            Instead it printed out 7 6 5 4 3 2 1 0

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by yolkman
              It didn't give me the printout 7 6 4 5 3 2 1 0
              Instead it printed out 7 6 5 4 3 2 1 0
              Could you post the entire code you are using to test this. I'd like to join but I think I need to look at all the parts first.

              Comment

              • yolkman
                New Member
                • Sep 2006
                • 14

                #8
                Originally posted by r035198x
                Could you post the entire code you are using to test this. I'd like to join but I think I need to look at all the parts first.
                I got it to work now. Thanks for your help!

                Comment

                Working...