| | |
Help with array-based implemenation adt list using java
![]() |
•
•
Join Date: Sep 2008
Posts: 9
Reputation:
Solved Threads: 0
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
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
Java Syntax (Toggle Plain Text)
// **************************************************** // Reference-based implementation of ADT list using arrays. // **************************************************** public class List { // reference to linked list of items public static final int MAX_LIST = 20; public static final int NULL = -1; private ListItem item[] = new ListItem[MAX_LIST]; // data private int next[] = new int[MAX_LIST]; // pointer to next item private int head; // pointer to front of list private int free; // pointer to front of free list private int numItems; // number of items in list // Constructor must initialize used list to empty and free list to // all available nodes. public List() { int index; for (index = 0; index < MAX_LIST-1; index++) next[index] = index + 1; next[MAX_LIST-1] = NULL; numItems = 0; head = NULL; free = 0; } // end default constructor public void removeAll() { // reinitialize all nodes to free int index; for (index = 0; index < MAX_LIST-1; index++) next[index] = index + 1; next[MAX_LIST-1] = NULL; numItems = 0; head = NULL; free = 0; } // end removeAll public boolean isEmpty() { return numItems == 0; } // end isEmpty public int size() { return numItems; } // end size private int find(int index) { // -------------------------------------------------- // Locates a specified node in a linked list. // Precondition: index is the number of the desired // node. Assumes that 1 <= index <= numItems // Postcondition: Returns a reference to the desired // node. // -------------------------------------------------- int curr = head; for (int skip = 1; skip < index; skip++) { curr = next[curr]; } return curr; } public ListItem get(int index) { if (index >= 1 && index <= numItems) { int curr = find(index); ListItem dataItem =item[curr]; return dataItem; } else { System.out.println("List index out of bounds on get"); return null; } } // end get public void add(int index, ListItem newItem) { if (index >= 1 && index <= numItems+1) { if (index == 1) { item[free]=newItem; int free2 = next[free]; next[free]=head; head=free; free = free2; } else { int curr=free; int free2=next[free]; int prev = find(index-1); int prev2=next[prev]; next[prev]=curr; next[curr]=prev2; free=free2; } numItems++; } else { System.out.println("List index out of bounds on add"); } } public void remove(int index) { if (index >= 1 && index <= numItems) { if (index == 1) { int nextHead=next[head]; next[head]=free; free=head; head=nextHead; } else { int prev = find(index-1); int curr=find(index); int getNext=find(index+1); next[prev]=getNext; next[curr]=free; free=curr; } numItems--; } // end if else { System.out.println("List index out of bounds on remove"); } } }// end remove
•
•
Join Date: Sep 2008
Posts: 1,568
Reputation:
Solved Threads: 196
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.
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.
![]() |
Other Threads in the Java Forum
- Previous Thread: open and read all files from folder
- Next Thread: Find Grade using class and methods
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






