-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackLinkedList.java
88 lines (70 loc) · 1.61 KB
/
StackLinkedList.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
import java.util.ArrayList;
public class StackLinkedList implements IStackAndQueue {
protected Node _topNode;
private class Node{
int value;
Node next;
Node(int value){
this.value = value;
}
}
StackLinkedList(){
_topNode = null;
}
@Override
public boolean Push(int value) {
if(!isFull())
{
Node tempNode= new Node(value);
tempNode.next = _topNode;
_topNode = tempNode;
return true;
}
return false;
}
@Override
public int Pop() {
if(!isEmpty()){
int value = _topNode.value;
_topNode = _topNode.next;
return value;
}
return -1;
}
@Override
public int Peek() {
if(!isEmpty()){
return _topNode.value;
}
return -1;
}
@Override
public int Count() {
return 0;
}
@Override
public boolean isFull() {
//K rõ
return false;
}
@Override
public boolean isEmpty() {
return _topNode == null;
}
@Override
public String ToString() {
String str = "";
if(isEmpty())
return str;
ArrayList<Integer> tempArray = new ArrayList<>();
Node tempNode = _topNode;
while(tempNode != null){
tempArray.add(tempNode.value);
tempNode = tempNode.next;
}
for (int i = tempArray.size() - 1; i >= 0; i--) {
str += String.valueOf(tempArray.get(i)) + " ";
}
return str;
}
}