-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTStack.java
More file actions
86 lines (73 loc) · 1.78 KB
/
Copy pathTStack.java
File metadata and controls
86 lines (73 loc) · 1.78 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
import java.util.EmptyStackException;
public class TStack<E> implements InterStackE<E> {
private TStackNode<E> top;
public TStack(){
top = null;
}
@Override
public boolean isEmpty() {
if(top==null)
return true;
else
return false;
}
@Override
public E top() {
return top.getData();
}
@Override
public E pop() {
if(top==null)
throw new EmptyStackException();
else{
E ExTop = top.getData();
top = top.getPrev();
return ExTop;
}
}
@Override
public int size() {
if(top == null){
return 0;
}else{
int counter =0;
TStackNode refNode = top;
while(refNode!=null){
refNode=refNode.getPrev();
counter++;
}
return counter;
}
}
@Override
public int deepLevel(E item) {
TStackNode<E> tmp = top;
int level =0;
while(tmp!=null){
if(item==tmp.getData()){
return level;
}
tmp = tmp.getPrev();
level++;
}
return -1;
}
public void deleteBottom(){
TStackNode<E> tmp = top;
if(top == null)
throw new EmptyStackException();
if(tmp.getPrev()==null){
tmp=null;
}else{
while(tmp.getPrev().getPrev()!=null){
tmp.getPrev();
}
tmp.setPrev(null);
}
}
@Override
public void push(E item) {
TStackNode<E> newNodeTop = new TStackNode<>(item,top);
top = newNodeTop;
}
}