2018-06-12 11:07:12 +00:00
|
|
|
#include "list_expand.h"
|
|
|
|
|
|
|
|
Node *nodeWithInt(int m_int) {
|
2018-07-18 03:53:09 +00:00
|
|
|
Node *p_node;
|
2018-06-12 11:07:12 +00:00
|
|
|
int *p_int = (int *)malloc(sizeof(int));
|
|
|
|
*p_int = m_int;
|
2018-07-18 03:53:09 +00:00
|
|
|
p_node = initNode();
|
2018-07-23 04:59:16 +00:00
|
|
|
initMallocValueForNode(p_node, INT, (void *)p_int);
|
2018-06-12 11:07:12 +00:00
|
|
|
return p_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *nodeWithDouble(double m_double) {
|
2018-07-18 03:53:09 +00:00
|
|
|
Node *p_node;
|
2018-06-12 11:07:12 +00:00
|
|
|
double *p_double = (double *)malloc(sizeof(double));
|
|
|
|
*p_double = m_double;
|
2018-07-18 03:53:09 +00:00
|
|
|
p_node = initNode();
|
2018-07-23 04:59:16 +00:00
|
|
|
initMallocValueForNode(p_node, DOUBLE, (void *)p_double);
|
2018-06-12 11:07:12 +00:00
|
|
|
return p_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *nodeWithString(const char *m_string) {
|
2018-07-18 03:53:09 +00:00
|
|
|
Node *p_node;
|
2018-06-12 11:07:12 +00:00
|
|
|
char *p_string = (char *)malloc(sizeof(char)*(strlen(m_string) + 1));
|
2018-07-23 04:59:16 +00:00
|
|
|
strcpy(p_string, m_string);
|
2018-07-18 03:53:09 +00:00
|
|
|
p_node = initNode();
|
2018-07-23 04:59:16 +00:00
|
|
|
initMallocValueForNode(p_node, STRING, (void *)p_string);
|
2018-06-12 11:07:12 +00:00
|
|
|
return p_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *nodeWithPointer(void *m_pointer) {
|
|
|
|
Node *p_node = initNode();
|
2018-07-23 04:59:16 +00:00
|
|
|
initMallocValueForNode(p_node, POINTER, m_pointer);
|
2018-06-12 11:07:12 +00:00
|
|
|
return p_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *nodeWithComplex(void) {
|
|
|
|
Node *p_node = initNode();
|
2018-07-23 04:59:16 +00:00
|
|
|
p_node->type = LIST;
|
2018-06-12 11:07:12 +00:00
|
|
|
p_node->value = initList();
|
|
|
|
p_node->if_malloc = 1;
|
|
|
|
return p_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *findByIndexForNode(List *p_list, unsigned long long m_index) {
|
|
|
|
Node *p_node = p_list->head;
|
2018-07-18 03:53:09 +00:00
|
|
|
unsigned long long i;
|
|
|
|
for (i = 0; i < m_index; i++) {
|
2018-06-12 11:07:12 +00:00
|
|
|
p_node = p_node->next;
|
|
|
|
}
|
|
|
|
return p_node;
|
|
|
|
}
|
|
|
|
|
2018-07-30 09:45:33 +00:00
|
|
|
int listThrough(List *p_list, int(*p_func)(unsigned int, void *)) {
|
2018-06-12 11:07:12 +00:00
|
|
|
Node *p_node = p_list->head;
|
|
|
|
while (p_node != NULL) {
|
|
|
|
if (p_node->if_malloc == 1) {
|
|
|
|
int m_return = (*p_func)(p_node->type, p_node->value);
|
|
|
|
if (m_return == -1) break;
|
|
|
|
else if (m_return == 1) {
|
|
|
|
p_node = p_node->last;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p_node = p_node->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getByIntForNode(Node *p_node) {
|
2018-07-23 04:59:16 +00:00
|
|
|
if (p_node->type == INT) return *(int *)(p_node->value);
|
2018-06-12 11:07:12 +00:00
|
|
|
else return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *getByStringForNode(Node *p_node) {
|
2018-07-23 04:59:16 +00:00
|
|
|
if (p_node->type == STRING) return (char *)(p_node->value);
|
2018-06-12 11:07:12 +00:00
|
|
|
else return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
double getByDoubleForNode(Node *p_node) {
|
2018-07-23 04:59:16 +00:00
|
|
|
if (p_node->type == DOUBLE) return *(double *)(p_node->value);
|
2018-06-12 11:07:12 +00:00
|
|
|
else return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *getByPointerForNode(Node *p_node) {
|
2018-07-23 04:59:16 +00:00
|
|
|
if (p_node->type == POINTER) return (void *)(p_node->value);
|
2018-06-12 11:07:12 +00:00
|
|
|
else return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void printListInfo(List *p_list, int priority) {
|
2018-07-18 03:53:09 +00:00
|
|
|
int i = 0;
|
|
|
|
Node *p_node;
|
|
|
|
for (i = 0; i < priority; i++) printf(" ");
|
2018-07-30 10:18:58 +00:00
|
|
|
printf("###LIST(location:%p, id:%s){\n", p_list, s_idToASCIIString(p_list->s_id));
|
2018-07-18 03:53:09 +00:00
|
|
|
for (i = 0; i < priority + 1; i++) printf(" ");
|
2018-06-12 11:07:12 +00:00
|
|
|
printf("HEAD->%p / Tail->%p / Length:%llu\n", p_list->head, p_list->tail, p_list->length);
|
2018-07-18 03:53:09 +00:00
|
|
|
p_node = p_list->head;
|
2018-06-12 11:07:12 +00:00
|
|
|
while (p_node != NULL) {
|
2018-07-18 03:53:09 +00:00
|
|
|
for (i = 0; i < priority + 1; i++) printf(" ");
|
2018-06-12 11:07:12 +00:00
|
|
|
printf("%d.... \n", i);
|
|
|
|
printNodeInfo(p_node, priority + 1);
|
|
|
|
p_node = p_node->next;
|
|
|
|
i++;
|
|
|
|
}
|
2018-07-18 03:53:09 +00:00
|
|
|
for (i = 0; i < priority; i++) printf(" ");
|
2018-06-12 11:07:12 +00:00
|
|
|
printf("}\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void printList(List *p_list) {
|
2018-07-18 03:53:09 +00:00
|
|
|
int if_nearLast = 0;
|
2018-06-12 11:07:12 +00:00
|
|
|
Node *p_node = p_list->head;
|
|
|
|
printf("[");
|
|
|
|
while (p_node != NULL) {
|
|
|
|
if (!if_nearLast && p_node->next == NULL) if_nearLast = 1;
|
2018-07-23 04:59:16 +00:00
|
|
|
if (p_node->type == INT) {
|
2018-06-12 11:07:12 +00:00
|
|
|
printf("%d", *(int *)(p_node->value));
|
|
|
|
}
|
2018-07-23 04:59:16 +00:00
|
|
|
else if (p_node->type == DOUBLE) {
|
2018-06-12 11:07:12 +00:00
|
|
|
printf("%a", *(double *)(p_node->value));
|
|
|
|
}
|
2018-07-23 04:59:16 +00:00
|
|
|
else if (p_node->type == STRING) {
|
2018-06-12 11:07:12 +00:00
|
|
|
printf("%s", (char *)(p_node->value));
|
|
|
|
}
|
2018-07-23 04:59:16 +00:00
|
|
|
else if (p_node->type == POINTER) {
|
2018-06-13 08:04:51 +00:00
|
|
|
printf("%p", (char *)(p_node->value));
|
2018-06-12 11:07:12 +00:00
|
|
|
}
|
2018-07-23 04:59:16 +00:00
|
|
|
else if (p_node->type == LIST) {
|
2018-06-12 11:07:12 +00:00
|
|
|
printList((List *)p_node->value);
|
|
|
|
}
|
|
|
|
if (!if_nearLast) {
|
|
|
|
printf(", ");
|
|
|
|
}
|
|
|
|
p_node = p_node->next;
|
|
|
|
}
|
|
|
|
printf("]");
|
|
|
|
}
|
|
|
|
|
|
|
|
void printNodeInfo(Node *p_node, int priority) {
|
2018-07-18 03:53:09 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < priority; i++) printf(" ");
|
2018-07-30 10:18:58 +00:00
|
|
|
printf("#NODE(location:%p, id:%s){\n", p_node, s_idToASCIIString(p_node->s_id));
|
2018-07-18 03:53:09 +00:00
|
|
|
for (i = 0; i < priority + 1; i++) printf(" ");
|
2018-06-12 11:07:12 +00:00
|
|
|
printf("NEXT->%p / LAST->%p / MALLOC:%d\n", p_node->next, p_node->last, p_node->if_malloc);
|
2018-07-23 04:59:16 +00:00
|
|
|
if (p_node->type == INT) {
|
2018-07-18 03:53:09 +00:00
|
|
|
for (i = 0; i < priority + 1; i++) printf(" ");
|
2018-06-13 08:15:25 +00:00
|
|
|
printf("VALUE(int):%d\n", *(int *)(p_node->value));
|
2018-06-12 11:07:12 +00:00
|
|
|
}
|
2018-07-23 04:59:16 +00:00
|
|
|
else if (p_node->type == DOUBLE) {
|
2018-07-18 03:53:09 +00:00
|
|
|
for (i = 0; i < priority + 1; i++) printf(" ");
|
2018-06-13 08:15:25 +00:00
|
|
|
printf("VALUE(double):%a\n", *(double *)(p_node->value));
|
2018-06-12 11:07:12 +00:00
|
|
|
}
|
2018-07-23 04:59:16 +00:00
|
|
|
else if (p_node->type == STRING) {
|
2018-07-18 03:53:09 +00:00
|
|
|
for (i = 0; i < priority + 1; i++) printf(" ");
|
2018-06-13 08:15:25 +00:00
|
|
|
printf("VALUE(string):%s\n", (char *)(p_node->value));
|
2018-06-12 11:07:12 +00:00
|
|
|
}
|
2018-07-23 04:59:16 +00:00
|
|
|
else if (p_node->type == POINTER) {
|
2018-07-18 03:53:09 +00:00
|
|
|
for (i = 0; i < priority + 1; i++) printf(" ");
|
2018-06-13 08:15:25 +00:00
|
|
|
printf("VALUE(pointer):%s\n", (char *)(p_node->value));
|
2018-06-12 11:07:12 +00:00
|
|
|
}
|
2018-07-23 04:59:16 +00:00
|
|
|
else if (p_node->type == LIST) {
|
2018-07-18 03:53:09 +00:00
|
|
|
for (i = 0; i < priority + 1; i++) printf(" ");
|
2018-06-12 11:07:12 +00:00
|
|
|
printf("VALUE(List):\n");
|
|
|
|
printListInfo((List *)p_node->value, priority + 2);
|
|
|
|
}
|
2018-07-18 03:53:09 +00:00
|
|
|
for (i = 0; i < priority; i++) printf(" ");
|
2018-06-12 11:07:12 +00:00
|
|
|
printf("}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void printNode(Node *p_node) {
|
2018-07-18 03:53:09 +00:00
|
|
|
int i;
|
2018-07-30 10:18:58 +00:00
|
|
|
printf("#NODE(location:%p, id:%s){\n", p_node, s_idToASCIIString(p_node->s_id));
|
2018-06-12 11:07:12 +00:00
|
|
|
printf(" ");
|
2018-07-23 04:59:16 +00:00
|
|
|
printf("NEXT->%p / LAST->%p\n", p_node->next, p_node->last);
|
2018-07-18 03:53:09 +00:00
|
|
|
for (i = 0; i < 1; i++) printf(" ");
|
2018-06-13 08:15:25 +00:00
|
|
|
printf("ifMalloc: ");
|
|
|
|
if (p_node->if_malloc) {
|
|
|
|
printf("YES\n");
|
2018-07-18 03:53:09 +00:00
|
|
|
for (i = 0; i < 1; i++) printf(" ");
|
2018-07-23 04:59:16 +00:00
|
|
|
printf("Value(type: %d): ", p_node->type);
|
|
|
|
if (p_node->type == INT) {
|
2018-06-13 08:15:25 +00:00
|
|
|
printf("%d", *(int *)(p_node->value));
|
|
|
|
}
|
2018-07-23 04:59:16 +00:00
|
|
|
else if (p_node->type == DOUBLE) {
|
2018-06-13 08:15:25 +00:00
|
|
|
printf("%a\n", *(double *)(p_node->value));
|
|
|
|
}
|
2018-07-23 04:59:16 +00:00
|
|
|
else if (p_node->type == STRING) {
|
2018-06-13 08:15:25 +00:00
|
|
|
printf("%s\n", (char *)(p_node->value));
|
|
|
|
}
|
2018-07-23 04:59:16 +00:00
|
|
|
else if (p_node->type == POINTER) {
|
|
|
|
printf("%p\n", (char *)(p_node->value));
|
2018-06-13 08:15:25 +00:00
|
|
|
}
|
2018-07-23 04:59:16 +00:00
|
|
|
else if (p_node->type == LIST) {
|
2018-06-13 08:15:25 +00:00
|
|
|
printList((List *)p_node->value);
|
|
|
|
}
|
2018-06-12 11:07:12 +00:00
|
|
|
}
|
2018-06-13 08:15:25 +00:00
|
|
|
else printf("NO\n");
|
|
|
|
|
2018-06-12 11:07:12 +00:00
|
|
|
printf("}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Node *findByIntForNode(List *p_list, int target) {
|
2018-07-18 03:53:09 +00:00
|
|
|
Node *t_node;
|
2018-06-12 11:07:12 +00:00
|
|
|
int *p_target = (int *)malloc(sizeof(int));
|
|
|
|
*p_target = target;
|
2018-07-23 04:59:16 +00:00
|
|
|
t_node = findByValue(p_list, INT, p_target);
|
2018-06-12 11:07:12 +00:00
|
|
|
free(p_target);
|
|
|
|
return t_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *findByDoubleForNode(List *p_list, double target) {
|
2018-07-18 03:53:09 +00:00
|
|
|
Node *t_node;
|
2018-06-12 11:07:12 +00:00
|
|
|
double *p_target = (double *)malloc(sizeof(double));
|
|
|
|
*p_target = target;
|
2018-07-23 04:59:16 +00:00
|
|
|
t_node = findByValue(p_list, DOUBLE, p_target);
|
2018-06-12 11:07:12 +00:00
|
|
|
free(p_target);
|
|
|
|
return t_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *findByStringForNode(List *p_list, char *target) {
|
2018-07-18 03:53:09 +00:00
|
|
|
Node *t_node;
|
2018-06-12 11:07:12 +00:00
|
|
|
char *p_temp = (char *)malloc(sizeof(char)*(strlen(target) + 1));
|
2018-07-23 04:59:16 +00:00
|
|
|
strcpy(p_temp, target);
|
|
|
|
t_node = findByValue(p_list, STRING, p_temp);
|
2018-06-12 11:07:12 +00:00
|
|
|
free(p_temp);
|
|
|
|
return t_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *findByPointerForNode(List *p_list, void *target) {
|
2018-07-23 04:59:16 +00:00
|
|
|
Node *t_node = findByValue(p_list, POINTER, target);
|
2018-06-12 11:07:12 +00:00
|
|
|
return t_node;
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:59:16 +00:00
|
|
|
int addValueForComplex(Node * p_node, int type, void *value) {
|
2018-07-18 03:53:09 +00:00
|
|
|
List *c_list;
|
|
|
|
Node *c_node;
|
2018-07-23 04:59:16 +00:00
|
|
|
if (p_node->type == LIST) {
|
2018-07-18 03:53:09 +00:00
|
|
|
c_list = (List *)p_node->value;
|
|
|
|
c_node = initNode();
|
2018-06-12 11:07:12 +00:00
|
|
|
initMallocValueForNode(c_node, type, value);
|
|
|
|
insertInTail(c_list, c_node);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int addIntForComplex(Node *p_node, int temp) {
|
2018-07-23 04:59:16 +00:00
|
|
|
if (p_node->type == LIST) {
|
2018-06-12 11:07:12 +00:00
|
|
|
int *p_temp = (int *)malloc(sizeof(int));
|
|
|
|
*p_temp = temp;
|
2018-07-23 04:59:16 +00:00
|
|
|
addValueForComplex(p_node, INT, p_temp);
|
2018-06-12 11:07:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int addDoubleForComplex(Node *p_node, double temp) {
|
2018-07-23 04:59:16 +00:00
|
|
|
if (p_node->type == LIST) {
|
2018-06-12 11:07:12 +00:00
|
|
|
double *p_temp = (double *)malloc(sizeof(double));
|
|
|
|
*p_temp = temp;
|
2018-07-23 04:59:16 +00:00
|
|
|
addValueForComplex(p_node, DOUBLE, p_temp);
|
2018-06-12 11:07:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int addStringForComplex(Node *p_node, char *temp) {
|
2018-07-23 04:59:16 +00:00
|
|
|
if (p_node->type == LIST) {
|
2018-06-12 11:07:12 +00:00
|
|
|
char *p_temp = (char *)malloc(sizeof(strlen(temp) + 1));
|
2018-07-23 04:59:16 +00:00
|
|
|
strcpy(p_temp, temp);
|
|
|
|
addValueForComplex(p_node, STRING, p_temp);
|
2018-06-12 11:07:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int addPointerForComplex(Node *p_node, void *temp) {
|
2018-07-23 04:59:16 +00:00
|
|
|
if (p_node->type == LIST) {
|
|
|
|
addValueForComplex(p_node, POINTER, temp);
|
2018-06-12 11:07:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
List *m_findByInt(List* p_list, int temp) {
|
|
|
|
int *p_temp = (int *)malloc(sizeof(int));
|
2018-07-18 03:53:09 +00:00
|
|
|
List *t_list;
|
2018-06-12 11:07:12 +00:00
|
|
|
*p_temp = temp;
|
2018-07-23 04:59:16 +00:00
|
|
|
t_list = mply_findByValue(p_list, INT, (void *)p_temp);
|
2018-06-12 11:07:12 +00:00
|
|
|
free(p_temp);
|
|
|
|
return t_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
List *m_findByDouble(List* p_list, double temp) {
|
2018-07-18 03:53:09 +00:00
|
|
|
List *t_list;
|
2018-06-12 11:07:12 +00:00
|
|
|
double *p_temp = (double *)malloc(sizeof(double));
|
|
|
|
*p_temp = temp;
|
2018-07-23 04:59:16 +00:00
|
|
|
t_list = mply_findByValue(p_list, DOUBLE, (void *)p_temp);
|
2018-06-12 11:07:12 +00:00
|
|
|
free(p_temp);
|
|
|
|
return t_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
List *m_findByString(List* p_list, char *temp) {
|
2018-07-18 03:53:09 +00:00
|
|
|
List *t_list;
|
2018-06-12 11:07:12 +00:00
|
|
|
char *p_temp = (char *)malloc(sizeof(char)*(strlen(temp) + 1));
|
2018-07-23 04:59:16 +00:00
|
|
|
strcpy(p_temp, temp);
|
|
|
|
t_list = mply_findByValue(p_list, STRING, (void *)p_temp);
|
2018-06-12 11:07:12 +00:00
|
|
|
free(p_temp);
|
|
|
|
return t_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
List *m_findByPointer(List* p_list, void *temp) {
|
2018-07-23 04:59:16 +00:00
|
|
|
List *t_list = mply_findByValue(p_list, DOUBLE, (void *)temp);
|
2018-06-12 11:07:12 +00:00
|
|
|
return t_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long getIndexByNode(List *p_list, Node *p_node) {
|
|
|
|
Node *t_node = p_list->head;
|
|
|
|
unsigned long long index = 0;
|
|
|
|
while (t_node != NULL) {
|
2018-07-30 10:18:58 +00:00
|
|
|
if (p_node->s_id == t_node->s_id) return index;
|
2018-06-12 11:07:12 +00:00
|
|
|
index++;
|
|
|
|
t_node = t_node->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
List *m_findByIntForNode(List* p_list, int temp) {
|
|
|
|
int *p_temp = (int *)malloc(sizeof(int));
|
|
|
|
*p_temp = temp;
|
2018-07-23 04:59:16 +00:00
|
|
|
return mply_findByValue(p_list, INT, (void *)p_temp);
|
2018-06-12 11:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
List *m_findByDoubleForNode(List* p_list, double temp) {
|
|
|
|
double *p_temp = (double *)malloc(sizeof(double));
|
|
|
|
*p_temp = temp;
|
2018-07-23 04:59:16 +00:00
|
|
|
return mply_findByValue(p_list, DOUBLE, (void *)p_temp);
|
2018-06-12 11:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
List *m_findByStringForNode(List* p_list, char *temp) {
|
|
|
|
char *p_temp = (char *)malloc(sizeof(char) * (strlen(temp) + 1));
|
2018-07-23 04:59:16 +00:00
|
|
|
strcpy(p_temp, temp);
|
|
|
|
return mply_findByValue(p_list, STRING, (void *)p_temp);
|
2018-06-12 11:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
List *m_findByPointerForNode(List* p_list, void *temp) {
|
2018-07-23 04:59:16 +00:00
|
|
|
return mply_findByValue(p_list, POINTER, (void *)temp);
|
2018-07-23 05:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long calListMemory(List * p_list){
|
|
|
|
Node *p_node = p_list->head;
|
|
|
|
unsigned long long nodes_size = 0LL;
|
|
|
|
unsigned long long list_size = sizeof(p_list);
|
|
|
|
while(p_node != NULL){
|
2018-08-03 10:35:10 +00:00
|
|
|
nodes_size += sizeof(p_node);
|
2018-07-23 05:47:31 +00:00
|
|
|
p_node = p_node->next;
|
|
|
|
}
|
|
|
|
return list_size + nodes_size;
|
2018-06-12 11:07:12 +00:00
|
|
|
}
|