From 510b191de5fd63f320c62d078a802bab75c9250e Mon Sep 17 00:00:00 2001 From: Saturneic Date: Mon, 11 Jun 2018 15:00:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B9=B6=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BA=86=E4=B8=80=E4=BA=9B=E5=87=BD=E6=95=B0=EF=BC=8C=E5=BC=80?= =?UTF-8?q?=E5=A7=8B=E5=86=99=E6=A0=88=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- list/list.c | 16 ++++++------- list/list.h | 60 +++++++++++++++++++++++++++++++++------------- list/list_expand.h | 39 +++++++++++++++++++++++++++++- stack/stack.c | 14 +++++++++++ stack/stack.h | 37 ++++++++++++++++++++++++++++ 5 files changed, 141 insertions(+), 25 deletions(-) create mode 100644 stack/stack.c create mode 100644 stack/stack.h diff --git a/list/list.c b/list/list.c index 9bcf50f..91bb75c 100644 --- a/list/list.c +++ b/list/list.c @@ -9,6 +9,7 @@ int main(int argc, char **argv){ rand_init(); + safeMode(1); List *t_list = init_list(); /*for(int i = 0; i < 9; i++){ @@ -25,20 +26,19 @@ int main(int argc, char **argv){ for(int i = 0; i < 12; i++){ insertInHead(t_list, nodeWithInt(i)); + insertInTail(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); + List *m_list; + m_list = m_findByInt(t_list, 5); + printList(m_list); printf("\n"); - releaseList(t_list); + + releaseAll(); return 0; } diff --git a/list/list.h b/list/list.h index 180081a..3081af7 100644 --- a/list/list.h +++ b/list/list.h @@ -44,6 +44,9 @@ int insertInTail(List *p_list, Node *p_node); /*交换函数*/ int exchangeLocation(Node *p_node,Node *t_node); +/*复制函数*/ +Node *copyNode(Node *); + /*移除函数*/ int removeById(List *p_list, unsigned long id); int removeByNode(List *p_list, Node *p_node); @@ -61,6 +64,7 @@ int releaseListForSingle(List *p_list);//单独释放List int releaseNode(Node *p_node);//释放Node int isListEmpty(List *p_list);//判断List是否为空 +List *copyList(List *p_list);//复制链表 /*有关安全模式的变量*/ int if_safeMode = 0; @@ -397,23 +401,23 @@ Node *findByValue(List *p_list, const char *type, const void *value){ } if(!strcmp(type,"int")){ if(*((int *)p_node->value) == *((int *)value)){ - return p_node; + return copyNode(p_node); } } else if(!strcmp(type,"double")){ if(*((double *)p_node->value) == *((double *)value)){ - return p_node; + return copyNode(p_node); } } else if(!strcmp (type,"string")){ if(!strcmp((char *)p_node->value,(char *)value)) { - return p_node; + return copyNode(p_node); } } else if(!strcmp(type,"pointer")){ if(p_node->value == value){ - return p_node; + return copyNode(p_node); } } @@ -427,34 +431,33 @@ List *mply_findByValue(List *p_list, const char *type, const void *value){ List *f_list = init_list(); Node *p_node = p_list->head; while(p_node != NULL){ - if(strcmp(p_node->type,type)) continue; + if(strcmp(p_node->type,type)){ + p_node = p_node->next; + continue; + } if(!strcmp(type,"int")){ if(*((int *)p_node->value) == *((int *)value)){ - Node *f_node = init_node(); - initMallocValue(f_node,"pointer",(void *)p_node); + Node *f_node = copyNode(p_node); insertInTail(f_list,f_node); } } else if(!strcmp(type,"double")){ if(*((double *)p_node->value) == *((double *)value)){ - Node *f_node = init_node(); - initMallocValue(f_node,"pointer",(void *)p_node); - insertInTail(f_list,f_node); + Node *f_node = copyNode(p_node); + insertInTail(f_list,f_node); } } else if(!strcmp (type,"string")){ if(!strcmp((char *)p_node->value,(char *)value)) { - Node *f_node = init_node(); - initMallocValue(f_node,"pointer",(void *)p_node); - insertInTail(f_list,f_node); + Node *f_node = copyNode(p_node); + insertInTail(f_list,f_node); } } else if(!strcmp(type,"pointer")){ if(p_node->value == value){ - Node *f_node = init_node(); - initMallocValue(f_node,"pointer",(void *)p_node); - insertInTail(f_list,f_node); + Node *f_node = copyNode(p_node); + insertInTail(f_list,f_node); } } @@ -484,4 +487,29 @@ int exchangeLocation(Node *p_node,Node *t_node){ return 0; } +Node *copyNode(Node *p_node){ + Node *t_node = init_node(); + t_node->id = p_node->id; + t_node->last = p_node->last; + t_node->next = p_node->next; + t_node->if_malloc = p_node->if_malloc; + t_node->type = p_node->type; + t_node->value = p_node->value; + return t_node; +} + +List *copyList(List *p_list){ + List *t_list = init_list(); + t_list->head = p_list->head; + t_list->tail = p_list->tail; + t_list->id = p_list->id; + Node *p_node = p_list->head; + while(p_node != NULL){ + Node *t_node = copyNode(p_node); + insertInTail(t_list, t_node); + p_node = p_node->next; + } + return t_list; +} + #endif diff --git a/list/list_expand.h b/list/list_expand.h index d75c22d..388756d 100644 --- a/list/list_expand.h +++ b/list/list_expand.h @@ -22,6 +22,11 @@ Node *findByDouble(List *, double);//依照特定类型查找一个节点 Node *findByString(List *, char *);//依照特定类型查找一个节点 Node *findByPointer(List *, void *);//依照特定类型查找一个节点 +List *m_findByInt(List*, int);//根据位置查找所有匹配的节点 +List *m_findByDouble(List*, double);//根据位置查找所有匹配的节点 +List *m_findByString(List*, char *);//根据位置查找所有匹配的节点 +List *m_findByPointer(List*, void *);//根据位置查找所有匹配的节点 + void printListInfo(List *p_list,int priority);//打印列表的详细信息 void printNodeInfo(Node *p_node,int priority);//打印节点的详细信息 void printList(List *);//打印列表 @@ -237,7 +242,10 @@ Node *findByDouble(List *p_list, double target){ } Node *findByString(List *p_list, char *target){ - Node *t_node = findByValue(p_list, "string", target); + char *p_temp = (char *)malloc(sizeof(char)*(strlen(target)+1)); + strcpy(p_temp, target); + Node *t_node = findByValue(p_list, "string", p_temp); + free(p_temp); return t_node; } @@ -295,4 +303,33 @@ int addPointerForComplex(Node *p_node, void *temp){ return -1; } +List *m_findByInt(List* p_list, int temp){ + int *p_temp = (int *)malloc(sizeof(int)); + *p_temp = temp; + List *t_list = mply_findByValue(p_list, "int", (void *)p_temp); + free(p_temp); + return t_list; +} + +List *m_findByDouble(List* p_list, double temp){ + double *p_temp = (double *)malloc(sizeof(double)); + *p_temp = temp; + List *t_list = mply_findByValue(p_list, "double", (void *)p_temp); + free(p_temp); + return t_list; +} + +List *m_findByString(List* p_list, char *temp){ + char *p_temp = (char *)malloc(sizeof(char)*(strlen(temp)+1)); + strcpy(p_temp, temp); + List *t_list = mply_findByValue(p_list, "string", (void *)p_temp); + free(p_temp); + return t_list; +} + +List *m_findByPointer(List* p_list, void *temp){ + List *t_list = mply_findByValue(p_list, "double", (void *)temp); + return t_list; +} + #endif diff --git a/stack/stack.c b/stack/stack.c new file mode 100644 index 0000000..347880c --- /dev/null +++ b/stack/stack.c @@ -0,0 +1,14 @@ +// +// stack.c +// ZE-Standard-Libraries +// +// Created by 胡一兵 on 2018/6/11. +// Copyright © 2018年 ZE. All rights reserved. +// + +#include "stack.h" + +int main(int argc, char **argv){ + + return 0; +} diff --git a/stack/stack.h b/stack/stack.h new file mode 100644 index 0000000..83b4e4e --- /dev/null +++ b/stack/stack.h @@ -0,0 +1,37 @@ +// +// stack.h +// ZE-Standard-Libraries +// +// Created by 胡一兵 on 2018/6/11. +// Copyright © 2018年 ZE. All rights reserved. +// + +#ifndef stack_h +#define stack_h + +#include "list.h" + +typedef struct stack_node{ + unsigned long long id; + int if_malloc; + void *value; + struct stack_node *next; +} SNode; + +typedef struct stack{ + unsigned long long id; + unsigned long long length; + SNode *top; +} Stack; + +Stack *initStack(void); +SNode *initSNode(void); + +SNode *popStack(Stack *p_stack); +int pushStack(Stack *p_stack, SNode *p_snode); + +int releaseStack(Stack *p_stack); +int releaseSNode(SNode *p_snode); + + +#endif /* stack_h */