Difference between revisions of "CSC212 Lab 6 2014"

From dftwiki3
Jump to: navigation, search
(Question 3: Testing!)
Line 12: Line 12:
 
* Create a new file with a class called '''BasicLinkedList'''.   
 
* Create a new file with a class called '''BasicLinkedList'''.   
 
* Put the code from the video that creates a list of 3 elements in the main() method of your new class.
 
* Put the code from the video that creates a list of 3 elements in the main() method of your new class.
* You can display your list with this code:
+
* You can printAll your list with this code:
 
<br />
 
<br />
 
::<source lang="java">
 
::<source lang="java">
 
for ( IntSLLNode it = head; it  != null; it = it.next ) {
 
for ( IntSLLNode it = head; it  != null; it = it.next ) {
System.out.println( it.info );
+
    System.out.println( it.info );
 
}
 
}
 
</source>
 
</source>
Line 25: Line 25:
 
* Make '''head''' and '''tail''' two private members of the new class
 
* Make '''head''' and '''tail''' two private members of the new class
 
* Add a constructor that will set head and tail to null
 
* Add a constructor that will set head and tail to null
* Add an '''insertFront( int el )''' method that inserts a new integer at the front of the list.  Note that the code is different depending whether the list is empty, or not.
+
* Add an '''addToHead( int el )''' method that inserts a new integer at the front of the list.  Note that the code is different depending whether the list is empty, or not.
* Add an '''insertTail( int el )''' method that inserts a new integer at the end of the list.  Note, as well, that the code is different depending on whether the list is empty or not.
+
* Add an '''addToTail( int el )''' method that inserts a new integer at the end of the list.  Note, as well, that the code is different depending on whether the list is empty or not.
* Add a '''display()''' method that will use a loop to display the contents of your list.
+
* Add a '''printAll()''' method that will use a loop to printAll the contents of your list.
 
* Add this code in the '''main()''' method:
 
* Add this code in the '''main()''' method:
 
<br />
 
<br />
Line 34: Line 34:
 
     MyLinkedList L = new MyLinkedList();
 
     MyLinkedList L = new MyLinkedList();
  
     L.insertFront( 5 );
+
     L.addToHead( 5 );
     L.insertFront( 10 );
+
     L.addToHead( 10 );
     L.insertTail( 3 );
+
     L.addToTail( 3 );
     L.display();
+
     L.printAll();
  
 
}
 
}
Line 51: Line 51:
 
     MyLinkedList L = new MyLinkedList();
 
     MyLinkedList L = new MyLinkedList();
  
     L.insertTail( 30 );
+
     L.addToTail( 30 );
     L.insertTail( 20 );
+
     L.addToTail( 20 );
     L.insertTail( 10 );
+
     L.addToTail( 10 );
  
     L.display();
+
     L.printAll();
  
 
}
 
}
Line 67: Line 67:
 
* Test your method.
 
* Test your method.
 
<br />
 
<br />
==Question 5: add a ''removeFront( )'' Method==
+
==Question 5: add a ''deleteFromHead( )'' Method==
 
<br />
 
<br />
 +
* First, figure out on a piece of paper how to remove the front element of a non-empty list.
 +
* Once you have a diagram ready, code the series of actions that need to take place.  We will assume that ''deleteFromHead()'' will always be called on a non-empty list.  The user will have to use ''isEmpty()'' first before trying to remove anything.
 +
* Make your method return the integer in the element just removed.
 +
* Test your new method as follows:
 +
<br />
 +
::<source lang="java">
 +
public static int main( String[] args ) {
 +
    MyLinkedList L = new MyLinkedList();
 +
 +
    L.addToTail( 30 );
 +
    L.addToTail( 20 );
 +
    L.addToTail( 10 );
 +
 +
    while ( ! L.isEmpty() ) {
 +
        int el = L.deleteFromHead();
 +
        System.out.println( "--- Just removed: " + el );
 +
        System.out.print( "L = " );
 +
        L.printAll();
 +
    }
 +
}
 +
</source>

Revision as of 10:07, 1 October 2014

--D. Thiebaut (talk) 10:56, 1 October 2014 (EDT)


Singly-Linked Lists


Watch the video below first.


Question 1


  • Take the IntSLLNode code from the video and put it in a separate class in your directory. Make sure it's public.
  • Create a new file with a class called BasicLinkedList.
  • Put the code from the video that creates a list of 3 elements in the main() method of your new class.
  • You can printAll your list with this code:


for ( IntSLLNode it = head; it  != null; it = it.next ) {
    System.out.println( it.info );
}


Question 2: A Better Linked-List


  • Create a new class called MyLinkedList
  • Make head and tail two private members of the new class
  • Add a constructor that will set head and tail to null
  • Add an addToHead( int el ) method that inserts a new integer at the front of the list. Note that the code is different depending whether the list is empty, or not.
  • Add an addToTail( int el ) method that inserts a new integer at the end of the list. Note, as well, that the code is different depending on whether the list is empty or not.
  • Add a printAll() method that will use a loop to printAll the contents of your list.
  • Add this code in the main() method:


public static int main( String[] args ) {
    MyLinkedList L = new MyLinkedList();

    L.addToHead( 5 );
    L.addToHead( 10 );
    L.addToTail( 3 );
    L.printAll();

}


  • Verify that you get a list with 10, 5, and 3 listed in that order.

Question 3: Testing!

  • Try this code, and verify that it works with your list.


public static int main( String[] args ) {
    MyLinkedList L = new MyLinkedList();

    L.addToTail( 30 );
    L.addToTail( 20 );
    L.addToTail( 10 );

    L.printAll();

}


  • Make sure you fix any errors you may get!


Question 4: add an isEmpty() Method


  • Add an isEmpty() method. Make it return true if the list is empty, false otherwise.
  • Test your method.


Question 5: add a deleteFromHead( ) Method


  • First, figure out on a piece of paper how to remove the front element of a non-empty list.
  • Once you have a diagram ready, code the series of actions that need to take place. We will assume that deleteFromHead() will always be called on a non-empty list. The user will have to use isEmpty() first before trying to remove anything.
  • Make your method return the integer in the element just removed.
  • Test your new method as follows:


public static int main( String[] args ) {
    MyLinkedList L = new MyLinkedList();

    L.addToTail( 30 );
    L.addToTail( 20 );
    L.addToTail( 10 );

    while ( ! L.isEmpty() ) {
        int el = L.deleteFromHead();
        System.out.println( "--- Just removed: " + el );
        System.out.print( "L = " );
        L.printAll();
    }
}