-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForwardList.java
More file actions
135 lines (120 loc) · 4.06 KB
/
Copy pathForwardList.java
File metadata and controls
135 lines (120 loc) · 4.06 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import aud.Stack;
/**ForwardList represents a list with single linked nodes, that do have
a next-pointer. */
public class ForwardList<T> implements Iterable<T>{
//-----------------------------------------------------------------//
//------------- !!Do not change the following lines!! -------------//
public String toString() {
if (is_empty())
return "[]";
String rv = "[";
Node node = head_;
do {
rv += node.data_.toString();
if (node.next_ != head_)
rv += ",";
node = node.next_;
} while(node != null);
rv += "]";
return rv;
}
//-----------------------------------------------------------------//
//---------------- !!Insert your solution below!! -----------------//
//A Node is a single element int the ring
public class Node {
//---------------------------------------------------------------//
// !!! Do not change the existing lines in class Node!!! //
// !!! It is allowed to add new content !!! //
// !!! It is not allowed to add a previous pointer !!! //
//---------------------------------------------------------------//
public T data_;
public Node next_ = null;
public Node(T data, Node next) {
data_ = data;
next_ = next;
}
}
//-----------------------------------------------------------------//
public Node head_ = null;
//-----------------------------------------------------------------//
public ForwardList()
{}
//-----------------------------------------------------------------//
public void push_front(T obj) {
// TODO: implement adding elements to the front of the list
Node x = head_;
head_ = new Node(obj,x);
}
//-----------------------------------------------------------------//
public boolean is_empty() {
// TODO: should return `true` if the list is empty else `false`
return head_ == null;
}
//-----------------------------------------------------------------//
public void backTraverse() {
// TODO: implement a recursive method to print out reversed
Node x = head_;
System.out.print("[");
_backTraverse(x);
System.out.println("]");
}
public void _backTraverse(Node x)
{
if( x == null)
return;
else{
System.out.print(x.data_ + ",");
_backTraverse(x.next_);
}
}
//-----------------------------------------------------------------//
public class BackIterator implements java.util.Iterator<T> {
// TODO: implement an iterator that provides a reveres iteration
Stack<Node> stack;
public BackIterator(Node node) {
stack = new Stack<Node>();
Node temp = node;
while (temp != null) {
stack.push(temp);
temp = temp.next_;
}
}
public boolean hasNext() {
if (stack.is_empty())
return false;
else return true;
}
public T next() {
T next = stack.pop().data_;
return next;
}
}
//-----------------------------------------------------------------//
public BackIterator iterator() {
// TODO: return an instance of the iterator
return new BackIterator(head_);
}
//-----------------------------------------------------------------//
public static void main(String[] args) {
// TODO: test your code with appropriate examples
ForwardList<Integer> l = new ForwardList<>();
System.out.println("List is empty? " + l.is_empty());
for(int i = 1; i < 6; i++)
{
System.out.println(i + " added to List!");
l.push_front(i);
}
System.out.println("Elements in List : ");
//System.out.println(l.toString());
System.out.print("[");
for(int el : l)
{
System.out.print(el + ",");
}
System.out.println("]");
System.out.println("List is empty? " + l.is_empty());
System.out.println("Elements in List in Reverse Order(Back Traversal) : ");
l.backTraverse();
System.out.println();
}
}