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. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. MyLinkedList list = new MyLinkedList();
  13. list.Add(1);
  14. list.Add(2);
  15. list.Add(3);
  16. list.Print();
  17. }
  18.  
  19. public static class Node {
  20. public int data;
  21. public Node next;
  22. }
  23.  
  24. public static class MyLinkedList {
  25.  
  26. public Node head;
  27. public Node curr;
  28.  
  29. public MyLinkedList() {
  30. // TODO Auto-generated constructor stub
  31. head = null;
  32. curr = null;
  33. }
  34.  
  35. public void Add(int data) {
  36. Node box = new Node();
  37. box.data = data;
  38. box.next = null;
  39. curr = head;
  40. if (curr == null) {
  41. head = box;
  42. curr = null;
  43. }
  44.  
  45. else {
  46. while (curr.next != null) {
  47. curr = curr.next;
  48.  
  49. }
  50. curr.next = box;
  51. }
  52. }
  53.  
  54. public void Print() {
  55. curr = head;
  56. while (curr != null) {
  57. System.out.println(curr.data);
  58. curr = curr.next;
  59. }
  60. }
  61. }
  62.  
  63. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
1
2
3