-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingLL.java
More file actions
70 lines (57 loc) · 1.39 KB
/
StackUsingLL.java
File metadata and controls
70 lines (57 loc) · 1.39 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package learn.ds.stack;
/**
* @author Varma Penmetsa
*
* Pros: The linked list implementation of stack can grow and shrink according to the needs at runtime.
* Cons: Requires extra memory due to involvement of pointers.
*
* https://www.geeksforgeeks.org/stack-data-structure-introduction-program/
*/
public class StackUsingLL {
public class Node {
public String val;
public Node next;
public Node(String val) {
this.val = val;
}
}
Node head;
public void push(String val) {
Node new_node = new Node(val);
new_node.next = head;
head = new_node;
}
public String pop() {
if (head != null) {
Node temp = head;
head = head.next;
return temp.val;
}else{
return "";
}
}
public String peek() {
if (head != null) {
return head.val;
} else {
return "";
}
}
public boolean isEmpty() {
if (head != null) {
return false;
} else {
return true;
}
}
public static void main(String args[]) {
StackUsingLL ll = new StackUsingLL();
ll.push("S");
ll.push("T");
ll.push("A");
ll.push("C");
ll.push("K");
System.out.println(ll.peek());
System.out.println(ll.isEmpty());
}
}