forked from srishilesh/Data-Structure-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.2
96 lines (91 loc) · 1.9 KB
/
3.2
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
// Design a stack with push, pop and minimum value functions operating with O(n)
// Approach 1 - Attach the Min value header to each nodes (Not efficient)
// Approach 2 - Use separate stack to keep track of the minimum values
import java.util.Stack;
public class stacklinkedlist {
StackNode root;
Stack<Integer> MinStack;
public stacklinkedlist(){
MinStack = new Stack<Integer>();
}
static class StackNode{
int data;
StackNode next;
StackNode(int data)
{
this.data = data;
}
}
public boolean isEmpty()
{
if(root == null)
return true;
else
return false;
}
public void push(int data)
{
StackNode node = new StackNode(data);
if(root == null)
root = node;
else
{
StackNode temp = root;
root = node;
node.next = temp;
}
if(data<=min())
MinStack.push(data);
// if(minValue>data)
// minValue = data;
System.out.println(data + " pushed to stack");
}
public int min()
{
if(MinStack.isEmpty())
return Integer.MAX_VALUE;
else
return MinStack.peek();
}
public int pop()
{
int popped = Integer.MIN_VALUE;
if(root == null)
System.out.println("Stack is empty");
else
{
popped = root.data;
root = root.next;
}
if(popped==min())
MinStack.pop();
return popped;
}
// public int min()
// {
// return minValue;
// }
public int peek()
{
if (root == null) {
System.out.println("Stack is empty");
return Integer.MIN_VALUE;
}
else {
return root.data;
}
}
public static void main(String args[])
{
stacklinkedlist sll = new stacklinkedlist();
sll.push(10);
System.out.println("Minimum "+sll.min());
sll.push(20);
System.out.println("Minimum "+sll.min());
sll.push(30);
System.out.println("Minimum "+sll.min());
System.out.println("Popping "+sll.pop());
System.out.println("Peeking "+sll.peek());
System.out.println("Minimum "+sll.min());
}
}