made work: inserting a value in a list at a given index
This commit is contained in:
@@ -49,19 +49,19 @@ Examples of this work of art are in the `/examples/` directory of this repo.
|
||||
|
||||
#### What works so far:
|
||||
|
||||
+ Creating a list
|
||||
+ Pushing into a list
|
||||
+ Printing a list
|
||||
+ Popping the last value of a list
|
||||
+ Cleanup of a list
|
||||
+ Vectors inside of a list
|
||||
1. Creating a list
|
||||
2. Pushing into a list
|
||||
3. Printing a list
|
||||
4. Popping the last value of a list
|
||||
5. Cleanup of a list
|
||||
6. Vectors inside of a list
|
||||
7. Inserting at a given index
|
||||
|
||||
#### TODO:
|
||||
|
||||
1. Inserting at a given index
|
||||
2. Verify multi-dimensional lists working - lists of lists
|
||||
3. Get/set at a given index
|
||||
4. Remove at a given index
|
||||
1. Verify multi-dimensional lists working - lists of lists
|
||||
2. Get/set at a given index
|
||||
3. Remove at a given index
|
||||
|
||||
## Installation
|
||||
|
||||
|
||||
@@ -1,81 +1,36 @@
|
||||
// #include <stdio.h>
|
||||
// #include <odweta/util.h>
|
||||
//
|
||||
// int main() {
|
||||
// char* string = intToString(1729843);
|
||||
//
|
||||
// printf("final string: %s\n", string);
|
||||
//
|
||||
// char* sstring = intToString(123);
|
||||
//
|
||||
// printf("final string: %s\n", sstring);
|
||||
//
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
#include <odweta/file.h>
|
||||
#include <odweta/list.h>
|
||||
#include <odweta/vector.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
printf("creating a file using 'fileInit(<filename>)' (returns a file_t*)\n");
|
||||
file_t* f = fileInit("test.txt");
|
||||
printf("creating a list using 'listInit()' (returns a list_t*)\n");
|
||||
list_t* l = listInit();
|
||||
|
||||
printf("printing the file contents\n\n");
|
||||
filePrint(f);
|
||||
printf("pushing an integer value into the list\nnote the kind of unusual way of passing the number argument: if you want to pass an expression (e.g. the value '1' or '4*4'), something not stored in a variable, you follow this syntax - &(type){value} (e.g. &(int){1 + 1})\nalso, the type of the variable must be given as the second argument to the function\n->'listPush(list_t*, INT, &(int){123})'\n");
|
||||
listPush(l, INT, &(int){123});
|
||||
listPrint(l);
|
||||
|
||||
printf("\nconvering the file contents into a string (char*)\n");
|
||||
char* string = fileToString(f);
|
||||
printf("\"\"\"\n%s\"\"\"\n", string);
|
||||
printf("popping the last value of the list; in this case, it is an integer, so you type: 'int poppedValue = *((int *)listPop(list_t*))'\n");
|
||||
int poppedValue = *((int *)listPop(l));
|
||||
printf("popped: %d\n", poppedValue);
|
||||
listPrint(l);
|
||||
|
||||
printf("writing is quite simple, just call the 'fileWrite(file_t*, char*)' or 'fileWriteln(file_t*, char*)' function, where the second argument is the string you want to write into a file\nthe 'fileWriteln()' function writes the specified string AND a '\\n' after it, whereas the 'fileWrite()' function does not append the '\\n'\n");
|
||||
fileWrite(f, "'this will not end with a \"\\n\"' ");
|
||||
fileWriteln(f, "this line was appended");
|
||||
filePrint(f);
|
||||
printf("more examples with diferrent types\nnote that the vector_t* is a pointer, so there is no need to write the special syntax!\n");
|
||||
listPush(l, FLOAT, &(float){3.14});
|
||||
listPush(l, CHAR, &(char){'x'});
|
||||
vector_t* int_vec = vectorInit(INT);
|
||||
vectorPush(int_vec, &(int){456});
|
||||
listPush(l, VECTOR, int_vec);
|
||||
|
||||
printf("if you are finished working with the file BEFORE the immediate end of the program, run 'fileCleanup(file_t*)', otherwise it is not strictly necessary\nit will, though, append a '\\n' at the end of the file if you have not done so, making writing into files more consistent, so for this reason, I recommend you use it with every file_t*\n");
|
||||
fileCleanup(f);
|
||||
listPrint(l);
|
||||
|
||||
printf("copying and moving\nafter this block of code, the contents of test.txt will have been copied into copy_of_test.txt and moved to moved_copy_of_test.txt, which means that copy_of_test.txt is deleted and the contents of test.txt are only in test.txt and moved_copy_of_test.txt\n");
|
||||
printf("inserting at index 1\n");
|
||||
listInsertAt(l, 1, FLOAT, &(float){6.67});
|
||||
|
||||
file_t* src = fileInit("test.txt");
|
||||
listPrint(l);
|
||||
|
||||
fileCopy(src, "copy_of_test.txt");
|
||||
|
||||
printf("src:\n");
|
||||
filePrint(src);
|
||||
|
||||
printf("\ndst:\n");
|
||||
file_t* dst = fileInit("copy_of_test.txt");
|
||||
filePrint(dst);
|
||||
|
||||
fileMove(dst, "moved_copy_of_test.txt");
|
||||
|
||||
printf("moved file:\n");
|
||||
file_t* move_dst = fileInit("moved_copy_of_test.txt");
|
||||
filePrint(move_dst);
|
||||
|
||||
fileCleanup(src);
|
||||
printf("DO NOT cleanup a deleted file! (dst, in this case) it is already done for you by the 'fileMove(file_t*, char*)' function\n");
|
||||
// fileCleanup(dst);
|
||||
fileCleanup(move_dst);
|
||||
|
||||
printf("when copying and moving files, the first argument is a file_t* (source file) and the second argument is a char* (the path of the destination file), not a file_t*!\n");
|
||||
|
||||
printf("prepending line numbers at the beggining of each line in a file\n");
|
||||
|
||||
file_t* g = fileInit("test.txt");
|
||||
fileCopy(g, "copy_of_test.txt");
|
||||
file_t* g_copy = fileInit("copy_of_test.txt");
|
||||
fileNumberLines(g_copy);
|
||||
|
||||
printf("file withOUT prepended numbers:\n");
|
||||
filePrint(g);
|
||||
|
||||
printf("file with prepended numbers:\n");
|
||||
filePrint(g_copy);
|
||||
|
||||
fileCleanup(g);
|
||||
//fileCleanup(g_copy);
|
||||
printf("if you are finished working with the list BEFORE the immediate end of the program, run 'listCleanup(list_t*)', otherwise it is not strictly necessary\n");
|
||||
listCleanup(l);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,11 @@ int main() {
|
||||
|
||||
listPrint(l);
|
||||
|
||||
printf("inserting at index 1\n");
|
||||
listInsertAt(l, 1, FLOAT, &(float){6.67});
|
||||
|
||||
listPrint(l);
|
||||
|
||||
printf("if you are finished working with the list BEFORE the immediate end of the program, run 'listCleanup(list_t*)', otherwise it is not strictly necessary\n");
|
||||
listCleanup(l);
|
||||
|
||||
|
||||
@@ -26,3 +26,5 @@ void listPrint(list_t* l); // prints a list
|
||||
void listCleanup(list_t* l); // freeing pointers
|
||||
|
||||
void* listPop(list_t* l); // popping the last value of the list and returning it
|
||||
|
||||
void listInsertAt(list_t* l, int index, dataType_e type, void* element); // inserts an element at a given index
|
||||
|
||||
+52
@@ -110,3 +110,55 @@ listPop(list_t* l) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
listInsertAt(list_t* l, int index, dataType_e type, void* element) {
|
||||
if (l->size == 0) {
|
||||
switch (type) {
|
||||
case INT:
|
||||
listPush(l, INT, element);
|
||||
break;
|
||||
case FLOAT:
|
||||
listPush(l, FLOAT, element);
|
||||
break;
|
||||
case CHAR:
|
||||
listPush(l, CHAR, element);
|
||||
break;
|
||||
case VECTOR:
|
||||
listPush(l, VECTOR, element);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
vector_t* newVec;
|
||||
switch (type) {
|
||||
case INT:
|
||||
newVec = vectorInit(INT);
|
||||
vectorPush(newVec, element);
|
||||
vectorInsertAt((vector_t *)l->data, index, newVec);
|
||||
break;
|
||||
case FLOAT:
|
||||
newVec = vectorInit(FLOAT);
|
||||
vectorPush(newVec, element);
|
||||
vectorInsertAt((vector_t *)l->data, index, newVec);
|
||||
break;
|
||||
case CHAR:
|
||||
newVec = vectorInit(CHAR);
|
||||
vectorPush(newVec, element);
|
||||
vectorInsertAt((vector_t *)l->data, index, newVec);
|
||||
break;
|
||||
case VECTOR:
|
||||
newVec = vectorInit(VECTOR);
|
||||
vectorPush(newVec, element);
|
||||
vectorInsertAt((vector_t *)l->data, index, newVec);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "specify a valid vector type\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
l->size += 1;
|
||||
if (l->size == l->capacity) {
|
||||
l->capacity *= 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user