Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1.  
  2. public class LLIndexList {
  3.  
  4. private Customer info; // information stored in each node
  5. private LLNode<Customer> linList; // Link to the following node
  6. private int totalElements = 0;
  7.  
  8. public LLNode<Customer> getPointerTo(int index) {
  9. LLNode<Customer> temp = linList;
  10.  
  11. for (int i = 0; i < index; i++) {
  12. temp = temp.getLink();
  13. }
  14.  
  15. return temp;
  16. }
  17.  
  18. public LLIndexList() {
  19. linList = null;
  20. totalElements = 0;
  21. }
  22.  
  23. public int size() {
  24. return totalElements;
  25. }
  26.  
  27. public void printList() {
  28. LLNode<Customer> temp = linList;
  29. int count = 0;
  30. while (temp != null) {
  31. System.out.println(count + ": " + temp.getInfo());
  32. temp = temp.getLink();
  33.  
  34. count++;
  35. }
  36.  
  37. }
  38.  
  39. Customer getItem(int index) {
  40.  
  41. if (index == 0)
  42. return linList.getInfo();
  43. else
  44. return getPointerTo(index).getInfo();
  45.  
  46. }
  47.  
  48. public void insert(int index, Customer item) {
  49. LLNode<Customer> newNode = new LLNode<Customer>(item);
  50. LLNode<Customer> prevPtr = linList;
  51. // int count = 1;
  52.  
  53. if (prevPtr == null || index == 0) {
  54. newNode.setLink(linList);
  55. linList = newNode;
  56. totalElements++;
  57. } else {
  58.  
  59. prevPtr = getPointerTo(index - 1);
  60.  
  61. newNode.setLink(prevPtr.getLink());
  62. prevPtr.setLink(newNode);
  63. totalElements++;
  64. }
  65.  
  66. }
  67.  
  68. public void deleteItem(int index) {
  69. LLNode<Customer> prevPtr = linList;
  70.  
  71. if (linList == null)
  72. return;
  73.  
  74. prevPtr = getPointerTo(index - 1);
  75.  
  76. if (index == 0) {
  77. linList = linList.getLink();
  78. totalElements--;
  79. } else {
  80. LLNode<Customer> deleteMe = prevPtr.getLink();
  81. prevPtr.setLink(deleteMe.getLink());
  82. totalElements--;
  83. }
  84.  
  85. }
  86.  
  87. public int findInSorted(Customer item) {
  88. LLNode<Customer> temp = linList;
  89.  
  90. for (int index = 0; index < totalElements; index++) {
  91. temp = getPointerTo(index);
  92. if (temp.getInfo().getKey() == item.getKey()) {
  93. return index;
  94. }
  95. }
  96. return -1;
  97. }
  98.  
  99. public int locationToInsert(Customer item) {
  100. LLNode<Customer> temp = linList;
  101. int count = 0;
  102.  
  103. while (temp != null && temp.getInfo().getKey() < item.getKey()) {
  104. temp = temp.getLink();
  105. count++;
  106. }
  107.  
  108. return count;
  109.  
  110. }
  111.  
  112. public void insertIntoSorted(Customer cust) {
  113.  
  114. int index = locationToInsert(cust);
  115. insert(index, cust);
  116.  
  117. }
  118.  
  119. public void deleteFromSorted(Customer cust) {
  120. int index = findInSorted(cust);
  121. deleteItem(index);
  122.  
  123. }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement