MySortedList.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import org.omg.CORBA.Current;
  2. public class MySortedList
  3. {
  4. private Node head;
  5. private class Node
  6. {
  7. public int element;
  8. public Node next;
  9. public Node(int element)
  10. {
  11. this.element = element;
  12. this.next = null;
  13. }
  14. }
  15. public MySortedList()
  16. {
  17. this.head = null;
  18. }
  19. public void add(int newElm)
  20. {
  21. if (head == null)
  22. {
  23. head = new Node(newElm);
  24. }
  25. else if(head.element > newElm)
  26. {
  27. Node tmp = new Node(newElm);
  28. tmp.next = head;
  29. head = tmp;
  30. }
  31. else
  32. {
  33. Node current = new Node(newElm);
  34. Node tmpHead = head;
  35. while ( tmpHead.next.element < current.element)
  36. {
  37. tmpHead = tmpHead.next;
  38. }
  39. current.next = tmpHead.next;
  40. tmpHead.next = current;
  41. }
  42. }
  43. public void delete(int elm)
  44. {
  45. if (head == null)
  46. {
  47. //DO NOTHING
  48. }
  49. else
  50. {
  51. //Set tmp variable head, iterate w/while until tmp.next == null
  52. //Stop when tmp.elem = elm
  53. //
  54. }
  55. }
  56. public int max()
  57. {
  58. Node tmpHead = head;
  59. while (tmpHead.next != null)
  60. {
  61. tmpHead = tmpHead.next;
  62. }
  63. return tmpHead.element;
  64. }
  65. public int min()
  66. {
  67. return head.element;
  68. }
  69. public void print()
  70. {
  71. Node tmpHead = head;
  72. while (tmpHead.next != null)
  73. {
  74. System.out.print(tmpHead.element + " " );
  75. tmpHead = tmpHead.next;
  76. }
  77. System.out.println();
  78. }
  79. public boolean isEmpty()
  80. {
  81. return (head == null);
  82. }
  83. }