-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
48 lines (36 loc) · 873 Bytes
/
Stack.java
File metadata and controls
48 lines (36 loc) · 873 Bytes
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
// Name: Sani Haseeb
/** This class implements the stack
* it follows a data structure that follows
* FIRST IN LAST OUT principle
* @author Sani
*
*/
public class Stack {
public listNode top = null;
/**
*
* @param myStr i.e the argument given to the push method-used to
* push the new string on the top of the stack
*/
public void push(String myStr) {
listNode node = new listNode();
node.data = myStr;
if (top != null) {
node.next = top;
}
top = node;
}
/**
* This method called the 'pop' removes the top value from the stack and returns it
*
* @return RETURNS THE TOP STACK IF STACK IS NOT EMPTY
* IF EMPTY, RETURNS NULL VALUE
*/
public String pop() {
if (top == null)
return null;
String topValue = top.data;
top = top.next;
return topValue;
}
}