Help with array-based implemenation adt list using java

Reply

Join Date: Sep 2008
Posts: 9
Reputation: yingfo is on a distinguished road 
Solved Threads: 0
yingfo yingfo is offline Offline
Newbie Poster

Help with array-based implemenation adt list using java

 
0
  #1
Mar 31st, 2009
Hi I am having trouble trying to get my add and remove function to work.
We had to create an ADT List using array's.
It keeps giving my program errors
any help would be great, I think my problem might be my second part of the add function
where it will add in between 2 arrays

  1. // ****************************************************
  2. // Reference-based implementation of ADT list using arrays.
  3. // ****************************************************
  4. public class List {
  5. // reference to linked list of items
  6.  
  7. public static final int MAX_LIST = 20;
  8. public static final int NULL = -1;
  9.  
  10. private ListItem item[] = new ListItem[MAX_LIST]; // data
  11. private int next[] = new int[MAX_LIST]; // pointer to next item
  12.  
  13. private int head; // pointer to front of list
  14. private int free; // pointer to front of free list
  15. private int numItems; // number of items in list
  16.  
  17. // Constructor must initialize used list to empty and free list to
  18. // all available nodes.
  19.  
  20. public List()
  21. {
  22. int index;
  23. for (index = 0; index < MAX_LIST-1; index++)
  24. next[index] = index + 1;
  25.  
  26. next[MAX_LIST-1] = NULL;
  27.  
  28. numItems = 0;
  29. head = NULL;
  30. free = 0;
  31. } // end default constructor
  32.  
  33. public void removeAll() { // reinitialize all nodes to free
  34. int index;
  35. for (index = 0; index < MAX_LIST-1; index++)
  36. next[index] = index + 1;
  37.  
  38. next[MAX_LIST-1] = NULL;
  39.  
  40. numItems = 0;
  41. head = NULL;
  42. free = 0;
  43. } // end removeAll
  44.  
  45. public boolean isEmpty() {
  46. return numItems == 0;
  47. } // end isEmpty
  48.  
  49. public int size() {
  50. return numItems;
  51. } // end size
  52.  
  53. private int find(int index) {
  54. // --------------------------------------------------
  55. // Locates a specified node in a linked list.
  56. // Precondition: index is the number of the desired
  57. // node. Assumes that 1 <= index <= numItems
  58. // Postcondition: Returns a reference to the desired
  59. // node.
  60. // --------------------------------------------------
  61. int curr = head;
  62. for (int skip = 1; skip < index; skip++)
  63. {
  64. curr = next[curr];
  65. }
  66. return curr;
  67. }
  68.  
  69. public ListItem get(int index)
  70. {
  71. if (index >= 1 && index <= numItems)
  72. {
  73. int curr = find(index);
  74. ListItem dataItem =item[curr];
  75. return dataItem;
  76. }
  77. else
  78. {
  79. System.out.println("List index out of bounds on get");
  80. return null;
  81. }
  82. } // end get
  83.  
  84.  
  85. public void add(int index, ListItem newItem)
  86. {
  87. if (index >= 1 && index <= numItems+1)
  88. {
  89. if (index == 1)
  90. {
  91. item[free]=newItem;
  92. int free2 = next[free];
  93.  
  94. next[free]=head;
  95. head=free;
  96. free = free2;
  97. }
  98. else
  99. {
  100. int curr=free;
  101. int free2=next[free];
  102. int prev = find(index-1);
  103. int prev2=next[prev];
  104.  
  105. next[prev]=curr;
  106. next[curr]=prev2;
  107. free=free2;
  108. }
  109. numItems++;
  110. }
  111. else
  112. {
  113. System.out.println("List index out of bounds on add");
  114. }
  115. }
  116.  
  117.  
  118. public void remove(int index)
  119. {
  120. if (index >= 1 && index <= numItems)
  121. {
  122. if (index == 1)
  123. {
  124. int nextHead=next[head];
  125. next[head]=free;
  126. free=head;
  127. head=nextHead;
  128. }
  129. else
  130. {
  131. int prev = find(index-1);
  132. int curr=find(index);
  133. int getNext=find(index+1);
  134. next[prev]=getNext;
  135. next[curr]=free;
  136. free=curr;
  137. }
  138. numItems--;
  139. } // end if
  140. else
  141. {
  142. System.out.println("List index out of bounds on remove");
  143. }
  144. }
  145. }// end remove
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,568
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Help with array-based implemenation adt list using java

 
0
  #2
Mar 31st, 2009
For your add (should be called insert?) method:

1. You have to first figure out what index they want to add the item at. If the index they want to add the item at is the empty, then just insert it and you're done.
2. If the index is not empty, and your array is large enough to add an item, then you have to move every element after that index down. You have to start at the last filled index and move it down first. Then move the one next to it into it's place, and do this until you can add the item you want to add. You have to start at the very end so that you don't overwrite any items.
3. If you do not have an array large enough, you have to create a new array with enough space, then copy the elements over in the correct spots.

I hope that explanation helps. If you need an example, I can try to give you one, but it's hard to draw it.
Last edited by BestJewSinceJC; Mar 31st, 2009 at 6:42 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC