Skip to content

Commit dacb217

Browse files
committed
Fix cyclelist. ALso fix the gradle build file.
1 parent 3b91a07 commit dacb217

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

cyclelist/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ sourceCompatibility = 1.6
99
version = '0.1'
1010
jar {
1111
manifest {
12-
attributes 'Implementation-Title': 'simplelist',
12+
attributes 'Implementation-Title': 'cyclelist',
1313
'Implementation-Version': version,
14-
'Main-Class': 'net.veroy.minibench.simplelist.SimpleList'
14+
'Main-Class': 'net.veroy.minibench.cyclelist.CycleList'
1515
}
1616
}
1717

cyclelist/src/main/java/net/veroy/minibench/cyclelist/CycleList.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private static int get_number_option(String arg)
2828
private static void create_list_sequential_node( MyLinkedList mylist,
2929
int number )
3030
{
31-
for (int i = 0; i < number; i++) {
31+
for (int i = 1; i < (number+1); i++) {
3232
mylist.add(new Node(new Integer(i)));
3333
}
3434
}
@@ -38,7 +38,7 @@ private static void create_list_sequential_node( MyLinkedList mylist,
3838
public static void main( String[] args )
3939
{
4040
// Check for positional arguments
41-
if (args.length != 2) {
41+
if (args.length != 1) {
4242
print_help();
4343
System.exit(1);
4444
}
@@ -50,12 +50,17 @@ public static void main( String[] args )
5050
create_list_sequential_node( mylist,
5151
number );
5252
Integer total = 0;
53-
while (!mylist.isEmpty()) {
54-
// System.out.println(cur);
53+
Integer count = 0;
54+
mylist.resetCurrent();
55+
boolean flag = mylist.moveNext();
56+
while (flag) {
57+
// DEBUG: System.out.println(" XXX:" + count);
5558
total += mylist.current.getValue();
56-
mylist.next();
59+
flag = mylist.moveNext();
60+
count++;
5761
}
58-
// System.out.println( "Total: " + total );
62+
System.out.println( "Total: " + total );
63+
System.out.println( "Count: " + count );
5964
}
6065

6166
private static void print_help()

cyclelist/src/main/java/net/veroy/minibench/cyclelist/MyLinkedList.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class MyLinkedList
1616
public MyLinkedList()
1717
{
1818
// Always a cycle
19+
this.head = new Node(0xdeadbeef);
20+
this.end= new Node(0xbeefdead);
1921
this.head.setNext(end);
2022
this.end.setNext(head);
2123
this.current = head;
@@ -31,28 +33,44 @@ private boolean is_end( Node mynode )
3133
return (this.end == mynode);
3234
}
3335

36+
public boolean moveNext()
37+
{
38+
Node mynext = this.current.getNext();
39+
assert( mynext != this.head );
40+
if (mynext != this.end) {
41+
this.current = mynext;
42+
return true;
43+
} else {
44+
return false;
45+
}
46+
}
47+
48+
public void resetCurrent()
49+
{
50+
this.current = this.head;
51+
}
52+
3453
private boolean sanity_check()
3554
{
36-
return (this.end.getNext() == this.head);
55+
return ( (this.end.getNext() == this.head) &&
56+
(this.current != this.end) );
3757
}
3858

3959
public void add( Node newnode )
4060
{
41-
// Search for node that cycles back to head and add there.
42-
Node prev = this.end;
43-
Node current = this.head;
44-
while (current.getNext() != this.end) {
45-
prev = current;
46-
current = current.getNext();
47-
}
48-
assert( current.getNext() == this.end );
49-
prev = current;
61+
// Add at current
62+
Node oldnext = this.current.getNext();
5063
current.setNext( newnode );
51-
newnode.setNext( this.end );
64+
newnode.setNext( oldnext );
5265
}
5366

5467
public boolean isEmpty()
5568
{
5669
return (this.head.getNext() == this.end);
5770
}
71+
72+
public boolean atEnd()
73+
{
74+
return (this.current.getNext() == this.end);
75+
}
5876
}

cyclelist/src/main/java/net/veroy/minibench/cyclelist/Node.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ public class Node
1010
public Integer value;
1111
public Node next;
1212

13-
public Node( Integer new_number,
14-
Node next_node )
13+
public Node( Integer new_number )
1514
{
1615
this.value = new_number;
17-
this.next = next_node;
1816
}
1917

2018
public void setNext( Node next_node )

0 commit comments

Comments
 (0)