| 1. |
Set up a new project for today's lab in Eclipse (you may use PSPad if you prefer).
- Start Eclipse.
- Switch to the workspace on your flash drive.
- From the File menu select New , then Java Project.
- For Project Name: type CS 2 Lab 7 and click Finish.
|
| 2. |
Create a linked list class with methods to iterate through the list.
- From the File menu select New, then Class.
- Make sure the entry for Source folder: says CS 2 Lab 7/src.
- For Name: type MyLinkedList and click Finish.
- There should now be an editor open with the following code:
public class MyLinkedList {
}
- For this lab we will also be using Java Generics to create a data structure that can contain any type.
To do this, modify the class declaration you just created to read as follows:
public class MyLinkedList<SomeType> {
}
- Add the following code before the class:
class Link<SomeType> {
public SomeType data;
public Link<SomeType> next;
public Link(SomeType data) {
this.data = data;
this.next = null;
}
public void display() {
System.out.println("{data -> "+ data + "}" );
}
}
- Add the following code to the class:
private Link<SomeType> first;
private Link<SomeType> current;
private Link<SomeType> previous;
public MyLinkedList() {
first = null;
current = null;
previous = null;
}
public void next() {
if(!atEnd()) {
previous = current;
current = current.next;
}
}
public SomeType getCurrent() {
if(isEmpty()) {
return null;
}
else {
return current.data;
}
}
public void reset() {
current = first;
previous = null;
}
public boolean isEmpty() {
return first == null;
}
public boolean atEnd() {
return current == null;
}
public boolean atStart() {
return previous == null;
}
public void insertAfterCurrent( SomeType item) {
// YOUR CODE HERE
}
public SomeType deleteCurrent( ) {
// YOUR CODE HERE
}
public void display( ) {
Link<SomeType> currentToDisplay = first;
while(currentToDisplay != null) {
currentToDisplay.display();
currentToDisplay = currentToDisplay.next;
}
}
- This version of the linked list data structure contains methods to iterate through the list, and extra fields to keep track of where the user is in the list:
- current always points to the current Link in the list,
unless the list is empty or the user has reached the end of the list in which case it is null
- previous always points to the Link before the current Link,
unless the list is empty or the user is at the beginning of the list in which case it is null
- The next method advances current and previous unless the user is at the end of the list.
- The getCurrent returns the item stored in the current Link
- The reset method moves current and previous to the beginning of the list.
- The atEnd and atStart return true when the user is at the end or beginning of the list respectively.
- You must now implement the insertAfterCurrent and removeCurrent methods as follows:
- insertAfterCurrent should create a Link object,
insert it after the curent Link, set previous to the current Link, and
set current to the new Link.
- removeCurrent should delete the current Link from the list,
and set current to the next Link.
- Make sure both of these methods work when the current Link is the first link, in the middle, or the last link.
- Also make sure both of these methods handle an empty list correctly.
- From the File menu select Save.
|
| 3. |
Create and run the MyLinkedListTester class
- From the File menu select New, then Class.
- For Name: type MyLinkedListTester,
check the box next to public static void main(String args[]),
and click Finish.
- There should now be another editor open with the following code:
public class MyLinkedListTester {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
- To create a MyLinkedList object that contains String objects, add the following line to your main method:
MyLinkedList<String> queue = new MyLinkedList<String>();
- Now test out the methods you just implemented.
- To iterate through a linked list, do the following:
list.reset();
while(!list.atEnd()) {
// do something with the current item
list.next();
}
or if you prefer, you can use a for loop:
for(list.reset(); !list.atEnd(); list.next()) {
// do something with the current item
}
- To run your program, click on the down arrow to the right of the green button with the white triangle on it and select Run Configurations.
- For Name: type Lab 7 Test,
for Project: type CS 2 Lab 7,
for Main class: type MyLinkedListTester,
then click Run.
- A console should pop up under your editor with the results of your tests.
|
| 4. |
Upload your files to the CS Server using FileZilla.
- Start up FileZilla
- Type in the following:
| Host: | cs.wvutech.edu |
| Username: | your username here |
| Password: | your password here |
| Port: | 22 |
If you do not remember your password, I will reset it for you.
- Click Quickconnect.
Several files should pop up in the right panel, including a folder called handin.
Double click on this folder.
- For Local Site navigate to your workspace, double click the folder for the CS 2 Lab 7 project,
then double click on the src folder. Your source files should be in this folder.
- Drag all of the files ending in .java to your handin folder on the CS server.
|