添加了一些简便。并且添加了符合值节点的定义及一些功能。

This commit is contained in:
Saturneic 2018-06-10 17:21:11 +08:00
parent efc7bfe9b2
commit 684a842b7e
4 changed files with 374 additions and 117 deletions

View File

@ -5,28 +5,40 @@
* *******************************************************/
#include "list.h"
#include "list_expand.h"
int main(int argc, char **argv){
rand_init();
safeMode(1);
List *t_list = init_list();
for(int i = 0; i < 9; i++){
/*for(int i = 0; i < 9; i++){
Node *t_node = init_node();
int *t_i = (int *)malloc(sizeof(int));
*t_i = i;
init_value(t_node,"int",(void *)t_i);
initMallocValue(t_node,"int",(void *)t_i);
insertInTail(t_list,t_node);
}
}*/
/*Node *t_node = init_node();
insertInTail(t_list,t_node);
init_value(t_node,(void *)"there");*/
initMalllocValue(t_node,(void *)"there");*/
int *f_i = (int *)malloc(sizeof(int));
*f_i = 3;
//releaseMalloc();
releaseAll();
for(int i = 0; i < 12; i++){
insertInHead(t_list, nodeWithInt(i));
}
Node *t_node = nodeWithComplex();
addIntForComplex(t_node, 32);
addIntForComplex(t_node, 64);
insertInTail(t_list, t_node);
insertInTail(t_list, nodeWithDouble(32.5));
insertInTail(t_list, nodeWithString("There"));
printNodeInfo(findByString(t_list, "There"), 0);
printListInfo(t_list,0);
printList(t_list);
printf("\n");
releaseList(t_list);
return 0;
}

View File

@ -9,7 +9,7 @@
typedef struct Node{
unsigned long long id;//唯一标识符
void *value;
int if_setvalue;//记录是否已经初始化值
int if_malloc;//记录是否已经初始化值
const char *type;//记录值的类型
struct Node *next;
struct Node *last;
@ -24,37 +24,40 @@ typedef struct List{
}List;
int safeMode(int ifon);//安全模式确保显式声明过的内存都会被释放
int releaseMalloc(void);//释放所有声明过的内存
int releaseSingleList(List *p_list);
int releaseSingleNode(List *p_list);
int releaseSingleListForSafeMode(List *p_list);//释放list_list
int releaseSingleNodeForSafeMode(List *p_list);//释放node_list
int releaseAll(void);//安全模式最后调用的函数
List *init_list(void);
Node *init_node(void);
int init_value(Node *,const char *,void *);
void init_rand(void);
int initMallocValue(Node *,const char *,void *);//赋予已分配内存的值,并标明类型
/*有关id的函数*/
void init_rand(void);
unsigned long long getId(void);
/*插入函数*/
int insertInHead(List *p_list, Node *p_node);
int insertInTail(List *p_list, Node *p_node);
/*移除函数*/
int removeById(List *p_list, unsigned long id);
int removeByNode(List *p_list, Node *p_node);
int popFromHead(List *p_list);
int popFromTail(List *p_list);
unsigned long long len(List *p_list);
unsigned long long len(List *p_list);//查看链表的长度
Node *findById(List *p_list, const unsigned long long id);
Node *findByValue(List *p_list, const char *type, const void *value);
Node *findById(List *p_list, const unsigned long long id);//通过id查找目标链表中的匹配节点
Node *findByValue(List *p_list, const char *type, const void *value);//通过值来查找目标链表中的匹配节点
List *mply_findByValue(List *p_list, const char *type, const void *value);//寻找多个值匹配的节点
int releaseList(List *p_list);
int releaseNode(Node *p_node);
int releaseList(List *p_list);//释放List并可选择是否释放中所有的其中的Node
int releaseListForSingle(List *p_list);//单独释放List
int releaseNode(Node *p_node);//释放Node
int isListEmpty(List *p_list);
int isListEmpty(List *p_list);//判断List是否为空
/*有关安全模式的变量*/
int if_safeMode = 0;
@ -85,7 +88,7 @@ int safeMode(int ifon){
return ifon;
}
int releaseSingleList(List *p_list){
int releaseSingleListForSafeMode(List *p_list){
Node *p_node = p_list->head;
List *plv_node = NULL;
while(p_node != NULL){
@ -105,13 +108,13 @@ int releaseSingleList(List *p_list){
return 0;
}
int releaseSingleNode(List *p_list){
int releaseSingleNodeForSafeMode(List *p_list){
Node *p_node = p_list->head;
Node *pnv_node = NULL;
while(p_node != NULL){
pnv_node = (Node *)p_node->value;
pnv_node->id = 0;
pnv_node->if_setvalue = 0;
pnv_node->if_malloc = 0;
pnv_node->last = NULL;
pnv_node->next = NULL;
pnv_node->type = NULL;
@ -130,8 +133,8 @@ int releaseSingleNode(List *p_list){
int releaseAll(void){
if(if_safeMode == 1){
if_safeMode = 0;
releaseList(node_list);
releaseSingleList(list_list);
releaseSingleNodeForSafeMode(node_list);
releaseSingleListForSafeMode(list_list);
}
return 0;
}
@ -139,14 +142,14 @@ int releaseAll(void){
Node *init_node(void){
Node *p_node = (Node *) malloc(sizeof(Node));
p_node->id = getId();
p_node->if_setvalue = 0;
p_node->if_malloc = 0;
p_node->next = NULL;
p_node->last = NULL;
if(if_safeMode) {
if_safeMode = 0;
Node *prec_node = init_node();
if_safeMode = 1;
init_value(prec_node, "pointer", (void *)p_node);
initMallocValue(prec_node, "pointer", (void *)p_node);
insertInTail(node_list,prec_node);
}
return p_node;
@ -162,14 +165,14 @@ List *init_list(void){
if_safeMode = 0;
Node *p_node = init_node();
if_safeMode = 1;
init_value(p_node,"pointer",(void *)p_list);
initMallocValue(p_node,"pointer",(void *)p_list);
insertInTail(list_list,p_node);
}
return p_list;
}
int init_value(Node *p_node,const char *type,void *p_value){
p_node->if_setvalue = 1;
int initMallocValue(Node *p_node,const char *type,void *p_value){
p_node->if_malloc = 1;
p_node->type = type;
p_node->value = p_value;
return 0;
@ -223,8 +226,15 @@ int releaseNode(Node *p_node){
if(if_safeMode == 1){
removeByNode(node_list,p_node);
}
if(p_node->if_setvalue == 1){
if(p_node->if_malloc == 1){
if(strcmp(p_node->type,"pointer")) {
if(!strcmp(p_node->type, "list")){
releaseList((List *)p_node->value);
}
else{
free(p_node->value);
}
}
p_node->value = NULL;
}
p_node->last = NULL;
@ -232,13 +242,12 @@ int releaseNode(Node *p_node){
p_node->type = NULL;
p_node->value = NULL;
p_node->id = 0;
p_node->if_setvalue = 0;
p_node->if_malloc = 0;
free(p_node);
return 0;
}
int releaseList(List *p_list){
Node *p_node, *pl_node;
p_node = p_list->head;
@ -261,6 +270,15 @@ int releaseList(List *p_list){
return 0;
}
int releaseListForSingle(List *p_list){
p_list->head = NULL;
p_list->tail = NULL;
p_list->id = 0;
p_list->length = 0;
free(p_list);
return 0;
}
unsigned long long len(List *p_list){
return p_list->length;
}
@ -273,7 +291,6 @@ int removeById(List *p_list, unsigned long id){
if(tmp->id == id) {
tmp->last->next = tmp->next;
tmp->next->last = tmp->last;
//releaseNode(tmp); 在安全模式下不必要
p_list->length -= 1;
return 1;//found
}
@ -293,7 +310,6 @@ int removeByNode(List *p_list, Node *p_node){
if(tmp == p_node){
tmp->last->next = tmp->next;
tmp->next->last = tmp->last;
//releaseNode(tmp); 在安全模式下不必要
p_list->length -= 1;
return 1;//found
}
@ -372,7 +388,10 @@ Node *findById(List *p_list, const unsigned long long id){
Node *findByValue(List *p_list, const char *type, const void *value){
Node *p_node = p_list->head;
while(p_node != NULL){
if(strcmp(p_node->type,type)) continue;//continue when type is not the same.
if(strcmp(p_node->type,type)){
p_node = p_node->next;
continue;//跳过不合类型的节点
}
if(!strcmp(type,"int")){
if(*((int *)p_node->value) == *((int *)value)){
return p_node;
@ -409,14 +428,14 @@ List *mply_findByValue(List *p_list, const char *type, const void *value){
if(!strcmp(type,"int")){
if(*((int *)p_node->value) == *((int *)value)){
Node *f_node = init_node();
init_value(f_node,"pointer",(void *)p_node);
initMallocValue(f_node,"pointer",(void *)p_node);
insertInTail(f_list,f_node);
}
}
else if(!strcmp(type,"double")){
if(*((double *)p_node->value) == *((double *)value)){
Node *f_node = init_node();
init_value(f_node,"pointer",(void *)p_node);
initMallocValue(f_node,"pointer",(void *)p_node);
insertInTail(f_list,f_node);
}
}
@ -424,14 +443,14 @@ List *mply_findByValue(List *p_list, const char *type, const void *value){
if(!strcmp((char *)p_node->value,(char *)value))
{
Node *f_node = init_node();
init_value(f_node,"pointer",(void *)p_node);
initMallocValue(f_node,"pointer",(void *)p_node);
insertInTail(f_list,f_node);
}
}
else if(!strcmp(type,"pointer")){
if(p_node->value == value){
Node *f_node = init_node();
init_value(f_node,"pointer",(void *)p_node);
initMallocValue(f_node,"pointer",(void *)p_node);
insertInTail(f_list,f_node);
}
}

View File

@ -1,72 +0,0 @@
#include "list.h"
#ifndef LIST_EASY_H
#define LIST_EASY_H
Node *new_nodeWithInt(int);
Node *new_nodeWithDouble(double);
Node *new_nodeWithString(const char *);
Node *new_nodeWithPointer(void *);
Node *find_nodeByIndex(List *, unsigned long long);
int list_through(List *p_list, int (*p_func)(void *, const char *));//Go through the list.Call p_func every time.
Node *new_nodeWithInt(int m_int){
int *p_int = (int *)malloc(sizeof(int));
*p_int = m_int;
Node *p_node = init_node();
init_value(p_node,"int",(void *)p_int);
return p_node;
}
Node *new_nodeWithDouble(double m_double){
double *p_double = (double *)malloc(sizeof(double));
*p_double = m_double;
Node *p_node = init_node();
init_value(p_node,"double",(void *)p_double);
return p_node;
}
Node *new_nodeWithString(const char *m_string){
char *p_string = (char *)malloc(sizeof(char)*(strlen(m_string)+1));
strcpy(p_string,m_string);
Node *p_node = init_node();
init_value(p_node,"string",(void *)p_string);
return p_node;
}
Node *new_nodeWithPointer(void *m_pointer){
Node *p_node = init_node();
init_value(p_node,"pointer",(void *)m_pointer);
return p_node;
}
Node *find_nodeByIndex(List *p_list, unsigned long long m_index){
Node *p_node = p_list->head;
for(unsigned long long i= 0; i < m_index; i++){
p_node = p_node->next;
}
return p_node;
}
int list_through(List *p_list, int (*p_func)(void *, const char *)){
Node *p_node = p_list->head;
while(p_node != NULL){
if(p_node->if_setvalue == 1){
int m_return = (*p_func)(p_node->value, p_node->type);
if (m_return == -1) break;
else if (m_return == 1){
p_node = p_node->last;
continue;
}
else{
}
}
p_node = p_node->next;
}
return 0;
}
#endif

298
list/list_expand.h Normal file
View File

@ -0,0 +1,298 @@
#include "list.h"
#include <stdio.h>
#ifndef LIST_EASY_H
#define LIST_EASY_H
Node *nodeWithInt(int);//快速初始化一个单一值节点并赋值
Node *nodeWithDouble(double);//快速初始化一个节单一值点并赋值
Node *nodeWithString(const char *);//快速初始化一个单一值节点并赋值
Node *nodeWithPointer(void *);//快速初始化一个单一值节点并赋值
Node *nodeWithComplex(void);//快速初始化一个复合值节点并赋值
int addValueForComplex(Node *, char *type, void *value);//为复合节点添加值
int addIntForComplex(Node *, int);//为复合节点添加一个特定类型的值
int addDoubleForComplex(Node *, double);//为复合节点添加一个特定类型的值
int addStringForComplex(Node *, char *);//为复合节点添加一个特定类型的值
int addPointerForComplex(Node *, void *);//为复合节点添加一个特定类型的值
Node *findByIndex(List *, unsigned long long);//根据位置查找一个节点
Node *findByInt(List *, int);//依照特定类型查找一个节点
Node *findByDouble(List *, double);//依照特定类型查找一个节点
Node *findByString(List *, char *);//依照特定类型查找一个节点
Node *findByPointer(List *, void *);//依照特定类型查找一个节点
void printListInfo(List *p_list,int priority);//打印列表的详细信息
void printNodeInfo(Node *p_node,int priority);//打印节点的详细信息
void printList(List *);//打印列表
void printNode(Node *p_node);//打印节点
int getByInt(Node *);//直接得到节点的值
double getByDouble(Node *);//直接得到节点的值
char *getByString(Node *);//直接得到节点的值
void *getByPointer(Node *);//直接得到节点的值
int listThrough(List *p_list, int (*p_func)(void *, const char *));//遍历链表并不断调用目标函数。目标函数将接受节点储存值的指针及其类型。
Node *nodeWithInt(int m_int){
int *p_int = (int *)malloc(sizeof(int));
*p_int = m_int;
Node *p_node = init_node();
initMallocValue(p_node,"int",(void *)p_int);
return p_node;
}
Node *nodeWithDouble(double m_double){
double *p_double = (double *)malloc(sizeof(double));
*p_double = m_double;
Node *p_node = init_node();
initMallocValue(p_node,"double",(void *)p_double);
return p_node;
}
Node *nodeWithString(const char *m_string){
char *p_string = (char *)malloc(sizeof(char)*(strlen(m_string)+1));
strcpy(p_string,m_string);
Node *p_node = init_node();
initMallocValue(p_node,"string",(void *)p_string);
return p_node;
}
Node *nodeWithPointer(void *m_pointer){
Node *p_node = init_node();
initMallocValue(p_node,"pointer",m_pointer);
return p_node;
}
Node *nodeWithComplex(void){
Node *p_node = init_node();
p_node->type = "list";
p_node->value = init_list();
p_node->if_malloc = 1;
return p_node;
}
Node *findByIndex(List *p_list, unsigned long long m_index){
Node *p_node = p_list->head;
for(unsigned long long i= 0; i < m_index; i++){
p_node = p_node->next;
}
return p_node;
}
int listThrough(List *p_list, int (*p_func)(void *, const char *)){
Node *p_node = p_list->head;
while(p_node != NULL){
if(p_node->if_malloc == 1){
int m_return = (*p_func)(p_node->value, p_node->type);
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 getByInt(Node *p_node){
if (strcmp(p_node->type, "int")) return *(int *)(p_node->value);
else return -1;
}
char *getByString(Node *p_node){
if (strcmp(p_node->type, "string")) return (char *)(p_node->value);
else return NULL;
}
double getByDouble(Node *p_node){
if (strcmp(p_node->type, "double")) return *(double *)(p_node->value);
else return -1;
}
void *getByPointer(Node *p_node){
if (strcmp(p_node->type, "pointer")) return (void *)(p_node->value);
else return NULL;
}
void printListInfo(List *p_list,int priority){
for(int i = 0; i < priority; i++) printf(" ");
printf("###LIST(location:%p, id:%llu){\n",p_list,p_list->id);
for(int i = 0; i < priority+1; i++) printf(" ");
printf("HEAD->%p / Tail->%p / Length:%llu\n",p_list->head,p_list->tail,p_list->length);
Node *p_node = p_list->head;
int i = 0;
while (p_node != NULL){
for(int i = 0; i < priority+1; i++) printf(" ");
printf("%d.... \n",i);
printNodeInfo(p_node, priority+1);
p_node = p_node->next;
i++;
}
for(int i = 0; i < priority; i++) printf(" ");
printf("}\n");
}
void printNodeInfo(Node *p_node,int priority){
for(int i = 0; i < priority; i++) printf(" ");
printf("#NODE(location:%p, id:%llu){\n",p_node,p_node->id);
for(int i = 0; i < priority+1; i++) printf(" ");
printf("NEXT->%p / LAST->%p / MALLOC:%d\n",p_node->next,p_node->last,p_node->if_malloc);
if(!strcmp(p_node->type, "int")){
for(int i = 0; i < priority+1; i++) printf(" ");
printf("VALUE(Int):%d\n",*(int *)(p_node->value));
}
else if(!strcmp(p_node->type, "double")){
for(int i = 0; i < priority+1; i++) printf(" ");
printf("VALUE(Double):%a\n",*(double *)(p_node->value));
}
else if(!strcmp(p_node->type, "string")){
for(int i = 0; i < priority+1; i++) printf(" ");
printf("VALUE(String):%s\n",(char *)(p_node->value));
}
else if(!strcmp(p_node->type, "pointer")){
for(int i = 0; i < priority+1; i++) printf(" ");
printf("VALUE(Pointer):%s\n",(char *)(p_node->value));
}
else if(!strcmp(p_node->type, "list")){
for(int i = 0; i < priority+1; i++) printf(" ");
printf("VALUE(List):\n");
printListInfo((List *)p_node->value,priority+2);
}
for(int i = 0; i < priority; i++) printf(" ");
printf("}\n");
}
void printNode(Node *p_node){
printf("#NODE(location:%p, id:%llu){\n",p_node,p_node->id);
printf(" ");
printf("NEXT->%p / LAST->%p / MALLOC:%d\n",p_node->next,p_node->last,p_node->if_malloc);
printf(" ");
if(!strcmp(p_node->type, "int")){
printf("%d",*(int *)(p_node->value));
}
else if(!strcmp(p_node->type, "double")){
printf("%a\n",*(double *)(p_node->value));
}
else if(!strcmp(p_node->type, "string")){
printf("%s\n",(char *)(p_node->value));
}
else if(!strcmp(p_node->type, "pointer")){
printf("%s\n",(char *)(p_node->value));
}
else if(!strcmp(p_node->type, "list")){
printList((List *)p_node->value);
}
printf("}\n");
}
void printList(List *p_list){
Node *p_node = p_list->head;
printf("[");
int if_nearLast = 0;
while (p_node != NULL) {
if(!if_nearLast && p_node->next == NULL) if_nearLast = 1;
if(!strcmp(p_node->type, "int")){
printf("%d",*(int *)(p_node->value));
}
else if(!strcmp(p_node->type, "double")){
printf("%a",*(double *)(p_node->value));
}
else if(!strcmp(p_node->type, "string")){
printf("%s",(char *)(p_node->value));
}
else if(!strcmp(p_node->type, "pointer")){
printf("%s",(char *)(p_node->value));
}
else if(!strcmp(p_node->type, "list")){
printList((List *)p_node->value);
}
if(!if_nearLast){
printf(", ");
}
p_node = p_node->next;
}
printf("]");
}
Node *findByInt(List *p_list, int target){
int *p_target = (int *)malloc(sizeof(int));
*p_target = target;
Node *t_node = findByValue(p_list, "int", p_target);
free(p_target);
return t_node;
}
Node *findByDouble(List *p_list, double target){
double *p_target = (double *)malloc(sizeof(double));
*p_target = target;
Node *t_node = findByValue(p_list, "double", p_target);
free(p_target);
return t_node;
}
Node *findByString(List *p_list, char *target){
Node *t_node = findByValue(p_list, "string", target);
return t_node;
}
Node *findByPointer(List *p_list, void *target){
Node *t_node = findByValue(p_list, "pointer", target);
return t_node;
}
int addValueForComplex(Node * p_node, char *type, void *value){
if(!strcmp(p_node->type ,"list")){
List *c_list = (List *)p_node->value;
Node *c_node = init_node();
initMallocValue(c_node, type, value);
insertInTail(c_list, c_node);
return 0;
}
return -1;
}
int addIntForComplex(Node *p_node, int temp){
if(!strcmp(p_node->type, "list")){
int *p_temp = (int *)malloc(sizeof(int));
*p_temp = temp;
addValueForComplex(p_node, "int", p_temp);
return 0;
}
return -1;
}
int addDoubleForComplex(Node *p_node, double temp){
if(!strcmp(p_node->type, "list")){
double *p_temp = (double *)malloc(sizeof(double));
*p_temp = temp;
addValueForComplex(p_node, "double", p_temp);
return 0;
}
return -1;
}
int addStringForComplex(Node *p_node, char *temp){
if(!strcmp(p_node->type, "list")){
char *p_temp = (char *)malloc(sizeof(strlen(temp)+1));
strcpy(p_temp, temp);
addValueForComplex(p_node, "string", p_temp);
return 0;
}
return -1;
}
int addPointerForComplex(Node *p_node, void *temp){
if(!strcmp(p_node->type, "list")){
addValueForComplex(p_node, "pointer", temp);
return 0;
}
return -1;
}
#endif