-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.java
123 lines (109 loc) · 4.86 KB
/
Stack.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
/* Yasin İnal */
////////////////////////////////////////////////////////////////////////////
////////////////////////////// STACK ////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
public static class Stack<E> {
private Node<E> trailer; //top point
private int size = 0;
public Stack() {
trailer = new Node<>(null, null, null);
}
/* DEFINE THE STRUCTURE OF A SINGLE NODE */
private static class Node<E>
{
private E element; //hold the element
private Node<E> prev; //hold the reference to the previous node
private Node<E> next; //hold the reference to the next node
/* NODE CONSTRUCTOR */
public Node(E element, Node<E> prev, Node<E> next) {
this.element = element;
this.prev = prev;
this.next = next;
}
/* SETTER GETTER FUNCTIONS */
public E getElement() {return element;}
public void setElement(E element) {this.element = element; }
public Node<E> getPrev() {return prev; }
public void setPrev(Node<E> prev) {this.prev = prev; }
public Node<E> getNext() {return next;}
public void setNext(Node<E> next) {this.next = next;}
}
/* Return the size of the stack */
public int size() {
return size;
}
void push(E e){
if(size == 0) {
// Special function for adding the first element.
addFirst(e);
return;
}
///////////// STANDARD PUSH PROCESS ///////////////
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Take a copy of the trailer(top element) to the 'lastNode',
* and make the 'newNode' as the top element of the stack (trailer)...
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
Node<E> lastNode = trailer;
Node<E> newNode = new Node<>(e, lastNode, null);
lastNode.setNext(newNode); // Set the next element of the old top as newNode...
trailer = newNode;
size++; // Increase the size...
}
E pop(){
if(size == 0) return null;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/* Get the element of the trailer (top) unless the stack is empty */
E value = trailer.getElement(); //
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
if(size == 1){
trailer.element = null;
size--;
return value;
}
///////////// STANDARD POP PROCESS ///////////////
/* * * * * * * * * * * * * * * * * * * * * * * * * *
* If the size of the stack is bigger than 1,
* Reach the previous node of the top
* and make the trailer show that.
* So, the new 'top' of the stack is the previous node...
* * * * * * * * * * * * * * * * * * * * * * * * * */
Node<E> previousNode = trailer.getPrev();
previousNode.setNext(null); // Clean the old top of the stack...
trailer = previousNode;
size--; // Reduce the size...
return value;
}
/* * * * * * * * * * * * * * * * * * *
* PRINT ALL THE STACK *
* * * * * * * * * * * * * * * * * * * */
public String printStack(){
StringBuilder sb = new StringBuilder();
sb.append("( ");
/* Cursor is at the trailer,(top of the stack) initially... */
Node<E> cursor = trailer;
for(int i=0; i<size; i++)
{
/* * * * * * * * * * * * * * * * * * * /
/* APPEND THE CURRENT ELEMENT */
sb.append(cursor.element); //
/* * * * * * * * * * * * * * * * * * **/
cursor = cursor.getPrev(); // Move the cursor to the previous element of the stack...
/* Don't add comma to the last block... */
if(i<size-1) {
sb.append(", ");
}
/* Go next line after 20 output... */
if(i%20==19) {
sb.append(" ==> next line\n");
}
}
/* Close the parenthesis... */
sb.append(" )");
return sb.toString();
}
// Special function for adding the first element.
private void addFirst(E e) {
trailer = new Node<>(e, null, null);
size++;
}
}