-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackList.java
134 lines (125 loc) · 3.37 KB
/
StackList.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
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
package project_stackqueuelinkedlist;
import project_stackqueuelinkedlist.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.*;
//import java.util.Stack;
/**
*
* @author hannahgsimon
*/
public class StackList <T extends Comparable> implements Comparable<T> {
//Stack<T> data = new Stack<>();
//Node data = new Node(null); Not needed because data isn't being stored in a specific reserved location, the LinkedList is only a cluster of values & pointers.
Node<T> tail = null, root = null;
public String getAuthorInfo()
{
return("Hannah Simon\n");
}
public void push(T item)
{
//data.push(item);
Node<T> newVal = new Node<>(item); //Runs constructor method of Node, item is passed in as val.
if (!hasData()) //LinkedList is empty
{
tail = root = newVal;
}
else
{
root.next = newVal;
root = newVal;
}
}
public T pop()
{
//return(data.pop());
T tmp1 = root.value;
if (hasData()) //LinkedList isn't empty
{
Node<T> tmp2 = tail;
if (tmp2.next == null) //LinkedList has one item.
{
root = null;
tail = null;
}
else
{
while (tmp2.next != root)
{
tmp2 = tmp2.next;
}
tmp2.next = null; //old root becomes null
root = tmp2; //new root
}
}
return tmp1;
}
public T peek()
{
// if (data.isEmpty())
// {
// return(null);
// }
// return(data.peek());
if (hasData() == false)
{
return(null);
}
return root.value;
}
@Override
public String toString()
{
//return(data.toString());
if (hasData())
{
String stackList = "";
Node<T> tmp = tail;
while (tmp != null)
{
stackList += (" ," + tmp.value.toString());
tmp = tmp.next;
}
StringBuilder stackListReversed = new StringBuilder();
stackListReversed.append(stackList);
stackListReversed.reverse();
stackListReversed.delete(stackListReversed.length() - 2, stackListReversed.length());
return ("[" + stackListReversed.toString() + "]");
}
return null;
}
@Override
public int compareTo(T o) {
// if (data.isEmpty() && o == null)
// {
// return(0);
// }
// else if (data.isEmpty())
// {
// return(-1);
// }
// else if (o == null)
// {
// return(1);
// }
// return(data.peek().compareTo(o));
if (hasData() == false && o == null)
{
return(0);
}
else if (hasData() == false)
{
return(-1);
}
else if (o == null)
{
return(1);
}
return peek().compareTo(o);
}
public boolean hasData()
{
//return(!data.isEmpty());
return (root != null);
}
}