A way to manage a list of strings?
Author Message
A way to manage a list of strings?

I'm trying to think of a fairly efficient (but not radically efficient)
algorithm to do the following:

An AddString function, that takes a storage array as one parameter, and the
string to be added as the other.

What the function does is adds the string to the end of the storage array.
If the storage array is filled, it is to take the array and remove the
first string and shift all the other strings up one, and add the new string
to the end.

I'm thinking of just using a temporary array in my function that I'll use
to make the new array, and then just copy the temp array back to the
original array.

I was also thinking about using a linked list, since I recently wrote a
small program that taught me how to use them a bit.

Any ideas would be appreciated.

But don't get too fancy.  This algorithm doesn't have to be insanely
clever.

Thu, 04 Nov 1999 03:00:00 GMT
A way to manage a list of strings?

Quote:

> I'm trying to think of a fairly efficient (but not radically efficient)
> algorithm to do the following:

> An AddString function, that takes a storage array as one parameter, and the
> string to be added as the other.

> What the function does is adds the string to the end of the storage array.
> If the storage array is filled, it is to take the array and remove the
> first string and shift all the other strings up one, and add the new string
> to the end.

> I'm thinking of just using a temporary array in my function that I'll use
> to make the new array, and then just copy the temp array back to the
> original array.

Thats an overkill. What you need to know is, how many element the array
contains and how much it can maximally contain. This enables you to do
the following:
if ( numStrings < maxStrings )
{
strcpy( theArray[numStrings], newString );
numStrings += 1;
}
else
{
/* Shift the array contents one place down, this overwrites the **
** first string in the array.                                   */
for ( i=1 ; i < maxStrings ; i++ )
strcpy( theArray[i-1], theArray[i] );

strcpy( theArray[maxStrings], newString );
}

Quote:
> I was also thinking about using a linked list, since I recently wrote a
> small program that taught me how to use them a bit.

That's not really necessary, if you want arrays. It's a completely
different algorithms, because you will have to do some pointer

Quote:
> But don't get too fancy.  This algorithm doesn't have to be insanely
> clever.

It might be insanely clever, but it's certainly not too fancy :-)

Stephan
(self appointed member of the campaign against grumpiness in c.l.c)

Sat, 06 Nov 1999 03:00:00 GMT
A way to manage a list of strings?

Quote:
>I'm trying to think of a fairly efficient (but not radically efficient)
>algorithm to do the following:

>An AddString function, that takes a storage array as one parameter, and the
>string to be added as the other.

>What the function does is adds the string to the end of the storage array.
>If the storage array is filled, it is to take the array and remove the
>first string and shift all the other strings up one, and add the new string
>to the end.

Are the strings the same length? If not you may have to remove more than
one existing string to make room for the new one.

Quote:
>I'm thinking of just using a temporary array in my function that I'll use
>to make the new array, and then just copy the temp array back to the
>original array.

You don't need a temporary array. Determine how much free space you have.
If you don't have enough scan strings from the start of the array totalling
the amount of space you would have if you deleted them. When you have enough
copy back the reamining block of strings to the start of the array using
memmove(). You then have enough space at the end of the array in which to
write the new string.

Quote:
>I was also thinking about using a linked list, since I recently wrote a
>small program that taught me how to use them a bit.

That makes a lot of sense since you don't have to do a lot of copying.
You need to determine how the size of the datastructure is to be limited,
e.g. total number of chararacters, number of strings held and so on.

--
-----------------------------------------

-----------------------------------------

Sun, 07 Nov 1999 03:00:00 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages