-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitCounter.java
94 lines (77 loc) · 1.44 KB
/
BitCounter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class AListNode{
public int data;
public AListNode next;
AListNode(int i, AListNode next){
data=i;
this.next=next;
}
}
class AList{
private static int uses=0; // used to track number of Alist operations
private AListNode head;
public boolean isEmpty(){
// checks for empty list
uses++;
if(head==null){
return true;
}
return false;
}
public void insertFront(int i){
//inserts integer as MSB
uses++;
AListNode newNode = new AListNode(i,head);
head = newNode;
}
public int deleteFront() throws Exception{
//removes MSB
uses++;
int temp=head.data;
head=head.next;
return temp;
}
public void insertRear(int i){
//insert integer as LSB
if(head==null){
insertFront(i);
}
else{
uses++;
AListNode curr =head;
AListNode newNode = new AListNode(i,null);
while(curr.next!=null){
curr=curr.next;
}
curr.next=newNode;
}
}
public int deleteRear() throws Exception{
//remove LSB
uses++;
AListNode curr =head;
if(curr.next==null){
head=null;
return curr.data;
}
while(curr.next.next!=null){
curr=curr.next;
}
int temp=curr.next.data;
curr.next=null;
return temp;
}
///////test functions, see BitCounterTest.java
public void test_print(){
AListNode tester = head;
while(tester!=null){
System.out.print(tester.data);
tester=tester.next;
}
}
public void test_uses(){
System.out.println(" "+uses);
}
public void test_reset_uses(){
uses=0;
}
}