Linked Lists

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1878 Views - Last Post: 02 March 2011 - 07:18 AM Rate Topic: -----

#1 pureblood40  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-November 10

Linked Lists

Posted 01 March 2011 - 08:52 AM

I am trying to use a linked list to add a song. 'A' is supposed to add the song to list of objects. My problem is that every time I append a song again it seems my list gets reset. Also on the side. How come I cant use the showFromMeOn method down in 'P'?
Here is my class.
import jm.music.data.*;
import jm.JMC;
import jm.util.*;
import jm.music.tools.*;

/**
 * Represents a node in a flexible song structure
 * 
 * @author Mark Guzdial
 * @author Barb Ericson
 */
public class SoundNode2 {

 /** the next SongNode in the list */
 private SoundNode2 next;

 /** holds the sound */
 private Sound mySound;
 
 /**
  * setPhrase takes a Phrase and makes it the one for this node
  * 
  * @param thisPhrase
  *            the phrase for this node
  */
 public void setSound(Sound thisSound) {
  this.mySound = thisSound;
 }

 /**
  * Creates a link between the current node and the input node
  * 
  * @param nextOne
  *            the node to link to
  */
 public void setNext(SoundNode2 nextOne) {
  this.next = nextOne;
 }

 

 /**
  * Repeat the input phrase for the number of times specified. But do an
  * insertion, to save the rest of the list.
  * 
  * @param nextOne
  *            node to be copied into the list
  * @param count
  *            number of times to copy it in.
  */


 /**
  * Insert the input SongNode AFTER this node, and make whatever node comes
  * NEXT become the next of the input node.
  * 
  * @param nextOne
  *            SongNode to insert after this one
  */
 public void insertAfter(SoundNode2 nextOne) {
  SoundNode2 oldNext = this.getNext(); // Save its next
  this.setNext(nextOne); // Insert the copy
  nextOne.setNext(oldNext); // Make the copy point on to the rest
 }

 /**
  * Weave the input song node count times every skipAmount nodes
  * 
  * @param nextOne
  *            node to be copied into the list
  * @param count
  *            how many times to copy
  * @param skipAmount
  *            how many nodes to skip per weave
  */
 public void weave(SoundNode2 nextOne, int count, int skipAmount) {

  SoundNode2 current = this; // Start from here
  SoundNode2 copy; // Where we keep the one to be weaved in

  // loop count times
  for (int i = 0; i < count; i++) {

   // Skip skipAmount nodes (this one is 1)
   for (int j = 1; j < skipAmount; j++) {

    // as long as current isn't null move to next
    if (current != null) {
     current = current.getNext();
    }
   }

   // if current isn't null
   if (current != null) {
    // make a new copy
    copy = nextOne.copyNode();

    // insert it after current
    current.insertAfter(copy);

    // move current along
    current = copy.getNext();
   }

   // else if current is null return (break out early)
   if (current == null) {
    return;
   }
  }
 }

 /**
  * Get the next song node
  * 
  * @return the next song node or null if none
  */
 public SoundNode2 getNext() {
  return this.next;
 }

 /**
  * Accessor for the node's Phrase
  * 
  * @return internal phrase
  */
 private Sound getSound() {
  return this.mySound;
 }

 /**
  * Collect all the notes from this node on in an part (then a score) and
  * open it up for viewing.
  * 
  * @param instrument
  *            the MIDI instrument (program) to be used in playing
  */
 public void showFromMeOn() {

    // Make a new Phrase that will contain the notes from
  // all the phrases

  // Start from this element (this)
  SoundNode2 current = this;
  // While we're not through...
  while (current != null) {
    mySound.blockingPlay();

   // Now, move on to the next element
   current = current.getNext();
  }
 }
}


Here is my main.
import java.util.*;

