-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstantTimeStack.java
More file actions
145 lines (121 loc) · 3.19 KB
/
Copy pathConstantTimeStack.java
File metadata and controls
145 lines (121 loc) · 3.19 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
136
137
138
139
140
141
142
143
144
145
package edu.cofc.cs.csci230;
import java.util.EmptyStackException;
/**
* A LIFO stack that has constant time complexity O(1) for
* all three stack interface methods (i.e., push, pop, and
* peek).
*
* This data structure was discussed in class along with the
* operations, please review your notes.
*
* @author CSCI 230: Data Structures and Algorithms Fall 2016
*
* @param <AnyType>
*/
public class ConstantTimeStack<AnyType extends Comparable> implements Stack<AnyType>{
/**
* private instance variables
*/
private SinglyLinkedList<AnyType> list = new SinglyLinkedList<AnyType>();
/**
* Pushes an item onto the top of this stack in constant
* time O(1)
*
* @param t the item to be pushed onto this stack.
*/
public void push(AnyType t) {
list.add(0,t);
/**
* -------------------------------------------
* TODO: You fully implement this method
*
* Note: Your push solution must be a constant
* time O(1) operation
*
*/
} // end push() method
/**
* Removes the object at the top of this stack and return the
* item in constant time O(1)
* .
* @return The item at the top of this stack
* @throws EmptyStackException - if this stack is empty.
*/
public AnyType pop() throws EmptyStackException {
if(list.size() > 0){
return list.remove(0);
}
else{
throw new EmptyStackException();
}
/**
* -------------------------------------------
* TODO: You fully implement this method
*
* Note: Your pop solution must be a constant
* time O(1) operation
*
*/
} // end pop() method
/**
* Looks at the item at the top of this stack without removing it
* from the stack in constant time O(1)
*
* @return the item at the top of this stack
* @throws EmptyStackException - if this stack is empty.
*/
public AnyType peek() throws EmptyStackException {
if(list.size() > 0){
return list.get(0);
}
else{
throw new EmptyStackException();
}
/**
* -------------------------------------------
* TODO: You fully implement this method
*
* Note: Your peek solution must be a constant
* time O(1) operation
*
*/
} // end peek() method
/**
*
* @param args
*/
public static void main( String[] args ) {
ConstantTimeStack<Integer> Stack = new ConstantTimeStack<Integer>();
/////peek at null
try{
System.out.println(Stack.peek().toString());
}catch(EmptyStackException ex){
System.out.println(ex);
}
////push
Stack.push(1337);
////peek
try{
System.out.println(Stack.peek().toString());
}catch(EmptyStackException ex){
System.out.println(ex);
}
///remove
try{
System.out.println("removed : " + Stack.pop());
}catch(EmptyStackException ex){
System.out.println(ex);
}
////pop element form empty stack
try{
Stack.pop();
}catch(EmptyStackException ex){
System.out.println(ex);
}
/**
* -------------------------------------------
* TODO: You put your test cases here
*
*/
} // end main method
} // end ConstantTimeStack class definition