-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackUsingLinkedList.java
133 lines (119 loc) · 3.88 KB
/
StackUsingLinkedList.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// the node of linked list used to implement stack
class Node{
int data;
Node next;
Node(int x){
this.data=x;
this.next=null;
}
}
public class StackUsingLinkedList {
Node head;
// Function to push an element into stack
// Time Complexity - O(1) , Space Complexity - O(1)
public void push(int data){
Node newNode = new Node(data);
// if linked list is empty
if(head==null){
head=newNode;
return;
}
// else if linked list is not empty, we will add a new node to the start of linked list
newNode.next=head;
head=newNode;
}
// Function to pop an element from top of stack
// Time Complexity - O(1) , Space Complexity - O(1)
public void pop(){
// if linked list is empty
if(head==null){
System.out.println("Stack is already empty, can't pop");
return;
}
// else if linked list is not empty, we remove the head node and update the new head
System.out.println("Pop Operation Successfull - "+head.data+" popped from stack");
head=head.next;
}
// Function to peek the top of the stack
// Time Complexity - O(1) , Space Complexity - O(1)
public void peek(){
// if linked list is empty
if(head==null){
System.out.println("Stack is already empty, can't peek");
return;
}
// if linked list is not empty
System.out.println("Top of the stack is : "+head.data);
}
// function to delete the complete stack
// Time Complexity - O(1) , Space Complexity - O(1)
public void deleteStack(){
// if linked list is empty
if(head==null){
System.out.println("Stack is empty, can't delete");
return;
}
// else if linked list is not empty
head=null;
}
// function to traverse the stack from top to bottom as left to right in linked list
// Time Complexity - O(n) , Space Complexity - O(1)
public void traverseStack(){
// if linked list is empty
if(head==null){
System.out.println("Stack is empty, can't traverse");
return;
}
// else if linked list is not empty
Node temp=head;
System.out.println("Traversing Stack from top as left and bottom as right...");
while(temp!=null){
System.out.print(temp.data+" ");
temp=temp.next;
}
System.out.println();
}
public static void main(String[] args) throws Exception {
StackUsingLinkedList stack = new StackUsingLinkedList();
stack.pop();
stack.peek();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.traverseStack();
stack.pop();
stack.traverseStack();
stack.peek();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.traverseStack();
stack.deleteStack();
stack.push(1000);
stack.traverseStack();
stack.deleteStack();
stack.traverseStack();
}
}
/* ================ OUTPUT =========================
Stack is already empty, can't pop
Stack is already empty, can't peek
Traversing Stack from top as left and bottom as right...
5 4 3 2 1
Pop Operation Successfull - 5 popped from stack
Traversing Stack from top as left and bottom as right...
4 3 2 1
Top of the stack is : 4
Pop Operation Successfull - 4 popped from stack
Pop Operation Successfull - 3 popped from stack
Pop Operation Successfull - 2 popped from stack
Pop Operation Successfull - 1 popped from stack
Stack is empty, can't traverse
Stack is empty, can't delete
Traversing Stack from top as left and bottom as right...
1000
Stack is empty, can't traverse
=====================================================*/