-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
53 lines (48 loc) · 1.67 KB
/
Stack.java
File metadata and controls
53 lines (48 loc) · 1.67 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
public class Stack {
private Node top; //a pointer to the top of the stack
/**
* method to push an element onto the stack
* @param x
*/
public void push(int x) {
Node temp = new Node(x); //create a new node with the data value x
temp.next = top; //points the new node with the data value x to the previous node which was at top of the stack
top = temp; //updates the top reference to point to the new node, making it the new top of the stack
}
/**
* method to pop the top element from the stack
* @return
*/
public int pop() {
if (top == null) {
System.out.println("Stack is empty");
return -1; //returns -1 to indicate an empty stack
}
else {
int data = top.data; //data is holding the value of the top node
top = top.next; //top is updated, and points to the next node
return data; //return the popped value
}
}
/**
* method to get the top element of the stack
* @return
*/
public int top() {
if (top == null) {
System.out.println("There is no top element in the stack");
return -1; //returns -1 to indicate an empty stack
}
else {
return top.data; //return the data value at the top of the stack
}
}
public void display(){
Node current = top; //using a pointer to point to the top of the stack
while(current != null){
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println("null");
}
}