CSC212 Lab 6 2014
--D. Thiebaut (talk) 10:56, 1 October 2014 (EDT)
Contents
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 display 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 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 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 a display() method that will use a loop to display the contents of your list.
- Add this code in the main() method:
public static int main( String[] args ) { MyLinkedList L = new MyLinkedList(); L.insertFront( 5 ); L.insertFront( 10 ); L.insertTail( 3 ); L.display(); }
- 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.insertTail( 30 ); L.insertTail( 20 ); L.insertTail( 10 ); L.display(); }
- Make sure you fix any errors you may get!