> C++ help! lists/array/abstract data types?

C++ help! lists/array/abstract data types?

Posted at: 2014-12-18 
Even though you are restricted to a one dimensional array you can still approach the problem as you would using a multi-dimensional array. In reality, the multi-dim array is just a long one dim array with another level of indexing.

Say you needed a list of 9 indexes with 5 possible chars. Thats normally a 9x5 array. 9 rows and 5 columns. Its also 45 memory locations all in sequence, but divided into 9 lists of chars.

To index the 3rd char in the 4th list you multiply 5 x 3 and add 3, then subtract 1 because of zero indexing. So the index is 18 -1 or 17.

Or you can create an array of arrays - or basically pointers to lists of 5 chars.

Thats an array of 9 lists of 5 chars each.

You can also use a struct instead of an array for the lists.

These are often called vectors. Its more useful if you need to refer to the elements of the list by name or id rather than an index.

In either case, you will be creating a list referenced by a pointer, then add the pointer to the 9 element array.

something like

char list_0[] = {'a', 'b','c','d','e'};

char* array[9];

array[0] = list_0;

To reference letter c

*(array[0]+2);

You are mostly confused, I think, and it's coming across. It's not clear what you actually need to achieve. Multiple characters in one position, you say, but the only example you give uses the same character ... just 5 of them. So I've no idea if you mean only the same character with a different count or if it may also be different characters where any and/or all of them may have separate, different counts. Who knows? You don't say. I also have no concept in mind for "position." It's not an array index. If it were, you'd say so. So it's something else. But I don't know what. A column on a page? Position within the linked list? What?

More examples of what you are being asked to do -- the assignment itself, for example -- would help.

Linked lists can be formed out of an array WITHOUT memory pointers. You can use indices, instead, which are definitely NOT memory pointers.

What you are describing is a two-dimensional array of chars which is basically the same as a string of chars except that a one-dimensional array of chars can be accessed as a series of elements or as individual elements.

Whereas a two-dimensional array can have a series of chars that can have the same element values such as row [0] = h, h, h, h, h and row [1] = z, z, z, z, z.

Therefore, you should use a two dimensional array for your example. However, if they expect you to do this in a one-dimensional array then they would have said to you use pointer arithmetic which is not the case here!

make an array of structures... use the structure for a linked list or a array... inside the struct is an char array you can use as a char buffer.

#typedef struct _NODE

{

char szVal[50];

_NODE *_pPrev;

_NODE *_pNext;

} NODE, *PNODE;

main()

{

// make an array of structs..

NODE myArray[10];

strcpy ( myArray[0].szVal, "h.h.h.h.h" );

myArray[0].pPrev = NULL;

myArray[0].pNext = NULL;

}

i have to do a project. and i'm not asking for any code here.

i'm stuck in the very beginning. literally. i cannot do the project until i figure this out and I cannot for the life of me.

so, i'm using a multi linked list. i HAVE to have a single one dimensional array.. only one. and I have to figure out what data type to put in the array.

This is what I have to do... basically theres like a million and one functions i have to create that all go around this:

i need to be able to have multiple chars in one position.

i've wasted a whole week trying to figure this one out by myself.

i thought i wasn't allowed to use pointers for this project, but i thought thats what linked lists use. sooo, but i'm probably wrong since this class just started. (we were given the project first, and then a week later learned about pointers for our second project)

back to the question: what in the world am i supposed to make an array of? in order to insert multiple chars into a list into one position.

ex. index 1 has h,h,h,h,h (5 h)

oh, and i'm not allowed to use ANY c or c++ strings.

pretty please any sort of help would be amazing.