Object
|
+---DListIterator
The DListIterator class implements a number of methods for iterating lists. //
#include <stdio.h>
#include "ofc/DList.h"
#include "ofc/DInt.h"
#include "ofc/DText.h"
int main(int argc, char *argv[])
{
DList *list = [DList new];
DListIterator *iter;
DInt *nr;
DText *str;
DInt *nr7;
id obj;
// Add strings and numbers to the list
nr = [DInt alloc]; [nr init :4];
[list append :nr]; // Append objects in list
nr = [DInt alloc]; [nr init :8];
[list append :nr];
str = [DText alloc]; [str init :"hello"];
[list append :str];
nr7 = [DInt alloc]; [nr7 init :7];
[list append :nr7];
str = [ DText alloc]; [str init :"bye"];
[list append :str];
printf("List has %ld elements.\n", [list length]); // Get the lenght of the list
nr = [DInt alloc]; [nr init :3];
[list insert :3 :nr]; // Insert an object in the list
str = [DText alloc]; [str init :"replace"];
nr = [list set :1 :str]; // Index 2 is replaced by "replace"
[nr free];
str = [list get :2]; // Get an object from the list
printf("Element 2 is \"%s\".\n", [str cstring]);
printf("List contains object nr7 %ld times.\n", [list count :nr7]); // Count occurences
printf("Index of object nr7 is %ld.\n", [list index :nr7]); // Find index of object
if ([list remove :nr7]) // Remove an object from the list
{
printf("Object nr7 is removed from the list.\n");
[nr7 free];
}
else
printf("Object nr7 could not be found in the list.\n");
str = [list join :',']; // Join all objects text to one text object
printf("Join of all objects text in the list:%s.\n", [str cstring]);
[str free];
// Iterate the list
iter = [DListIterator new];
[iter list :list]; // Set the list in the iterator
obj = [iter last]; // Iterate the list backwards
while (obj != nil)
{
if ([obj isKindOf :[DText class]]) // Find out the type of the object and ..
{
str = obj;
printf("Element: %s.\n", [str cstring]); // .. print the contents
}
else
if ([obj isKindOf :[DInt class]])
{
nr = obj;
printf("Element: %d\n", [nr get]);
}
else
printf("Element: unknown.\n");
obj = [iter prev];
}
[iter free]; // Cleanup
[list free];
return 0;
}