标签:bsp size 技术 closed col one false 链表操作 max
单链表实现栈结构->使用环状双链表重构,增加了对链表操作的灵活程度。我相信只要有一点能够优化就值得去做。
“栈”的单链表实现
1 typedef int stack_element; 2 typedef struct stack_node { 3 stack_element element; 4 struct stack_node *next; 5 }node; 6 typedef struct { 7 node *top, *bottom; 8 }stack;
1 /* 2 * 函数介绍: 初始化栈 3 * 参数: 4 * 返回值: 5 * 备注: 6 */ 7 void initStack (stack **stackPointer) 8 { 9 if(*stackPointer) *stackPointer = NULL; 10 *stackPointer = (stack *)malloc(sizeof(stack)); 11 if(!(*stackPointer)) { 12 perror("malloc"); 13 return; 14 } 15 node *tmp = (node *)malloc(sizeof(node)); 16 if(!tmp) { 17 perror("malloc"); 18 return; 19 } 20 tmp->element = -1; 21 tmp->next = NULL; 22 (*stackPointer)->top = tmp; 23 (*stackPointer)->bottom = tmp; 24 } 25 /* 26 * 函数介绍: 销毁栈 27 * 参数: 28 * 返回值: 29 * 备注: 30 */ 31 void destroyStack (stack **stackPointer) 32 { 33 if(!*stackPointer) return; 34 while((*stackPointer)->top) { 35 (*stackPointer)->bottom = (*stackPointer)->top; 36 (*stackPointer)->top = (*stackPointer)->top->next; 37 free((*stackPointer)->bottom); 38 (*stackPointer)->bottom = NULL; 39 } 40 free(*stackPointer); 41 *stackPointer = NULL; 42 } 43 /* 44 * 函数介绍: 进栈 45 * 参数: 46 * 返回值: 47 * 备注: 48 */ 49 void pushStack(stack *stackPointer, stack_element elem) 50 { 51 node *tmp = NULL; 52 if(stackPointer == NULL) return; 53 if(stackPointer->bottom->next) { 54 tmp = stackPointer->bottom->next; 55 stackPointer->bottom->next = tmp->next; 56 } else { 57 tmp = (node *)malloc(sizeof(node)); 58 if(!tmp) { 59 perror("malloc"); 60 return; 61 } 62 } 63 tmp->element = elem; 64 tmp->next = stackPointer->top; 65 stackPointer->top = tmp; 66 } 67 /* 68 * 函数介绍: 出栈 69 * 参数: 70 * 返回值: 71 * 备注: 72 */ 73 stack_element popStack(stack *stackPointer) 74 { 75 if(stackPointer == NULL) return 1; 76 if(stackPointer->top == stackPointer->bottom) return 1; 77 node *tmp = stackPointer->top; 78 stackPointer->top = tmp->next; 79 tmp->next = stackPointer->bottom->next; 80 stackPointer->bottom->next = tmp; 81 return tmp->element; 82 } 83 /* 84 * 函数介绍: 查找元素是否在栈中 85 * 参数: 86 * 返回值: 87 * 备注: 88 */ 89 int seachStack(stack *stackPointer, stack_element elem) 90 { 91 if(stackPointer == NULL) return false; 92 node *tmp = stackPointer->top; 93 for(; tmp != stackPointer->bottom; tmp = tmp->next) { 94 if(elem == tmp->element) 95 return true; 96 } 97 return false; 98 }
“栈”的双链表实现
1 typedef struct dual_Node { 2 unsigned int element; 3 struct dual_Node *pripor, *next; 4 }dualNode; 5 typedef struct { 6 dualNode *top, *bottom; 7 }stack;
1 /* 2 * 函数介绍: 初始化栈 3 * 参数: 4 * 返回值: 5 * 备注: MAX_VERTEX_NUM表示元素为空 6 */ 7 void initStack (stack **stackPointer) 8 { 9 if(*stackPointer) *stackPointer = NULL; 10 *stackPointer = (stack *)malloc(sizeof(stack)); 11 if(!(*stackPointer)) { 12 perror("malloc"); 13 return; 14 } 15 dualNode *tmp = (dualNode *)malloc(sizeof(dualNode)); 16 if(tmp) { 17 tmp->element = MAX_VERTEX_NUM; 18 tmp->pripor = tmp; 19 tmp->next = tmp; 20 (*stackPointer)->top = tmp; 21 (*stackPointer)->bottom = tmp; 22 } 23 return; 24 } 25 /* 26 * 函数介绍: 进栈 27 * 参数: 28 * 返回值: 29 * 备注: 30 */ 31 unsigned int pushStack(stack *stackPointer, unsigned int elem) 32 { 33 if(stackPointer->top->pripor == stackPointer->bottom) { 34 dualNode *tmp = (dualNode *)malloc(sizeof(dualNode)); 35 if(!tmp) return 1; 36 stackPointer->top->pripor = tmp; 37 tmp->next = stackPointer->top; 38 tmp->pripor = stackPointer->bottom; 39 stackPointer->bottom->next = tmp; 40 } 41 stackPointer->top = stackPointer->top->pripor; 42 stackPointer->top->element = elem; 43 return 0; 44 } 45 /* 46 * 函数介绍: 出栈 47 * 参数: 48 * 返回值: MAX_VERTEX_NUM表示元素为空 49 * 备注: 50 */ 51 unsigned int popStack(stack *stackPointer) 52 { 53 unsigned int data = stackPointer->top->element; 54 if(stackPointer->top != stackPointer->bottom) 55 stackPointer->top = stackPointer->top->next; 56 return data; 57 } 58 /* 59 * 函数介绍: 查找元素是否在栈中 60 * 参数: 61 * 返回值: 62 * 备注: 63 */ 64 int seachStack(stack *stackPointer, unsigned int elem) 65 { 66 dualNode *tmp = stackPointer->top; 67 while(tmp != stackPointer->bottom) { 68 if(elem == tmp->element) return true; 69 tmp = tmp->next; 70 } 71 return false; 72 } 73 /* 74 * 函数介绍: 销毁栈 75 * 参数: 76 * 返回值: 77 * 备注: 78 */ 79 void destroyStack (stack **stackPointer) 80 { 81 dualNode *tmp = (*stackPointer)->top; 82 (*stackPointer)->top = (*stackPointer)->top->pripor; 83 while(tmp != (*stackPointer)->top) { 84 (*stackPointer)->bottom = tmp->next; 85 free(tmp); 86 tmp = (*stackPointer)->bottom; 87 } 88 free(tmp); 89 tmp = NULL; 90 }
标签:bsp size 技术 closed col one false 链表操作 max
原文地址:https://www.cnblogs.com/pangea/p/14144312.html