-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackLLImplementation.java
More file actions
55 lines (47 loc) · 1.04 KB
/
StackLLImplementation.java
File metadata and controls
55 lines (47 loc) · 1.04 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
54
55
package DSImplementations;
public class StackLLImplementation<T> {
static class Node<E> {
public E data;
public Node<E> next;
public Node(E data) {
this.data = data;
this.next = null;
}
}
int size = -1;
Node<T> head;
public StackLLImplementation() {
head = null;
}
public void push(T data) {
Node<T> newNode = new Node<>(data);
newNode.next = head;
head = newNode;
size++;
}
public T pop() {
if (head == null) {
System.out.println("Stack is empty");
return null;
}
T data = head.data;
head = head.next;
size--;
return data;
}
public T peek() {
if (head == null) {
System.out.println("Stack is empty");
return null;
}
return head.data;
}
public void print() {
Node<T> current = head;
while (current != null) {
System.out.print(current.data + " > ");
current = current.next;
}
System.out.print("null\n");
}
}