-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedStack.java
114 lines (102 loc) · 3.13 KB
/
LinkedStack.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Assignment3;
import Node.Node;
/**
*
* @author kaeuc
*/
public class LinkedStack {
//Keeps the beggining of the stack
private Node topPtr;
private int length;
private final int maxElements;
//Stack constructor
public LinkedStack(int maxElements) {
this.maxElements = maxElements;
topPtr = null;
length = 0;
}
public void makeEmpty() {
// Temporary Node used to clear the current node through its address
Node tempPtr;
//it will execute until the last item of the stack which points to null
while (topPtr != null) {
//save the current address on tempPtr
tempPtr = topPtr;
//points the current top to the next
topPtr = topPtr.getNext();
//delete the previous node
tempPtr = null;
}
length = 0;
}
public final boolean isEmpty() {
//if the top of the stack is pointing to null, so there is no items
//on the stack
return topPtr == null;
}
public final boolean isFull() {
//if the length is equal to the max number of elements allowed, so it's full
return length == maxElements;
}
public void push(Object item) {
//if the stack is already full, just return, not allowing the user
// to insert another item
if (isFull()) {
return;
}
//Creates a new node
Node newNode = new Node();
//insert the data within the node
newNode.setData(item);
//set the next item into position
//newNode is on the top now
newNode.setNext(topPtr);
//and points the top to the newNode
topPtr = newNode;
length++;
}
public Object pop() {
//if the stack is already empty, just return null to indicate that
if (isEmpty()) {
return null;
}
//Creates a temporary node to get the current node on top
Node tempPtr;
//gets the address of the node on the top
tempPtr = topPtr;
//gets the data and insert into an object to return
Object item = tempPtr.getData();
//points the top of the stack to the next item
topPtr = topPtr.getNext();
//deletes the item
tempPtr = null;
length--;
//return the item
return item;
}
public int getLength() {
return length;
}
public void execute() {
LinkedStack stack = new LinkedStack(20);
stack.push(5);
stack.push(6);
stack.push(7);
stack.push(8);
stack.push(9);
while (!stack.isEmpty()) {
System.out.println((int) stack.pop());
}
for (int i = 0; i < 10; i++) {
stack.push(i);
}
System.out.println("Length before make empty: " + stack.length);
stack.makeEmpty();
System.out.println("Length after make empty: " + stack.length);
}
}