1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #include "linkstack.h"
bool InitStack(LinkStack *stack) { (*stack) = (LinkStack)malloc(sizeof(struct LinkStack)); InitLinkList(&((*stack)->block)); (*stack)->length = 0; (*stack)->base = (Item *)malloc(sizeof(Item)); (*stack)->top = (Item *)malloc(sizeof(Item)); printf("初始化LinkStack成功\n"); return true; }
bool DestroyStack(LinkStack *stack) { DestroyLinkList(&((*stack)->block)); free((*stack)); (*stack) = NULL; printf("销毁LinkStack成功\n"); return true; }
bool ClearStack(LinkStack *stack) { ClearLinkList(&((*stack)->block)); (*stack)->length = 0; (*stack)->base = NULL; (*stack)->top = NULL; printf("清空LinkStack成功\n"); return true; }
bool EmptyStack(LinkStack *stack) { if ((*stack)->length == 0) { printf("LinkStack空\n"); return true; } else { printf("LinkStack不空\n"); return false; } }
bool GetTopStack(LinkStack *stack, Item *itemsave) { if ((*stack)->length == 0) { printf("错误,栈中没有元素\n"); return false; } else { *itemsave = *((*stack)->top); return true; } }
bool PushStack(LinkStack *stack, Item *item) { InsertLinkList(&((*stack)->block), (*stack)->length, item); (*stack)->length++; if ((*stack)->length == 1) { GetItemLinkList(&((*stack)->block), 0, (*stack)->base); } GetItemLinkList(&((*stack)->block), (*stack)->length - 1, (*stack)->top); return true; }
bool PopStack(LinkStack *stack) { if ((*stack)->length == 0) { printf("已经空了,不能pop\n"); return false; } DeleteLinkList(&((*stack)->block), (*stack)->length - 1); (*stack)->length--; GetItemLinkList(&((*stack)->block), (*stack)->length - 1, (*stack)->top); return true; }
void AutoFillStack(LinkStack *stack) { for (int i = 0; i <= AUTOFILLSIZE; i++) { Item item; item.value = i; PushStack(stack, &item); } }
void TraverseStack(LinkStack *stack, void (*fun)(Item item)) { printf("TraverseStack\n"); Item item; while (!EmptyStack(stack)) { GetTopStack(stack, &item); fun(item); PopStack(stack); } }
|