



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Linked Lists
This assignment begins our discussions of data structures. In this assignment, you will implement a data struc-
ture called a doubly linked list. Read the whole handout before starting. Near the end, we give important instructions
on testing.
At the end of this handout, we tell you what and how to submit. We will ask you for the time spent in doing A3,
so please keep track of the time you spend on it. We will report the minimum, average, and maximum.
Learning objectives
Learn about and master the complexities of doubly linked lists.
Learn a little about inner classes.
Learn a little about generics.
Learn and practice a sound methodology in writing and debugging a small but intricate program.
You may do this assignment with one other person. Both members of the group should get on the CMS and do
what is required to form a group well before the assignment due date. Both must do something to form the group:
one proposes, the other accepts.
People in a group must work together. It is against the rules for one person to do some programming on this as-
signment without the other person sitting nearby and helping. Take turns ādrivingā āusing the keyboard and mouse.
With the exception of your CMS-registered group partner, you may not look at anyone else's code, in any form,
or show your code to anyone else (except the course staff), in any form. You may not show or give your code to an-
other student in the class. This all applies also to code from similar assignments in previous semesters of 2110.
If you don't know where to start, if you don't understand testing, if you are lost, etc., SEE SOMEONE IMMEDI-
ATELY āan instructor, a TA, a consultant. If you find yourself spending more than an hour or two on one issue, not
making any progress, STOP and get help. Some pondering and thinking is helpful, but too much of it just wastes
your time. A little in-person help can do wonders. See the course webpage for contact information.
The diagram below represents the list of values [6, 7, 3]. The leftmost object, LL@7, is called the header. It con-
tains two values: the size of the list, 3, and a pointer to the first node of the list. Each of the other three objects, of
class N(for Node) contains a value of the list and a pointer to the next node of the list āor null if there are no
more nodes in the list. This data structure is call a singly linked list , or just linked list.
One chooses a data structure that optimizes a program in some way, making the most frequently used operations
as fast as possible. For example, maintaining a list in an array has the advantage that any element, say number i, can
val
next N@ 8
0irst N@
size 3
d LL@
val
next
val 3
null next
be referenced in constant time, using (typically) b[i]. But maintaining a list in an array has disadvantages: (1) The
size of the array has to be determined when the array is first created, and (2) Inserting or removing values at the be-
ginning takes time proportional to the size of the list.
A singly linked list has these advantages: (1) The list can be any size, and (2) Inserting (or removing) a value at
the beginning can be done in constant time. It takes just a few operations, bounded above by some constant: Create a
new object and change a few pointers. On the other hand, to reference element number i of the list takes time pro-
portional to iāone has to sequence through all the nodes numbered 0..i-1 to find it.
At this point, you will gain more understanding by doing the following, to construct a linked list that represents
the sequence [4, 6, 7, 3]. Do what follows, donāt just read it. (1) Copy the linked list diagram shown near the bottom
of the previous page. (2) Below that diagram, draw a new object of class N , with 4 in field val and null in field
next. (3) Now change field next of the new node to point to node N@1 and change field first to point to the
new node. (4) Finally, change field d.size to 4. The diagram now represents the list [4, 6, 7, 3].
A singly linked list has field first in the header and field next in each node, as shown above. A doubly
linked list has, in addition, a field last in the header and a field prev in each node, as shown below. In the dia-
gram below, one can traverse the list of values in reverse: first d.last.val, then d.last.prev.val, then
d.last.prev.prev.val. This doubly linked lists represents the same sequence [6, 7, 3] as the singly linked list
given above ābut the data structure lets us easily enumerate the values in reverse, [3, 7, 6], as well as forward.
A major advantage of a doubly linked list over a singly linked list is that, given a node n (containing something
like N@8), one can get to nās previous and next nodes in constant time. For example, removing node n from the list
can be done in constant time, but in a singly linked list, the time may depend on the length of the list (why?).
In A3, you will implement a doubly linked list using the representation below. The header will be of class DLL
(for doubly linked list), and nodes will objects of class Node (given by N below), Study this diagram carefully. All
further work rests on understanding this data structure.
We often write such linked lists without the tabs on the objects and even without names in the pointer fields, as
shown below. No useable information is lost, since the arrows take the place of the object pointer-names.
0irst N@
size 3
last N@
d DLL@
next
val
null prev
prev
val
next
prev
val
null next
0irst
size
last
d
next
val
null prev prev
val
next
prev
val
null next
spent on this assignment. If you are doing the project alone, replace only the first nnnn. Please do all this careful-
ly. If the minutes is 0, replace mm by 0. We wrote a program to extract these times, and when you donāt actually
replace hh and mm but instead write in free form, that causes us trouble. Also, please take a few minutes to tell us
what you thought of this assignment.
Grading : The correctness of the 7 methods you write is worth 62. The testing of each is worth 4-5 points: we will
look carefully at class DLLTest. If you donāt test a method properly, points might be deducted in two places: (1)
the method might not be correct and (2) it was not tested properly.
Note that some methods that you will write have an extra comment in the body, giving more instructions and hints
on how to write it. Follow these instructions carefully. Also, writing some methods in terms of calls on previously
written methods may save you time.
Writing a method that changes the list : Five of the methods you write change the list in some way. These methods
are short, but you have to be extremely careful to write them correctly. It is best to draw the linked list before the
change; draw what it looks like after the change; note which variables have to be changed; and then write the code.
Not doing this is sure to cause you trouble.
Be careful with a method like append(v) because a single picture does not tell the whole story. Here, two cases
must be considered: the list is empty and it is not empty. So two sets of before-and-after diagrams should be drawn.
This will probably mean a method that uses an if-statement.
Methodology on testing : Write and test one group of methods at a time! Writing all and then testing will waste your
time, for if you have not fully understood what is required, you will make the same mistakes many times. Good pro-
grammers write and test incrementally, gaining more and more confidence as each method is completed and tested.
Determining what test cases to use : Please read the pdf file found in the JavaHyperText at entry ātestingā, especial-
ly the last part of that pdf file. It is important.
What to test and how to test it: Determining how to test a method that changes the linked list can be time consum-
ing and error prone. For example: after inserting 6 before 8 in list [2, 7, 8, 5], you must be sure that the list is now [2,
7, 6, 8, 5]. What fields of what objects need testing? What prev and next fields? How can you be sure you didnāt
change something that shouldnāt be changed?
To remove the need to think about this issue and to test all fields automatically , you must must must do the follow-
ing. In class DLL, FIRST write function toStringRev as best you can. In writing it, do not use field size. In-
stead, use only fields last in class DLL and the prev and val fields of nodes. You can look at how we wrote
toString āthat can help you. Do not put in JUnit a testing procedure for toStringRev, because it will be
tested when testing procedure append, just as getters were tested in testing a constructor in A1.
For example, after completing toStringRev, you can test that it works properly on the empty list using this
method:
@Test
public void testConstructor() {
DLL
assertEquals(ā[]ā, b.toString());
assertEquals(ā[]ā, b.toStringRev());
assertEquals(0, b.size());
Now write procedure append. Testing append will fully test toStringRev. You are testing those two
method together. Each call on append will be followed by 3 assertEquals calls, similar to those in test-
Constructor:
@Test
public void testAppend() {
DLL
ll.append("Sampson");
assertEquals(ā[Sampson]ā, ll.toString());
assertEquals(ā[Sampson]ā, ll.toStringRev());
assertEquals(1, ll.size());
The call ll.toString() tests field first, all fields next, and all fields val. The call ll.toStringRev()
tests field last, all fields prev, and all fields val. The call on ll.size() tests field size. Thus, all fields are tested.
Test all your methods that change the linked list with three such assertEquals calls. That way, you donāt have to
think about what fields to test; you test them the all.
Would you have thought of using toString and toStringRev like this? It is useful to spend time thinking
not only about writing the code but also about how to simplify testing.