Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. enter code here
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. class Node
  6. {
  7. int data;
  8. Node next;
  9. public Node(int data)
  10. {
  11. this.data = data;
  12. this.next = null;
  13. }
  14. }
  15. public class Solution
  16. {
  17. public Node head;
  18. public void push(int data)//insert at begin
  19. {
  20. Node newNode = new Node(data);
  21. newNode.next = head;
  22. head = newNode;
  23. }
  24. public void pop()//deletion at begin
  25. {
  26. if (head == null) {
  27. System.out.print("stack underFlow");
  28. return;
  29. }
  30. head = head.next;
  31. }
  32. public int maxele(Node head)
  33. {
  34. Node max = head;
  35. Node temp = head;
  36. while(temp!=null)
  37. {
  38. if(max.data>temp.data)
  39. max=temp;
  40. temp = temp.next;
  41. }
  42. return max.data;
  43. }
  44. public void display()
  45. {
  46. Node temp = head;
  47. while(temp!=null)
  48. {
  49. System.out.print(temp.data+"");
  50. temp = temp.next;
  51. }
  52. }
  53. public static void main(String[] args)
  54. {
  55. Scanner sc = new Scanner(System.in);
  56. Solution obj = new Solution();
  57. int maximumElement;
  58. int N,i=0;
  59. int test;
  60. N=sc.nextInt();
  61. while(i<N)
  62. {
  63. test = sc.nextInt();
  64. if(test==1)
  65. {
  66. obj.push(sc.nextInt());
  67. }
  68. else if(test==2)
  69. {
  70. obj.pop();
  71. }
  72. else if(test==3)
  73. {
  74. maximumElement=obj.maxele(obj.head);
  75. System.out.println(maximumElement);
  76. System.out.println();
  77. }
  78. i++;
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement