Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enter code here
- import java.io.*;
- import java.util.*;
- class Node
- {
- int data;
- Node next;
- public Node(int data)
- {
- this.data = data;
- this.next = null;
- }
- }
- public class Solution
- {
- public Node head;
- public void push(int data)//insert at begin
- {
- Node newNode = new Node(data);
- newNode.next = head;
- head = newNode;
- }
- public void pop()//deletion at begin
- {
- if (head == null) {
- System.out.print("stack underFlow");
- return;
- }
- head = head.next;
- }
- public int maxele(Node head)
- {
- Node max = head;
- Node temp = head;
- while(temp!=null)
- {
- if(max.data>temp.data)
- max=temp;
- temp = temp.next;
- }
- return max.data;
- }
- public void display()
- {
- Node temp = head;
- while(temp!=null)
- {
- System.out.print(temp.data+"");
- temp = temp.next;
- }
- }
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- Solution obj = new Solution();
- int maximumElement;
- int N,i=0;
- int test;
- N=sc.nextInt();
- while(i<N)
- {
- test = sc.nextInt();
- if(test==1)
- {
- obj.push(sc.nextInt());
- }
- else if(test==2)
- {
- obj.pop();
- }
- else if(test==3)
- {
- maximumElement=obj.maxele(obj.head);
- System.out.println(maximumElement);
- System.out.println();
- }
- i++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement