Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <string.h>
- //NUMLIST
- typedef struct numlist_element numlist_element;
- typedef struct numlist_handle numlist_handle;
- numlist_handle * numlist_newHandle();
- numlist_element * numlist_newElement(double element);
- double numlist_get(int index, numlist_handle * head);
- void numlist_add(double element, numlist_handle * head);
- void numlist_remove(int index, numlist_handle * head);
- void numlist_insert(int index, numlist_handle * head, double element);
- void numlist_clear(numlist_handle * head);
- void numlist_reverse(numlist_handle * head);
- void numlist_swap(numlist_handle * head, int first, int second);
- void numlist_set(int index, double value, numlist_handle * head);
- void numlist_sort(numlist_handle * head);
- numlist_handle numlist_copy(numlist_handle * source);
- //BOOLLIST
- typedef struct boollist_element boollist_element;
- typedef struct boollist_handle boollist_handle;
- boollist_handle * boollist_newHandle();
- boollist_element * boollist_newElement(int element);
- int boollist_get(int index, boollist_handle * head);
- void boollist_add(int element, boollist_handle * head);
- void boollist_remove(int index, boollist_handle * head);
- void boollist_insert(int index, boollist_handle * head, int element);
- void boollist_clear(boollist_handle * head);
- void boollist_reverse(boollist_handle * head);
- void boollist_swap(boollist_handle * head, int first, int second);
- void boollist_set(int index, int value, boollist_handle * head);
- boollist_handle boollist_copy(boollist_handle * source);
- //STRING
- typedef struct string_element string_element;
- typedef struct string_handle string_handle;
- string_handle * string_newHandle();
- string_element * string_newElement(char element);
- char string_get(int index, string_handle * head);
- void string_add(char element, string_handle * head);
- void string_remove(int index, string_handle * head);
- void string_insert(int index, string_handle * head, char element);
- void string_clear(string_handle * head);
- void string_reverse(string_handle * head);
- void string_swap(string_handle * head, int first, int second);
- void string_set(int index, char value, string_handle * head);
- void string_sort(string_handle * head);
- string_handle string_copy(string_handle * source);
- int string_equals(string_handle * first, string_handle * second);
- //LIST OF STRINGS
- typedef struct stringlist_handle stringlist_handle;
- typedef struct stringlist_element stringlist_element;
- stringlist_handle stringlist_new();
- stringlist_element *stringlist_newElement(string_handle *inputElement);
- string_handle stringlist_get(int index, stringlist_handle * head);
- void stringlist_add(string_handle *inputElement, stringlist_handle * head);
- void stringlist_remove(int index, stringlist_handle * head);
- void stringlist_insert(int index, stringlist_handle * head, string_handle *inputElement);
- void stringlist_clear(stringlist_handle * head);
- void stringlist_reverse(stringlist_handle * head);
- void stringlist_swap(stringlist_handle * head, int first, int second);
- void stringlist_set(int index, string_handle *value, stringlist_handle * head);
- stringlist_handle stringlist_copy(stringlist_handle * source);
- //STANDARD FUNCTIONS
- int program_convertNumToString(double input, string_handle * output);
- int program_convertBoolToString(int input, string_handle * output);
- int program_convertStringToNum(string_handle * input, double *output);
- int program_convertStringToBool(string_handle * input, int *output);
- string_handle standard_read();
- void standard_printNum(double input);
- void standard_printBool(int input);
- void standard_printString(string_handle * input);
- void standard_printChars(char input[]);
- string_handle standard_createString(char input[]);
- void standard_appendChars(string_handle * head, char input[]);
- void standard_appendString(string_handle * head, string_handle * input);
- void standard_appendNum(string_handle * head, double input);
- void standard_appendBool(string_handle * head, int input);
- //NUMLIST
- struct numlist_handle {
- numlist_element *first;
- numlist_element *last;
- int size;
- };
- struct numlist_element{
- double element;
- numlist_element *next;
- };
- numlist_handle numlist_new(){
- numlist_handle head;
- head.first = NULL;
- head.last = NULL;
- head.size = 0;
- return head;
- }
- numlist_element *numlist_newElement(double inputElement){
- numlist_element * element = malloc(sizeof(numlist_element));
- element->element = inputElement;
- element->next = NULL;
- return element;
- }
- double numlist_get(int index, numlist_handle * head){
- numlist_element * current = head->first;
- int i;
- for (i=0; i<index && current != NULL; i++) {
- current = current->next;
- }
- if (current == NULL){
- raise(SIGSEGV); //Segmentation fault
- }
- return current->element;
- }
- void numlist_add(double inputElement, numlist_handle * head){
- numlist_element * element = numlist_newElement(inputElement);
- if (head->first == NULL)
- {
- head->first = element;
- }
- else{
- numlist_element * current = head->first;
- while(current->next != NULL){
- current = current->next;
- }
- current->next = element;
- }
- head->last = element;
- head->size ++;
- }
- void numlist_remove(int index, numlist_handle * head){
- if (index >= head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- numlist_element * current = head->first;
- numlist_element * previous = NULL;
- if(index == 0 && head->first != NULL){
- head->first = head->first->next;
- }
- else{
- for (int i=0; i<index; i++) {
- previous = current;
- current = current->next;
- }
- previous->next = current->next;
- }
- if(current->next == NULL){
- head->last = previous;
- }
- free(current); //releases memory
- head->size--;
- }
- void numlist_insert(int index, numlist_handle * head, double inputElement){
- if (index > head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- numlist_element * element = numlist_newElement(inputElement);
- numlist_element * current = head->first;
- numlist_element * previous = NULL;
- if(index == 0){
- element->next = current;
- head->first = element;
- }
- else{
- for (int i=0; i<index; i++) {
- previous = current;
- current = current->next;
- }
- element->next = current;
- previous->next = element;
- }
- if(element->next == NULL){
- head->last = element;
- }
- head->size++;
- }
- void numlist_clear(numlist_handle * head){
- while(head->size != 0){
- numlist_remove(0, head);
- }
- }
- void numlist_reverse(numlist_handle * head){
- //PROBLEMER MED LISTE AF LISTER
- numlist_handle temporayHandle = numlist_new();
- while(head->size != 0){
- double element = numlist_get(0, head);
- numlist_remove(0, head);
- numlist_insert(0, &temporayHandle, element);
- }
- //ET ALTERNATIV TIL HEAD = TEMPORAYHANDLE?
- while(temporayHandle.size != 0){
- double element = numlist_get(0, &temporayHandle);
- numlist_remove(0, &temporayHandle);
- numlist_add(element, head);
- }
- }
- void numlist_swap(numlist_handle * head, int first, int second){
- if (first >= head->size || second >= head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- double temp = numlist_get(first, head);
- numlist_set(first, numlist_get(second, head), head);
- numlist_set(second, temp, head);
- }
- void numlist_set(int index, double value, numlist_handle * head){
- if (index >= head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- numlist_element * current = head->first;
- for (int i=0; i<index; i++) {
- current = current->next;
- }
- current->element = value;
- }
- int max (numlist_handle * head, int n, int i, int j, int k) {
- int m = i;
- if (j < n && numlist_get(j, head) > numlist_get(m, head)) {
- m = j;
- }
- if (k < n && numlist_get(k, head) > numlist_get(m, head)) {
- m = k;
- }
- return m;
- }
- void downheap (numlist_handle * head, int n, int i) {
- while (1) {
- int j = max(head, n, i, 2 * i + 1, 2 * i + 2);
- if (j == i) {
- break;
- }
- numlist_swap(head, j, i);
- i = j;
- }
- }
- void numlist_sort(numlist_handle * head){
- //https://rosettacode.org/wiki/Sorting_algorithms/Heapsort#C
- int n = head->size;
- for (int i = (n - 2) / 2; i >= 0; i--) {
- downheap(head, n, i);
- }
- for (int i = 0; i < n; i++) {
- numlist_swap(head, n - i - 1, 0);
- downheap(head, n - i - 1, 0);
- }
- }
- numlist_handle numlist_copy(numlist_handle * source){
- numlist_handle destination = numlist_new();
- numlist_element * current = source->first;
- for (int i = 0; i < source->size; ++i)
- {
- numlist_element * element = numlist_newElement(current->element);
- current = current->next;
- numlist_add(element->element, &destination);
- }
- return destination;
- }
- //BOOLLIST
- struct boollist_handle {
- boollist_element *first;
- boollist_element *last;
- int size;
- };
- struct boollist_element{
- int element;
- boollist_element *next;
- };
- boollist_handle boollist_new(){
- boollist_handle head;
- head.first = NULL;
- head.last = NULL;
- head.size = 0;
- return head;
- }
- boollist_element *boollist_newElement(int inputElement){ // ALLWAYS HAVE ADDCHARACTORTOLIST() CALL THIS!
- boollist_element * element = malloc(sizeof(boollist_element));
- element->element = inputElement;
- element->next = NULL;
- return element;
- }
- int boollist_get(int index, boollist_handle * head){
- boollist_element * current = head->first;
- int i;
- for (i=0; i<index && current != NULL; i++) {
- current = current->next;
- }
- if (current == NULL){
- raise(SIGSEGV); //Segmentation fault
- }
- return current->element;
- }
- void boollist_add(int inputElement, boollist_handle * head){
- boollist_element * element = boollist_newElement(inputElement);
- if (head->first == NULL)
- {
- head->first = element;
- }
- else{
- boollist_element * current = head->first;
- while(current->next != NULL){
- current = current->next;
- }
- current->next = element;
- }
- head->last = element;
- head->size ++;
- }
- void boollist_remove(int index, boollist_handle * head){
- if (index >= head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- boollist_element * current = head->first;
- boollist_element * previous = NULL;
- if(index == 0 && head->first != NULL){
- head->first = head->first->next;
- }
- else{
- for (int i=0; i<index; i++) {
- previous = current;
- current = current->next;
- }
- previous->next = current->next;
- }
- if(current->next == NULL){
- head->last = previous;
- }
- free(current); //releases memory
- head->size--;
- }
- void boollist_insert(int index, boollist_handle * head, int inputElement){
- if (index > head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- boollist_element * element = boollist_newElement(inputElement);
- boollist_element * current = head->first;
- boollist_element * previous = NULL;
- if(index == 0){
- element->next = current;
- head->first = element;
- }
- else{
- for (int i=0; i<index; i++) {
- previous = current;
- current = current->next;
- }
- element->next = current;
- previous->next = element;
- }
- if(element->next == NULL){
- head->last = element;
- }
- head->size++;
- }
- void boollist_clear(boollist_handle * head){
- while(head->size != 0){
- boollist_remove(0, head);
- }
- }
- void boollist_reverse(boollist_handle * head){
- //PROBLEMER MED LISTE AF LISTER
- boollist_handle temporayHandle = boollist_new();
- while(head->size != 0){
- int element = boollist_get(0, head);
- boollist_remove(0, head);
- boollist_insert(0, &temporayHandle, element);
- }
- //ET ALTERNATIV TIL HEAD = TEMPORAYHANDLE?
- while(temporayHandle.size != 0){
- int element = boollist_get(0, &temporayHandle);
- boollist_remove(0, &temporayHandle);
- boollist_add(element, head);
- }
- }
- void boollist_swap(boollist_handle * head, int first, int second){
- if (first >= head->size || second >= head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- int temp = boollist_get(first, head);
- boollist_set(first, boollist_get(second, head), head);
- boollist_set(second, temp, head);
- }
- void boollist_set(int index, int value, boollist_handle * head){
- if (index >= head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- boollist_element * current = head->first;
- for (int i=0; i<index; i++) {
- current = current->next;
- }
- current->element = value;
- }
- boollist_handle boollist_copy(boollist_handle * source){
- boollist_handle destination = boollist_new();
- boollist_element * current = source->first;
- for (int i = 0; i < source->size; ++i)
- {
- boollist_element * element = boollist_newElement(current->element);
- current = current->next;
- boollist_add(element->element, &destination);
- }
- return destination;
- }
- //STRING
- struct string_handle {
- string_element *first;
- string_element *last;
- int size;
- };
- struct string_element{
- char element;
- string_element *next;
- };
- string_handle string_new(){
- string_handle head;
- head.first = NULL;
- head.last = NULL;
- head.size = 0;
- return head;
- }
- string_element *string_newElement(char inputElement){ // ALLWAYS HAVE ADDCHARACTORTOLIST() CALL THIS!
- string_element * element = malloc(sizeof(string_element));
- element->element = inputElement;
- element->next = NULL;
- return element;
- }
- char string_get(int index, string_handle * head){
- string_element * current = head->first;
- int i;
- for (i=0; i<index && current != NULL; i++) {
- current = current->next;
- }
- if (current == NULL){
- raise(SIGSEGV); //Segmentation fault
- }
- return current->element;
- }
- void string_add(char inputElement, string_handle * head){
- string_element * element = string_newElement(inputElement);
- if (head->first == NULL)
- {
- head->first = element;
- }
- else{
- string_element * current = head->first;
- while(current->next != NULL){
- current = current->next;
- }
- current->next = element;
- }
- head->last = element;
- head->size ++;
- }
- void string_remove(int index, string_handle * head){
- if (index >= head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- string_element * current = head->first;
- string_element * previous = NULL;
- if(index == 0 && head->first != NULL){
- head->first = head->first->next;
- }
- else{
- for (int i=0; i<index; i++) {
- previous = current;
- current = current->next;
- }
- previous->next = current->next;
- }
- if(current->next == NULL){
- head->last = previous;
- }
- free(current); //releases memory
- head->size--;
- }
- void string_insert(int index, string_handle * head, char inputElement){
- if (index > head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- string_element * element = string_newElement(inputElement);
- string_element * current = head->first;
- string_element * previous = NULL;
- if(index == 0){
- element->next = current;
- head->first = element;
- }
- else{
- for (int i=0; i<index; i++) {
- previous = current;
- current = current->next;
- }
- element->next = current;
- previous->next = element;
- }
- if(element->next == NULL){
- head->last = element;
- }
- head->size++;
- }
- void string_clear(string_handle * head){
- while(head->size != 0){
- string_remove(0, head);
- }
- }
- void string_reverse(string_handle * head){
- //PROBLEMER MED LISTE AF LISTER
- string_handle temporayHandle = string_new();
- while(head->size != 0){
- char element = string_get(0, head);
- string_remove(0, head);
- string_insert(0, &temporayHandle, element);
- }
- //ET ALTERNATIV TIL HEAD = TEMPORAYHANDLE?
- while(temporayHandle.size != 0){
- char element = string_get(0, &temporayHandle);
- string_remove(0, &temporayHandle);
- string_add(element, head);
- }
- }
- void string_swap(string_handle * head, int first, int second){
- if (first >= head->size || second >= head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- char temp = string_get(first, head);
- string_set(first, string_get(second, head), head);
- string_set(second, temp, head);
- }
- void string_set(int index, char value, string_handle * head){
- if (index >= head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- string_element * current = head->first;
- for (int i=0; i<index; i++) {
- current = current->next;
- }
- current->element = value;
- }
- string_handle string_copy(string_handle * source){
- string_handle destination = string_new();
- string_element * current = source->first;
- for (int i = 0; i < source->size; ++i)
- {
- string_element * element = string_newElement(current->element);
- current = current->next;
- string_add(element->element, &destination);
- }
- return destination;
- }
- int string_equals(string_handle * first, string_handle * second){
- if(first->size == second->size){
- for (int i = 0; i < first->size; ++i)
- {
- if(string_get(i, first) != string_get(i, second))
- return 0;
- }
- return 1;
- }
- else
- return 0;
- }
- //LIST OF STRINGS
- struct stringlist_handle {
- stringlist_element *first;
- stringlist_element *last;
- int size;
- };
- struct stringlist_element{
- string_handle element;
- stringlist_element *next;
- };
- stringlist_handle stringlist_new(){
- stringlist_handle head;
- head.first = NULL;
- head.last = NULL;
- head.size = 0;
- return head;
- }
- stringlist_element *stringlist_newElement(string_handle *inputElement){
- stringlist_element * element = malloc(sizeof(stringlist_element));
- element->element = *inputElement;
- element->next = NULL;
- return element;
- }
- string_handle stringlist_get(int index, stringlist_handle * head){
- stringlist_element * current = head->first;
- int i;
- for (i=0; i<index && current != NULL; i++) {
- current = current->next;
- }
- if (current == NULL){
- raise(SIGSEGV); //Segmentation fault
- }
- return current->element;
- }
- void stringlist_add(string_handle *inputElement, stringlist_handle * head){
- stringlist_element * element = stringlist_newElement(inputElement);
- if (head->first == NULL)
- {
- head->first = element;
- }
- else{
- stringlist_element * current = head->first;
- while(current->next != NULL){
- current = current->next;
- }
- current->next = element;
- }
- head->last = element;
- head->size ++;
- }
- void stringlist_remove(int index, stringlist_handle * head){
- if (index >= head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- stringlist_element * current = head->first;
- stringlist_element * previous = NULL;
- if(index == 0 && head->first != NULL){
- head->first = head->first->next;
- }
- else{
- for (int i=0; i<index; i++) {
- previous = current;
- current = current->next;
- }
- previous->next = current->next;
- }
- if(current->next == NULL){
- head->last = previous;
- }
- free(current); //releases memory
- head->size--;
- }
- void stringlist_insert(int index, stringlist_handle * head, string_handle *inputElement){
- if (index > head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- stringlist_element * element = stringlist_newElement(inputElement);
- stringlist_element * current = head->first;
- stringlist_element * previous = NULL;
- if(index == 0){
- element->next = current;
- head->first = element;
- }
- else{
- for (int i=0; i<index; i++) {
- previous = current;
- current = current->next;
- }
- element->next = current;
- previous->next = element;
- }
- if(element->next == NULL){
- head->last = element;
- }
- head->size++;
- }
- void stringlist_clear(stringlist_handle * head){
- while(head->size != 0){
- stringlist_remove(0, head);
- }
- }
- void stringlist_reverse(stringlist_handle * head){
- stringlist_handle temporayHandle = stringlist_new();
- for (int i = 0; i < head->size; ++i)
- {
- string_handle temp = stringlist_get(i, head);
- stringlist_insert(0, &temporayHandle, &temp);
- }
- for (int i = 0; i < temporayHandle.size; ++i)
- {
- string_handle temp = stringlist_get(i, &temporayHandle);
- stringlist_set(i, &temp, head);
- }
- }
- void stringlist_swap(stringlist_handle * head, int first, int second){
- if (first >= head->size || second >= head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- string_handle tempFirst = stringlist_get(first, head);
- string_handle tempSecond = stringlist_get(second, head);
- stringlist_set(first, &tempSecond, head);
- stringlist_set(second, &tempFirst, head);
- }
- void stringlist_set(int index, string_handle *value, stringlist_handle * head){
- if (index >= head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- stringlist_element * current = head->first;
- for (int i=0; i<index; i++) {
- current = current->next;
- }
- current->element = *value;
- }
- stringlist_handle stringlist_copy(stringlist_handle * source){
- stringlist_handle destination = stringlist_new();
- stringlist_element * current = source->first;
- for (int i = 0; i < source->size; ++i)
- {
- stringlist_element * element = stringlist_newElement(¤t->element);
- current = current->next;
- stringlist_add(&element->element, &destination);
- }
- return destination;
- }
- //STANDARD FUNCTIONS
- int program_convertNumToString(double input, string_handle * output){
- char str[50];
- sprintf(str,"%lf",input);
- string_clear(output);
- for (int i = 0; str[i] != '\0' && i<50; ++i)
- {
- string_add(str[i], output);
- }
- return 1;
- }
- int program_convertBoolToString(int input, string_handle * output){
- char str[6], t[] = "true", f[] = "false";
- string_clear(output);
- if(input){
- strcpy(str, t);
- }
- else
- strcpy(str, f);
- for (int i = 0; str[i] != '\0' && i<6; ++i)
- string_add(str[i], output);
- return 1;
- }
- int program_convertStringToNum(string_handle * input, double *output){
- char *str = malloc(sizeof(char)*input->size);
- for (int i = 0; i < input->size; ++i)
- {
- str[i] = string_get(i, input);
- }
- int result = sscanf(str, "%lf", output);
- free(str);
- return result;
- }
- int program_convertStringToBool(string_handle * input, int *output){
- char *str = malloc(sizeof(char)*input->size);
- for (int i = 0; i < input->size; ++i)
- {
- str[i] = string_get(i, input);
- }
- int result = sscanf(str, "%d", output);
- free(str);
- return result;
- }
- string_handle standard_read(){
- char str[100];
- scanf ("%[^\n]%*c", str);
- string_handle head = string_new();
- for (int i = 0; str[i] != '\0' && i<100; ++i)
- {
- string_add(str[i], &head);
- }
- fflush(stdin);
- return head;
- }
- void standard_printNum(double input){
- printf("%lf", input);
- }
- void standard_printBool(int input){
- string_handle myString = string_new();
- program_convertBoolToString(input, &myString);
- for (int i = 0; i < myString.size; ++i)
- {
- printf("%c", string_get(i, &myString));
- }
- }
- void standard_printString(string_handle * input){
- for (int i = 0; i < input->size; ++i)
- {
- printf("%c", string_get(i, input));
- }
- }
- void standard_printChars(char input[]){
- printf("%s", input);
- }
- string_handle standard_createString(char input[]){
- string_handle myString = string_new();
- for (int i = 0; input[i] != '\0'; ++i)
- {
- string_add(input[i], &myString);
- }
- return myString;
- }
- void standard_appendChars(string_handle * head, char input[]){
- string_handle myInputString = standard_createString(input);
- standard_appendString(head, &myInputString);
- string_clear(&myInputString);
- }
- void standard_appendString(string_handle * head, string_handle * input){
- for (int i = 0; i < input->size; ++i)
- {
- string_add(string_get(i, input), head);
- }
- /* if(head->size == 0){
- head->last = input->last;
- head->first = input->first;
- }
- else{
- head->last->next = input->first;
- head->last = input->last;
- }
- head->size += input->size;*/
- }
- void standard_appendNum(string_handle * head, double input){
- string_handle myString = string_new();
- program_convertNumToString(input, &myString);
- standard_appendString(head, &myString);
- }
- void standard_appendBool(string_handle * head, int input){
- string_handle myString = string_new();
- program_convertBoolToString(input, &myString);
- standard_appendString(head, &myString);
- }
- typedef struct person person;
- string_handle person_ToString(person *this);
- typedef struct personlist_handle personlist_handle;
- typedef struct personlist_element personlist_element;
- personlist_handle personlist_new();
- personlist_element *personlist_newElement(person *inputElement);
- person personlist_get(int index, personlist_handle * head);
- void personlist_add(person *inputElement, personlist_handle * head);
- void personlist_remove(int index, personlist_handle * head);
- void personlist_insert(int index, personlist_handle * head, person *inputElement);
- void personlist_clear(personlist_handle * head);
- void personlist_reverse(personlist_handle * head);
- void personlist_swap(personlist_handle * head, int first, int second);
- void personlist_set(int index, person *value, personlist_handle * head);
- personlist_handle personlist_copy(personlist_handle * source);struct person {
- double age;string_handle name;
- };
- struct personlist_handle {
- personlist_element *first;
- personlist_element *last;
- int size;
- };
- struct personlist_element{
- person element;
- personlist_element *next;
- };
- personlist_handle personlist_new(){
- personlist_handle head;
- head.first = NULL;
- head.last = NULL;
- head.size = 0;
- return head;
- }
- personlist_element *personlist_newElement(person *inputElement){
- personlist_element * element = malloc(sizeof(personlist_element));
- element->element = *inputElement;
- element->next = NULL;
- return element;
- }
- person personlist_get(int index, personlist_handle * head){
- personlist_element * current = head->first;
- int i;
- for (i=0; i<index && current != NULL; i++) {
- current = current->next;
- }
- if (current == NULL){
- raise(SIGSEGV); //Segmentation fault
- }
- return current->element;
- }
- void personlist_add(person *inputElement, personlist_handle * head){
- personlist_element * element = personlist_newElement(inputElement);
- if (head->first == NULL)
- {
- head->first = element;
- }
- else{
- personlist_element * current = head->first;
- while(current->next != NULL){
- current = current->next;
- }
- current->next = element;
- }
- head->last = element;
- head->size ++;
- }
- void personlist_remove(int index, personlist_handle * head){
- if (index >= head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- personlist_element * current = head->first;
- personlist_element * previous = NULL;
- if(index == 0 && head->first != NULL){
- head->first = head->first->next;
- }
- else{
- for (int i=0; i<index; i++) {
- previous = current;
- current = current->next;
- }
- previous->next = current->next;
- }
- if(current->next == NULL){
- head->last = previous;
- }
- string_clear(¤t->element.name);
- free(current); //releases memory
- head->size--;
- }
- void personlist_insert(int index, personlist_handle * head, person *inputElement){
- if (index > head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- personlist_element * element = personlist_newElement(inputElement);
- personlist_element * current = head->first;
- personlist_element * previous = NULL;
- if(index == 0){
- element->next = current;
- head->first = element;
- }
- else{
- for (int i=0; i<index; i++) {
- previous = current;
- current = current->next;
- }
- element->next = current;
- previous->next = element;
- }
- if(element->next == NULL){
- head->last = element;
- }
- head->size++;
- }
- void personlist_clear(personlist_handle * head){
- while(head->size != 0){
- personlist_remove(0, head);
- }
- }
- void personlist_reverse(personlist_handle * head){
- personlist_handle temporayHandle = personlist_new();
- for (int i = 0; i < head->size; ++i)
- {
- person temp = personlist_get(i, head);
- personlist_insert(0, &temporayHandle, &temp);
- }
- for (int i = 0; i < temporayHandle.size; ++i)
- {
- person temp = personlist_get(i, &temporayHandle);
- personlist_set(i, &temp, head);
- }
- }
- void personlist_swap(personlist_handle * head, int first, int second){
- if (first >= head->size || second >= head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- person tempFirst = personlist_get(first, head);
- person tempSecond = personlist_get(second, head);
- personlist_set(first, &tempSecond, head);
- personlist_set(second, &tempFirst, head);
- }
- void personlist_set(int index, person *value, personlist_handle * head){
- if (index >= head->size){
- raise(SIGSEGV); //Segmentation fault
- }
- personlist_element * current = head->first;
- for (int i=0; i<index; i++) {
- current = current->next;
- }
- current->element = *value;
- }
- personlist_handle personlist_copy(personlist_handle * source){
- personlist_handle destination = personlist_new();
- personlist_element * current = source->first;
- for (int i = 0; i < source->size; ++i)
- {
- personlist_element * element = personlist_newElement(¤t->element);
- current = current->next;
- personlist_add(&element->element, &destination);
- }
- return destination;
- }
- person person_copy(person* input){
- person * returnValue = malloc(sizeof(person));
- returnValue->age = input->age;
- returnValue->name = string_copy(&input->name);
- return *returnValue;
- }
- string_handle person_ToString(person *this){string_handle tempName = this->name;tempName = string_copy(&tempName);double tempAge = this->age;string_handle _0 = string_new();
- standard_appendChars(&_0,"");standard_appendNum(&_0,tempAge);standard_appendChars(&_0,"\n");return _0;string_clear(&_0);}
- void main(){
- personlist_handle personList = personlist_new();
- string_handle _1 = string_new();
- standard_appendChars(&_1,"please enter the information asked for if you wish to stop enter \"Cancel\"");standard_printString(&_1);string_clear(&_1);
- string_handle _2 = string_new();
- standard_appendChars(&_2,"");string_handle name = _2;name = string_copy(&name);string_clear(&_2);
- string_handle _3 = string_new();
- standard_appendChars(&_3,"Cancel");while ((!string_equals(&name, &_3))){
- string_handle _3 = string_new();
- standard_appendChars(&_3,"Enter your name:");standard_printString(&_3);string_clear(&_3);
- name = standard_read();
- string_handle _4 = string_new();
- standard_appendChars(&_4,"Cancel");if((!string_equals(&name, &_4))){
- string_handle _4 = string_new();
- standard_appendChars(&_4,"Enter your age:");standard_printString(&_4);string_clear(&_4);
- string_handle ageString = standard_read();
- double ageNum = 0 ;
- if(!program_convertStringToNum(&ageString, &ageNum)){
- string_handle _5 = string_new();
- standard_appendChars(&_5,"Value entered was not a numeric value age was set to 0");standard_printString(&_5);string_clear(&_5);
- }
- else{
- }
- person tempPerson;
- tempPerson.name = name;
- tempPerson.age = ageNum;
- personlist_add(&tempPerson, & personList );
- }
- else{
- }
- string_clear(&_4);
- }
- string_clear(&_3);
- for (double j = 0 ;j< 10000 ;j++){
- for (double i = 0 ;i< 10001 ;i++){
- person tempPerson;
- tempPerson.age = 5 ;
- string_handle _4 = string_new();
- standard_appendChars(&_4,"hej");tempPerson.name = string_copy(&_4);
- string_clear(&_4);
- personlist_add(&tempPerson, & personList );
- }
- personlist_clear( & personList );
- string_handle _4 = string_new();
- standard_appendChars(&_4,"woohoo");standard_printString(&_4);string_clear(&_4);
- printf("%d",personList.size);
- }
- double cancer = personList.size;
- string_handle _4 = string_new();
- standard_appendChars(&_4,"");standard_appendNum(&_4,cancer);standard_appendChars(&_4,"");standard_printString(&_4);string_clear(&_4);
- string_handle _6 = standard_read();string_clear(&_6);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement