-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStackOfStrings.java
64 lines (53 loc) · 1.28 KB
/
StackOfStrings.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
class StackOfStrings {
private Node head;
private int size;
private class Node {
String item;
Node next;
public Node(String s) {
item = s;
next = null;
}
}
public StackOfStrings() {
head = null;
size = 0;
}
public boolean isEmpty() {
return head == null;
}
public void push(String s) {
Node temp = new Node(s);
if (!isEmpty()) {
temp.next = head;
}
head = temp;
size++;
}
public String pop() {
if (isEmpty()) {
return null;
}
Node temp = head;
head = head.next;
temp.next = null;
size--;
return temp.item;
}
public int size() {
return this.size;
}
public static void main(String[] args) {
String[] strings = {"a", "b", "c", "d"};
StackOfStrings stack = new StackOfStrings();
System.out.println("Size : " + stack.size());
for (String s : strings) {
stack.push(s);
System.out.println("Size : " + stack.size());
}
while (!stack.isEmpty()) {
System.out.println("Pop : " + stack.pop());
}
System.out.println("Size : " + stack.size());
}
}