Lab8: Due in class: 10 points

Document technical information

Format doc
Size 24.1 kB
First found May 22, 2018

Document content analysis

Category Also themed
Language
English
Type
not defined
Concepts
no text concepts found

Persons

Organizations

Places

Transcript

Lab8: Due in class: 10 points
Practice to build a class of linked list.
You need to do the following tasks.
1.
2.
3.
Run program. Printout input/output.
Study the structures of the program including different classes with their types, constructors and
methods. Then write an essay about the structure.
Submit the hard copy of 1. and 2. to your instructor by the end of the session.
Here is the program: ( program should work if you know how to import properly the classes.)
class ListNode {
// Data fields
String info;
ListNode link;
// data stored in the node
// link to next node
// Constructors
public ListNode() {info = ""; link = null;}
public ListNode(String i) {info = i; link = null;}
public ListNode(String i, ListNode l) {
info = i;
link = l;
}
} // class ListNode
class LinkList {
private ListNode head;
// reference to
// first list node
public void buildTestList() {
// Build a list with nodes p and q.
ListNode p = new ListNode("the");
ListNode q = new ListNode("hat");
p.link = q;
// Insert a new node between nodes p and q.
p.link = new ListNode("top", q);
// Insert a new node at head of list.
head = new ListNode("in", p);
//Append a new node to the end of the list.
q.link = new ListNode("box");
head.link = p.link; // Bypass node p.
p.link = null;
// Disconnect p from the list.
}
/*
Displays the linked list to which it is applied.
precondition : The last list node has a null link.
postcondition: The data fields of each list node
are displayed.
*/
public void traverse(ListNode next) {
if (next != null) {
System.out.print("Node:");
System.out.println(next.info);
traverse(next.link);
}
}
public void showListTraverse() {
buildTestList();
traverse(head);
}
} // class LinkList
// DUMMY TEST FILE. NOT AN EXAMPLE
// import linkList.*;
/* public */ class DemoList {
public static void main(String args[]) {
(new LinkList()).showListTraverse();
}
}
×

Report this document