-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackList.java
More file actions
43 lines (32 loc) · 800 Bytes
/
StackList.java
File metadata and controls
43 lines (32 loc) · 800 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
package com.company;
import java.util.EmptyStackException;
public class StackList {
private Node head;
private static class Node {
private Node next;
private Object data;
public Node(Object data) {
this.data = data;
}
@Override
public String toString() {
return this.data.toString();
}
}
public void push(Object element){
if(head == null){
head = new Node(element);
}
else {
Node next = head;
head = new Node(element);
head.next=next;
}
}
public Object pop(){
if (head==null) throw new EmptyStackException();
Node holder = head;
head=head.next;
return holder.data;
}
}