-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
54 lines (46 loc) · 855 Bytes
/
Stack.java
File metadata and controls
54 lines (46 loc) · 855 Bytes
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
package asg_2;
public class Stack {
private sNode top;
public Stack( ){
top = null;
}
//Adds a Node to the Stack
public void push(Node n){
top = new sNode(n, top);
}
//Removes the top Node from the Stack
public Node pop( ){
Node i;
if(top == null){
throw new NoItemException();
}
else {
i = top.item;
top=top.next;
return i;
}
}
//Gives the Data from the top node but does not remove it
public Node top( ){
if(top == null){
throw new NoItemException();
}
else{
return top.item;
}
}
//Returns the depth or length of the stack
public int depth( ){
sNode p = top;
int i =0;
while(p!=null){
p=p.next;
i++;
}
return i;
}
//Checks if there are any nodes in the stack
public boolean empty(){
return top == null;
}
}