Advertisement
Guest User

Untitled

a guest
May 28th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. package Algorithms.lists.firstLastLinkedList;
  2.  
  3. /**
  4. * Created by Ruslan Zhdan on 28.05.2016.
  5. */
  6.  
  7. class Link
  8. {
  9. public int iData;
  10. public Link next;
  11.  
  12. public Link(int iData)
  13. {
  14. this.iData = iData;
  15. }
  16.  
  17. public void displayLink()
  18. {
  19. System.out.print("{" + iData + "}");
  20. }
  21. }
  22.  
  23. class FirstLastList
  24. {
  25. private Link first;
  26. private Link last;
  27.  
  28. public FirstLastList()
  29. {
  30. this.first = null;
  31. this.last = null;
  32. }
  33.  
  34. public boolean isEmpty()
  35. {
  36. return (first == null);
  37. }
  38.  
  39. public void insertFirst(int dd)
  40. {
  41. Link newLink = new Link(dd);
  42. if (isEmpty())
  43. last = newLink;
  44. newLink.next = first;
  45. first = newLink;
  46. }
  47.  
  48. public void insertLast(int dd)
  49. {
  50. Link newLink = new Link(dd);
  51. if (isEmpty())
  52. first = newLink;
  53. else
  54. {
  55. last.next = newLink;
  56. last = newLink;
  57. }
  58. }
  59.  
  60. public int deleteFirst()
  61. {
  62. int tmp = first.iData;
  63. if (first.next == null)
  64. last = null;
  65. first = first.next;
  66. return tmp;
  67. }
  68.  
  69. public void displayList()
  70. {
  71. System.out.println("List (first-->last): ");
  72. Link current = first;
  73. while (current != null)
  74. {
  75. current.displayLink();
  76. current = current.next;
  77. }
  78. System.out.println("");
  79. }
  80. }
  81.  
  82. class FirstLastApp
  83. {
  84. public static void main(String[] args)
  85. {
  86. FirstLastList theList = new FirstLastList();
  87. theList.insertFirst(22);
  88. theList.insertFirst(44);
  89. theList.insertFirst(66);
  90.  
  91. theList.insertLast(11);
  92. theList.insertLast(33);
  93. theList.insertLast(55);
  94.  
  95. theList.displayList();
  96.  
  97. theList.deleteFirst();
  98. theList.deleteFirst();
  99.  
  100. theList.displayList();
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement