Lab 1: Disk-based data structures: A disk-based linked list

laboredbumbaileySoftware and s/w Development

Jun 7, 2012 (5 years and 3 months ago)

277 views

http://www.d.umn.edu/~cprince/courses/cs5631spring06/labs/lab1/
Lab 1: Disk-based data structures: A disk-based linked list
1
of
6
Lab 1: Disk-based data structures: A disk-based
linked
list
CS
5631,
Spring
2006
Due Tuesday, February 7 (20 points)
Revisions
2/1/06: Added web drop link. (See bottom of this page).
2/1/06: Added note about Netbeans. If you use Netbeans, be careful that when you zip up
your files for submission that you have included the source files. It can be easy for
Netbeans to have entirely separate directory locations for your project and your source
code.
Introduction
This lab has two purposes. First, it is intended to provide an introduction to programming
in Java, for those of you without much experience in Java. Second, it will give you
experience with a concept we will be using in the File System labs. This concept is
disk-based data structures
. File systems naturally reside on disks-- they are often used to
provide more permanent data storage than volatile memory. File systems are actually
complex data objects. For example, they contain directories, and they contain files. Some
file systems even provide methods of restoration given certain types of failures (e.g.,
"journalling" file systems). Thus, we can talk about file systems as being comprised of
disk-based data structures. To give you experience with disk-based data structures before
we start on the File Systems series of labs in this course, this first lab will have you work
with a more familiar data structure-- a linked list-- on disk. That is, you will implement a
disk-based linked list. Your linked list will be represented in a single file on disk.
Resources
"
The Java Tutorial
"
Java Language Reference
(online and downloadable in HTML and PDF formats)
Sun API documentation
(Java 2, 5.0).
Java 2 SDK (Software Development Kit) and JRE (Java Runtime Environment)
Netbeans
: A program for editing, constructing GUI's, and running Java programs
Goal
You will program methods to access the nodes of a linked list represented on disk. Typical
for disk-based data structures, you will not keep more than a few nodes from your linked
list in RAM at any one time. That is, you wil not keep the entire linked structure in RAM.
The advantage of this technique is that large lists do not use more RAM than small lists,
and because changes to your linked list will be immediately written out to disk, if your
http://www.d.umn.edu/~cprince/courses/cs5631spring06/labs/lab1/
Lab 1: Disk-based data structures: A disk-based linked list
2
of
6
program crashes (e.g., the computer crashes), you are less likely to loose your data.
You should create a
DiskLinkedList
class, a Main testing class, and any other additional
classes you find useful for this assignment. For the Main testing class, provide a command
line (menu) interface that enables testing of
all of the methods
provided by your
DiskLinkedList class. The following is a snippet of some of my testing:
chris$ java TestLinkedList
main: Enter command:
0) Exit program.
1) Initialize & open linked list on disk.
2) Close linked list file.
3) Open existing linked list file.
4) Print the list contained in the file.
5) Print all nodes in the file (raw).
6) Insert a new node into the list.
7) Delete node from the list.
Enter command: 1
DiskLinkedList.Initialize: SUCCESS: Initialized disk-based linked list.
DiskLinkedList.Initialize: SUCCESS: List has 10 data nodes.
DiskLinkedList.Initialize: SUCCESS: Each with 520 bytes.
DiskLinkedList.Open: SUCCESS: Opened disk-based linked list.
DiskLinkedList.Open: SUCCESS: List has 10 data nodes.
DiskLinkedList.Open: SUCCESS: Each of with 520 bytes.
main: Enter command:
0) Exit program.
1) Initialize & open linked list on disk.
2) Close linked list file.
3) Open existing linked list file.
4) Print the list contained in the file.
5) Print all nodes in the file (raw).
6) Insert a new node into the list.
7) Delete node from the list.
Enter command: 4
Header information for the list:
maxListLength= 10
numNodesUsed= 0
firstNodeIndex= 0
lastNodeIndex= 0
Start of contents of the list:
End of contents of the list.
main: Enter command:
0) Exit program.
1) Initialize & open linked list on disk.
2) Close linked list file.
3) Open existing linked list file.
4) Print the list contained in the file.
5) Print all nodes in the file (raw).
6) Insert a new node into the list.
7) Delete node from the list.
Enter command: 6
Enter the string for the new node:
node1
main: Enter command:
0) Exit program.
1) Initialize & open linked list on disk.
2) Close linked list file.
http://www.d.umn.edu/~cprince/courses/cs5631spring06/labs/lab1/
Lab 1: Disk-based data structures: A disk-based linked list
3
of
6
3) Open existing linked list file.
4) Print the list contained in the file.
5) Print all nodes in the file (raw).
6) Insert a new node into the list.
7) Delete node from the list.
Enter command: 6
Enter the string for the new node:
node2
main: Enter command:
0) Exit program.
1) Initialize & open linked list on disk.
2) Close linked list file.
3) Open existing linked list file.
4) Print the list contained in the file.
5) Print all nodes in the file (raw).
6) Insert a new node into the list.
7) Delete node from the list.
Enter command: 6
Enter the string for the new node:
node3
main: Enter command:
0) Exit program.
1) Initialize & open linked list on disk.
2) Close linked list file.
3) Open existing linked list file.
4) Print the list contained in the file.
5) Print all nodes in the file (raw).
6) Insert a new node into the list.
7) Delete node from the list.
Enter command: 4
Header information for the list:
maxListLength= 10
numNodesUsed= 3
firstNodeIndex= 1
lastNodeIndex= 3
Start of contents of the list:
Node 1: next= 2; len= 5; string= node1
Node 2: next= 3; len= 5; string= node2
Node 3: next= 0; len= 5; string= node3
End of contents of the list.
main: Enter command:
0) Exit program.
1) Initialize & open linked list on disk.
2) Close linked list file.
3) Open existing linked list file.
4) Print the list contained in the file.
5) Print all nodes in the file (raw).
6) Insert a new node into the list.
7) Delete node from the list.
Enter command: 5
DiskLinkedList.PrintRaw: There are 3 nodes in the list.
Node 1: next= 2; len= 5; string= node1
Node 2: next= 3; len= 5; string= node2
Node 3: next= 0; len= 5; string= node3
Node 4: next= -1; len= 0; string=
Node 5: next= -1; len= 0; string=
Node 6: next= -1; len= 0; string=
Node 7: next= -1; len= 0; string=
http://www.d.umn.edu/~cprince/courses/cs5631spring06/labs/lab1/
Lab 1: Disk-based data structures: A disk-based linked list
4
of
6
Node 8: next= -1; len= 0; string=
Node 9: next= -1; len= 0; string=
Node 10: next= -1; len= 0; string=
main: Enter command:
0) Exit program.
1) Initialize & open linked list on disk.
2) Close linked list file.
3) Open existing linked list file.
4) Print the list contained in the file.
5) Print all nodes in the file (raw).
6) Insert a new node into the list.
7) Delete node from the list.
Enter command: 7
Enter string of node to delete:
node1
main: Enter command:
0) Exit program.
1) Initialize & open linked list on disk.
2) Close linked list file.
3) Open existing linked list file.
4) Print the list contained in the file.
5) Print all nodes in the file (raw).
6) Insert a new node into the list.
7) Delete node from the list.
Enter command: 4
Header information for the list:
maxListLength= 10
numNodesUsed= 2
firstNodeIndex= 2
lastNodeIndex= 3
Start of contents of the list:
Node 1: next= 3; len= 5; string= node2
Node 2: next= 0; len= 5; string= node3
End of contents of the list.
main: Enter command:
0) Exit program.
1) Initialize & open linked list on disk.
2) Close linked list file.
3) Open existing linked list file.
4) Print the list contained in the file.
5) Print all nodes in the file (raw).
6) Insert a new node into the list.
7) Delete node from the list.
Enter command: 5
DiskLinkedList.PrintRaw: There are 2 nodes in the list.
Node 1: next= -1; len= 5; string= node1
Node 2: next= 3; len= 5; string= node2
Node 3: next= 0; len= 5; string= node3
Node 4: next= -1; len= 0; string=
Node 5: next= -1; len= 0; string=
Node 6: next= -1; len= 0; string=
Node 7: next= -1; len= 0; string=
Node 8: next= -1; len= 0; string=
Node 9: next= -1; len= 0; string=
Node 10: next= -1; len= 0; string=
main: Enter command:
0) Exit program.
1) Initialize & open linked list on disk.
http://www.d.umn.edu/~cprince/courses/cs5631spring06/labs/lab1/
Lab 1: Disk-based data structures: A disk-based linked list
5
of
6
2) Close linked list file.
3) Open existing linked list file.
4) Print the list contained in the file.
5) Print all nodes in the file (raw).
6) Insert a new node into the list.
7) Delete node from the list.
Enter command: 0