public class MashupDriver {
 public static void main(String[] args) {
  Scanner kybd = new Scanner(System.in);
  // Tells user what they can do
  System.out.println("This program puts sounds in a list and plays them.");
  System.out.println("Here is a list of options.");
  System.out.println("Please enter 'A' if you want to add another sound at the end");
  System.out.println("'I' if you want to add a song to the beginning.");
  System.out.println("'W' to to insert a clip repeatedly into the existing set and allowing to skip 1 or more clips in between.");
  System.out.println("'P' if you want to play the mashup.");
  System.out.println("'Q' to quit. The program");

  // loop asking what to do
  while (true) {
   SoundNode2 frontSound = null;
   SoundNode2 lastSound;
   System.out.println("Please enter an option.");
   String rawInput = kybd.next();
   char input = ((rawInput.toUpperCase()).charAt(0));
   switch (input) {
   case 'A':
    System.out.println("Please select a wav file");
    System.out.println("How many times would you like to append?");
    int appNum = kybd.nextInt();
    Sound s = new Sound(FileChooser.pickAFile());
      //loop to fill list with user appointed times
    for (int i = 1; i <appNum; i ++){
    SoundNode2 sn = new SoundNode2();
      sn.setSound(s);
     if (frontSound == null){ // handle very first node
     frontSound = new SoundNode2();
     sn.setNext(frontSound);
     frontSound = sn;
     }else{  lastSound = new SoundNode2();
       sn.setNext(lastSound);
       lastSound = sn;
}
      sn.showFromMeOn();
}
 
    break;
    
   case 'I':
     System.out.println("How many wav files do you want to insert.");
     int count = kybd.nextInt();
     //s.insertAfter(FileChooser.pickAFile(), count);
    break;
   case 'W':
       System.out.println("How many wav files do you want to add?");
       count = kybd.nextInt();
       System.out.println("How many tracks do you want to skip?");
       int skipAmount = kybd.nextInt();
       break;
   case 'P':
   showFromMeOn();
   System.out.println("Test play");
    break;
   case 'Q':
     System.out.println("Thank you for using my program.");
   System.exit(0);
   }
  }
 }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Linked Lists

#2 pureblood40  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-November 10

Re: Linked Lists

Posted 01 March 2011 - 09:00 AM

Opps i want this in Java not java script
Was This Post Helpful? 0
  • +
  • -

#3 pureblood40  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-November 10

Re: Linked Lists

Posted 01 March 2011 - 09:00 AM

Opps i want this in Java not java script
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,686
  • Joined: 27-December 08

Re: Linked Lists

Posted 01 March 2011 - 09:02 AM

Moved to Java. Note that Java != Javascript.

You really need to use spacing some in your MashupDriver class to make it easier to read.

In the 'A' case, note that you have this line, which resets your List: frontSound = new SoundNode2();. You'll want to iterate through the List, and add the new Node to the end of the last Node.

In case 'P', you shouldn't be able to invoke the showFromMeOn() method as you have it now. You have to invoke it from a Node.
Was This Post Helpful? 0
  • +
  • -

#5 pureblood40  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-November 10

Re: Linked Lists

Posted 01 March 2011 - 09:52 AM

Here is what I did with my A, it still didn't work right. I don't think I am iterating through the list.
Do I do that like this?
 while (current != null) {
   // Now, move on to the next element
   current = current.getNext();
  }


