fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone {
  9.  
  10. private static class Node {
  11. int data;
  12. Node next;
  13. }
  14.  
  15. Node head;
  16. public void push(int data)
  17. {
  18. Node node =new Node();
  19. node.data=data;
  20. node.next=null;
  21. if(head==null)
  22. {
  23. head=node;
  24.  
  25. }
  26. else
  27. {
  28. Node n=head;
  29. while (n.next!=null)
  30. {
  31. n=n.next;
  32. }
  33. n.next=node;
  34. }
  35.  
  36. }
  37. public void pop()
  38. {
  39. if(head==null)
  40. {
  41. System.out.println("Stack has 0 items .. cant delete");
  42. }
  43. else if(head.next == null) {
  44. head = null;
  45. }
  46. else {
  47. Node n = head;
  48. while (n.next != null && n.next.next != null) {
  49. n = n.next;
  50. }
  51. n.next=null;
  52. }
  53. }
  54. public void show()
  55. {
  56. Node n=head;
  57. while(n != null)
  58. {
  59. System.out.println(n.data);
  60. n=n.next;
  61. }
  62. }
  63.  
  64. public static void main(String[] args) {
  65. Ideone stk=new Ideone();
  66. stk.push(4);
  67. stk.push(54);
  68.  
  69. stk.push(23);
  70. stk.push(90);
  71.  
  72. stk.pop();
  73.  
  74. stk.show();
  75. stk.pop(); stk.pop(); stk.pop();
  76. stk.show();
  77.  
  78. }
  79.  
  80. }
Success #stdin #stdout 0.05s 27964KB
stdin
Standard input is empty
stdout
4
54
23