添加并修改了一些函数,开始写栈。

This commit is contained in:
Saturneic 2018-06-11 15:00:08 +08:00
parent d3550aa26b
commit 510b191de5
5 changed files with 141 additions and 25 deletions

View File

@ -9,6 +9,7 @@
int main(int argc, char **argv){ int main(int argc, char **argv){
rand_init(); rand_init();
safeMode(1);
List *t_list = init_list(); List *t_list = init_list();
/*for(int i = 0; i < 9; i++){ /*for(int i = 0; i < 9; i++){
@ -25,20 +26,19 @@ int main(int argc, char **argv){
for(int i = 0; i < 12; i++){ for(int i = 0; i < 12; i++){
insertInHead(t_list, nodeWithInt(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); printListInfo(t_list,0);
printList(t_list); printList(t_list);
List *m_list;
m_list = m_findByInt(t_list, 5);
printList(m_list);
printf("\n"); printf("\n");
releaseList(t_list);
releaseAll();
return 0; return 0;
} }

View File

@ -44,6 +44,9 @@ int insertInTail(List *p_list, Node *p_node);
/*交换函数*/ /*交换函数*/
int exchangeLocation(Node *p_node,Node *t_node); int exchangeLocation(Node *p_node,Node *t_node);
/*复制函数*/
Node *copyNode(Node *);
/*移除函数*/ /*移除函数*/
int removeById(List *p_list, unsigned long id); int removeById(List *p_list, unsigned long id);
int removeByNode(List *p_list, Node *p_node); 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 releaseNode(Node *p_node);//释放Node
int isListEmpty(List *p_list);//判断List是否为空 int isListEmpty(List *p_list);//判断List是否为空
List *copyList(List *p_list);//复制链表
/*有关安全模式的变量*/ /*有关安全模式的变量*/
int if_safeMode = 0; int if_safeMode = 0;
@ -397,23 +401,23 @@ Node *findByValue(List *p_list, const char *type, const void *value){
} }
if(!strcmp(type,"int")){ if(!strcmp(type,"int")){
if(*((int *)p_node->value) == *((int *)value)){ if(*((int *)p_node->value) == *((int *)value)){
return p_node; return copyNode(p_node);
} }
} }
else if(!strcmp(type,"double")){ else if(!strcmp(type,"double")){
if(*((double *)p_node->value) == *((double *)value)){ if(*((double *)p_node->value) == *((double *)value)){
return p_node; return copyNode(p_node);
} }
} }
else if(!strcmp (type,"string")){ else if(!strcmp (type,"string")){
if(!strcmp((char *)p_node->value,(char *)value)) if(!strcmp((char *)p_node->value,(char *)value))
{ {
return p_node; return copyNode(p_node);
} }
} }
else if(!strcmp(type,"pointer")){ else if(!strcmp(type,"pointer")){
if(p_node->value == value){ 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(); List *f_list = init_list();
Node *p_node = p_list->head; Node *p_node = p_list->head;
while(p_node != NULL){ 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(!strcmp(type,"int")){
if(*((int *)p_node->value) == *((int *)value)){ if(*((int *)p_node->value) == *((int *)value)){
Node *f_node = init_node(); Node *f_node = copyNode(p_node);
initMallocValue(f_node,"pointer",(void *)p_node);
insertInTail(f_list,f_node); insertInTail(f_list,f_node);
} }
} }
else if(!strcmp(type,"double")){ else if(!strcmp(type,"double")){
if(*((double *)p_node->value) == *((double *)value)){ if(*((double *)p_node->value) == *((double *)value)){
Node *f_node = init_node(); Node *f_node = copyNode(p_node);
initMallocValue(f_node,"pointer",(void *)p_node); insertInTail(f_list,f_node);
insertInTail(f_list,f_node);
} }
} }
else if(!strcmp (type,"string")){ else if(!strcmp (type,"string")){
if(!strcmp((char *)p_node->value,(char *)value)) if(!strcmp((char *)p_node->value,(char *)value))
{ {
Node *f_node = init_node(); Node *f_node = copyNode(p_node);
initMallocValue(f_node,"pointer",(void *)p_node); insertInTail(f_list,f_node);
insertInTail(f_list,f_node);
} }
} }
else if(!strcmp(type,"pointer")){ else if(!strcmp(type,"pointer")){
if(p_node->value == value){ if(p_node->value == value){
Node *f_node = init_node(); Node *f_node = copyNode(p_node);
initMallocValue(f_node,"pointer",(void *)p_node); insertInTail(f_list,f_node);
insertInTail(f_list,f_node);
} }
} }
@ -484,4 +487,29 @@ int exchangeLocation(Node *p_node,Node *t_node){
return 0; 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 #endif

View File

@ -22,6 +22,11 @@ Node *findByDouble(List *, double);//依照特定类型查找一个节点
Node *findByString(List *, char *);//依照特定类型查找一个节点 Node *findByString(List *, char *);//依照特定类型查找一个节点
Node *findByPointer(List *, void *);//依照特定类型查找一个节点 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 printListInfo(List *p_list,int priority);//打印列表的详细信息
void printNodeInfo(Node *p_node,int priority);//打印节点的详细信息 void printNodeInfo(Node *p_node,int priority);//打印节点的详细信息
void printList(List *);//打印列表 void printList(List *);//打印列表
@ -237,7 +242,10 @@ Node *findByDouble(List *p_list, double target){
} }
Node *findByString(List *p_list, char *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; return t_node;
} }
@ -295,4 +303,33 @@ int addPointerForComplex(Node *p_node, void *temp){
return -1; 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 #endif

14
stack/stack.c Normal file
View File

@ -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;
}

37
stack/stack.h Normal file
View File

@ -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 */