Some details
I provided a "PrintRaw" method for my DiskLinkedList class. This reads each node from
disk, and prints out its contents, independently of whether or not it is presently part of the
linked list. I found this useful for debugging my program code.
Do not represent all of your linked list nodes that are present in the disk file in RAM! This
goes against the nature of the assignment. If you do this, you may receive a 0 on the
assignment.
In general, you need to write out nodes to disk immediately after you make
changes to them.
How can you tell if you are representing all of your linked list nodes in
RAM? Well, ask your self this question: If you increased the maximum number of possible
nodes in your file containing the linked list nodes, would that increase the RAM your
program uses? If you answer "yes" to this, you are very very likely doing this assignment
incorrectly. If you are unclear about this, please see your instructor.
Your linked list should have a 'header' node containing information such as the maximum
number of nodes in the list, the current number of nodes, and pointers to the first and last
nodes of the list.
Pointers to nodes in your list will not be addresses in RAM memory. Instead, they will be
indices of nodes in the linked list represented in a disk file. Node 0 in your disk file should
be the linked list header. Nodes 1 through N (where N is the max number of nodes that can
be represented by your disk-based linked list) will be nodes that can be used by your linked
list. Use node index 0 to indicate "end-of-list" and node index -1 to indicate "unused".
Your linked list nodes should contain character data (char type in Java). Each of the nodes
in my list can contain at most 256 characters of data. Note that in Java, a "char" is
represented by 2 bytes.
Java exceptions (e.g., for java.io.* classes) need to be handled in try/catch blocks in your
program.
The
java.io.File
class may be useful to determine if a file already exists.
The
java.io.RandomAccessFile
class will likely be useful to have a file open for both
reading and writing. In addition to opening and closing files, you will need to use the
seek
,
read
, and
write
methods. Your program should not continually reopen the file containing
the linked list. Open it once at the start, and then close it after all operations have been done.
The
java.io.DataOutputStream
class can be used to initialize your linked list in your file.
Use plenty of output statements in your program to indicate what is going on. E.g., see my
output above. Also: If an exception or other error occurs, make sure you have a relevant
output statement.
Your linked list deletion should operate by searching for the contents of a string (i.e., the
http://www.d.umn.edu/~cprince/courses/cs5631spring06/labs/lab1/
Lab 1: Disk-based data structures: A disk-based linked list
6
of
6
string contents of the node), not by giving a node index. Your deletion code should traverse
the linked list structure, and not just iterate over the nodes in the file.
Use proper software engineering style--
(a) Document your methods (comments before each method indicating the
purpose of the method), and document your program code that implements the
methods.
(b) Use named constants instead of numeric values where this improves
readability.
(c) Use variable names and method names that have meaning in the context of
the program you are writing.
Submission
By the due date, turn in hardcopy (a printout) of your program code (.java files). Also turn
in hardcopy of your testing output. Make sure to be systematic in your testing. For
example, on the linked list deletion, you should be sure to show us that you have tested (a)
deletion from the start of the list, (b) deletion from somewhere in the middle of the list, and
(c) deletion from the end of the list. Also turn your program code on the web drop.
Webdrop
: upload your assignment (.zip file of all the working program code and classes)
by the due date to:
https://webdrop.d.umn.edu/
Please make sure your program code runs
when unzipped. It would be convenient if you told us how to run your program code (e.g.,
cd <directory>; java <FileName>).
If you use Netbeans, be careful that when you zip up your files for submission that you
have included the source files. It can be easy for Netbeans to have entirely separate
directory locations for your project and your source code.