-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackMinElement.java
More file actions
68 lines (65 loc) · 1.54 KB
/
StackMinElement.java
File metadata and controls
68 lines (65 loc) · 1.54 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
package Codes;
import java.util.Scanner;
public class StackMinElement {
Node top;
static class Node{
int data;
Node next;
Node(int temp){
data=temp;
}
}
void push(int key){
Node newnode=new Node(key);
if (newnode==null)
System.out.println("Stack overflow");
else {
newnode.next=top;
top=newnode;
}
}
void display(){
Node temp=top;
while (temp!=null){
System.out.print(temp.data+" -> ");
temp=temp.next;
}
}
void peek(){
System.out.println(top.data);
}
boolean isEmpty(){
return top==null;
}
void pop(){
if (top==null)
System.out.println("stack underflow");
else {
Node temp=top;
top=temp.next;
System.out.println("element popped");
}
}
int min(){
Node temp=top;
int min=top.data;
while (temp.next!=null){
if (temp.data<min){
min=temp.data;
}
temp=temp.next;
}
return min;
}
public static void main(String[] args) {
StackMinElement stackMinElement=new StackMinElement();
Scanner sc=new Scanner(System.in);
int element=sc.nextInt();
while (element!=-1){
stackMinElement.push(element);
element=sc.nextInt();
}
stackMinElement.display();
System.out.println("\n"+stackMinElement.min());
}
}