  while (true) {
   SoundNode2 frontSound = null;
   
   System.out.println("Please enter an option.");
   
   String rawInput = kybd.next();
   
   char input = ((rawInput.toUpperCase()).charAt(0));
   switch (input) {
     case 'A':
       System.out.println("Please select a wav file");
       System.out.println("How many times would you like to append?");
       int appNum = kybd.nextInt();
       Sound s = new Sound(FileChooser.pickAFile());
      //loop to fill list with user appointed times
         for (int i = 1; i <appNum; i ++){
         SoundNode2 sn = new SoundNode2();
         sn.setSound(s);
         sn.setNext(frontSound);
         frontSound = sn;
        }
        break;


Also I changed P to this.
frontSound.showFromMeOn();
and get a null pointed exception. I know why and it's because I have frontSound set to null but shouldn't it change when it goes through A?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,686
  • Joined: 27-December 08

Re: Linked Lists

Posted 01 March 2011 - 10:15 AM

You don't want to add Nodes to the list in the loop. You're on the write track with the first code snippet. You need to loop to get to the tail Node.
current <-- head
while current != null
      current <-- current.next

set current.next to the new Node



I also have a Linked List tutorial you may want to check out. :)
Was This Post Helpful? 0
  • +
  • -

#7 pureblood40  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-November 10

Re: Linked Lists

Posted 01 March 2011 - 11:58 AM

I read through the tutorial and tried some things but i didn't really get any where.
    for (int i = 1; i <appNum; i ++){
           while(frontSound != null){
           sn.setSound(s);
           sn.setNext(frontSound);
           frontSound=sn;
           }
            sn.setSound(s);
           
         }

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,686
  • Joined: 27-December 08

Re: Linked Lists

Posted 01 March 2011 - 12:05 PM

Why are you modifying the Nodes in the while loop? This is something you shouldn't be doing, as I've said in my above posts.
Was This Post Helpful? 0
  • +
  • -

#9 pureblood40  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-November 10

Re: Linked Lists

Posted 01 March 2011 - 12:17 PM

Sorry. I guess I didn't understand.

View Postmacosxnerd101, on 01 March 2011 - 12:05 PM, said:

Why are you modifying the Nodes in the while loop? This is something you shouldn't be doing, as I've said in my above posts.

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,686
  • Joined: 27-December 08

Re: Linked Lists

Posted 01 March 2011 - 12:19 PM

You just want to get to the end Node. That is the point of the while loop. Then modify the variable, which points to the end Node in the List.
Was This Post Helpful? 0
  • +
  • -

#11 pureblood40  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-November 10

Re: Linked Lists

Posted 01 March 2011 - 01:41 PM

I fee like i am making this more complicated than it is.
Something like this?
If (frontSound == null){ // handle very first node
frontSound = new SoundNode2(s);
}

View Postmacosxnerd101, on 01 March 2011 - 12:19 PM, said:

You just want to get to the end Node. That is the point of the while loop. Then modify the variable, which points to the end Node in the List.

Was This Post Helpful? 0
  • +
  • -

#12 pureblood40  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-November 10

Re: Linked Lists

Posted 01 March 2011 - 03:27 PM

I feel that I am so close yet so far away. I'm not sure how to keep track of nodes.
A adds a song to the beginning. And I adds a song to the end.
For some reason they are not getting added to list and I can't seem to figure it out.
This is my first time working with linked lists and I am a little bit confused.
I tried reading the tutorial but I couldn't figure out how to incorporate that into mine.

import java.util.*;

public class MashupDriver {
 public static void main(String[] args) {
  Scanner kybd = new Scanner(System.in);
  // Tells user what they can do
  System.out.println("This program puts sounds in a list and plays them.");
  System.out.println("Here is a list of options.");
  System.out.println("Please enter 'A' if you want to add another sound at the end");
  System.out.println("'I' if you want to add a song to the beginning.");
  System.out.println("'W' to to insert a clip repeatedly into the existing set and allowing to skip 1 or more clips in between.");
  System.out.println("'P' if you want to play the mashup.");
  System.out.println("'Q' to quit. The program");

  // loop asking what to do
  while (true) {
   SoundNode2 frontSound = null;
   System.out.println("Please enter an option.");
   
   String rawInput = kybd.next();
   SoundNode2 sn = new SoundNode2();
   char input = ((rawInput.toUpperCase()).charAt(0));
   switch (input) {
     case 'A':
       System.out.println("Please select a wav file");
       System.out.println("How many times would you like to append?");
       int appNum = kybd.nextInt();
       
      //loop to fill list with user appointed times
         for (int i = 1; i <appNum; i ++){
       Sound s = new Sound(FileChooser.pickAFile());
       sn.setSound(s);
       sn.setNext(frontSound);
       frontSound = sn;
      }
         sn.showFromMeOn();
        break;
     case 'I':
       System.out.println("How many wav files do you want to insert.");
       int count = kybd.nextInt();
       Sound si = new Sound(FileChooser.pickAFile());
       sn.setSound(si);
       sn.setNext(current);
       break;
   case 'W':
       System.out.println("How many wav files do you want to add?");
       count = kybd.nextInt();
       System.out.println("How many tracks do you want to skip?");
       int skipAmount = kybd.nextInt();
       break;
   case 'P':
   sn.showFromMeOn();
    break;
   case 'Q':
     System.out.println("Thank you for using my program.");
   System.exit(0);
   }
  }
 }
}

Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,686
  • Joined: 27-December 08

Re: Linked Lists

Posted 01 March 2011 - 07:00 PM

This is a diagram of how Linked Lists work. The front Node, which you store, is called Head, and the end Node (which you might want to store as well in a variable) is called Tail. Unlike arrays, Linked Lists aren't indexed. Rather, the list structure is in tact because the Head Node, which we store in a variable, points to the next Node. And that Node points to the next Node, etc., all the way to the end of the list.
Head --> Node --> Node --> Tail



Because of this, we have to iterate to get to a certain point in the List. To get to the end of the list, we just iterate until we find a Node that doesn't have a next Node. That's the Tail Node. Now that we know where the end is, just set temp.next to a new Node, but do so AFTER the loop. This means you have found the tail and are modifying it.
temp <-- head
while (temp.next != null)
     temp <-- temp.next

//now temp points to the tail Node of the List


Was This Post Helpful? 0
  • +
  • -

#14 pureblood40  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-November 10

Re: Linked Lists

Posted 01 March 2011 - 07:07 PM

I went and got help. And he explained this. While I was there I forgot to ask a question though. I am not pretty sure I got my link lists working but now I am get null pointer error when I try to play is using ShowFromMeOn. Do you want to see my code?

View Postmacosxnerd101, on 01 March 2011 - 07:00 PM, said:

This is a diagram of how Linked Lists work. The front Node, which you store, is called Head, and the end Node (which you might want to store as well in a variable) is called Tail. Unlike arrays, Linked Lists aren't indexed. Rather, the list structure is in tact because the Head Node, which we store in a variable, points to the next Node. And that Node points to the next Node, etc., all the way to the end of the list.
Head --> Node --> Node --> Tail



Because of this, we have to iterate to get to a certain point in the List. To get to the end of the list, we just iterate until we find a Node that doesn't have a next Node. That's the Tail Node. Now that we know where the end is, just set temp.next to a new Node, but do so AFTER the loop. This means you have found the tail and are modifying it.
temp <-- head
while (temp.next != null)
     temp <-- temp.next

//now temp points to the tail Node of the List


Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,686
  • Joined: 27-December 08

Re: Linked Lists

Posted 01 March 2011 - 07:09 PM

Yes. Basically though, you are invoking the method and the Node hasn't been initialized.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2