unsorted list...

Mar 11, 2010 at 4:46pm
Hey everybody! I know what i want to do, im just unsure of how to do it. I want to make an array and each index in the array pointing to an unsorted list. Im not sure what do declare the array because i need the elements in the array to be a pointer pointing to a unsorted list. I have 10 id numbers from 1 - 10. So i think a simple array would work good, and then each id number gets input, and i want to input the numbers into a unsorted list. I hope its clear, thanks for any help.


my unsorted list class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef unsortedType_h
#define unsortedType_h
 

const int MAX_ITEMS = 1000;

class unsortedType
{
	private:
	int length;
	int cursor;
	int info[MAX_ITEMS];

	public:
	unsortedType();
	int getLength();
	bool isEmpty();
	bool isFull();
	void gotoBegining();
	void getNext(int& item);
	void reset();
	void insert(int item);
	void remove(int item);
};

#endif 



Mar 11, 2010 at 5:42pm
So are you saying that each element in 'info' points to an array of 10 id numbers?

If that's the case, your arrays appears to be fixed at 1000 arrays of 10 elements each. Just declare the array as follows:
 
int info[MAX_ITEMS][10];


Or, even better, declare a constant for the number 10, in case you decide to change it:
 
const int MAX_LIST[10];


Then just assign the numbers to their respective locations:
1
2
3
4
5
6
7
8
9
for (int i=0; i<MAX_ITEMS; i++) {
    for (int j=0; j<MAX_LIST; j++) {
        int id;
        //Get the id number, however you are doing that

        //Assign id
        info[i][j] = id;
    }
}


Now, if what you mean is that you'd like to assign existing id arrays to 'info' elements, then declare 'info' as:
 
int *info[MAX_ITEMS];  //Simply declare array of pointers 


Then when assigning to existing arrays, just set:
1
2
3
4
//Let pIntArray be a pointer to some array of integers
for (int i=0; i<MAX_ITEMS; i++) {
    info[i] = pIntArray;
}


Let me know if neither of those is what you intended.
Mar 11, 2010 at 5:54pm
no i want to declare an array, in my driver or whatever, of 10 elements, and each element in that array points to a different unsortedType ie. info[]. thanks.
Mar 11, 2010 at 6:25pm
Does something like this work?

1
2
3
4
5
6
(unsortedType *) array[10];

for (int i = 0; i < 10; i++)
        array[i] = new unsortedType(); // if you do not initialize, it could end badly

...


alternativly just do something like

1
2
3
4
5
unsortedType **array = new (*unsortedType) [10];

...

delete [] array;


I'm never sure about the priorities of [] and *. Please respond if something worked.
Last edited on Mar 11, 2010 at 6:25pm
Mar 11, 2010 at 6:39pm
yea, i was thining about something like that, thanks ill give it a try.
Mar 11, 2010 at 7:16pm
yea i tried it the first way earlier but i forgot to initialize it (duh!?!?). The first way works when i get rid of the brackets around the declaration. The second way i didnt really try im not sure about the syntax for that, but i got a couple errors. thanks a lot man.
Topic archived. No new replies allowed.