1.栈的相关概念
栈是一种特殊的线性表, 其中只允许在固定的一端进行插入和删除元素.进行数据插入和删除的一端叫做栈顶, 另一端成为栈底. 不含任何元素的栈称为空栈, 栈又称为先进先出的线性表.
2. 顺序栈的结构
3. 顺序栈的具体操作
(1). 数据结构
typedef char SeqStackType;typedef struct SeqStack
{SeqStackType *data;int size;int capacity;
}SeqStack;
(2).顺序栈的初始化
void SeqStackInit(SeqStack* stack)
{if(stack == NULL){return;//非法输入}stack -> size = 0;stack -> capacity = 1000;stack -> data = (SeqStackType*)malloc(( stack -> capacity ) * sizeof(SeqStack));
}
(3). 顺序栈的销毁
void SeqStackDestroy(SeqStack* stack)
{if(stack == NULL){return;//非法输入}free(stack -> data);stack -> size = 0;stack -> capacity = 0;
}
(4). 顺序栈的扩容
void SeqStackReSize(SeqStack* stack)
{if(stack == NULL){return;}if(stack -> size < stack -> capacity){return;}stack -> capacity = stack -> capacity * 2 + 1;SeqStackType* new_ptr = (SeqStackType*)malloc(stack -> capacity);int i = 0;for(; i < stack -> size; i++){new_ptr[i] = stack -> data[i];}}
(5). 顺序栈的入栈
void SeqStackPush(SeqStack* stack, SeqStackType value)
{if(stack == NULL){return;}if(stack -> size >= stack -> capacity){//扩容SeqStackReSize(stack);}stack -> data[stack -> size++] = value;
}
(6). 顺序栈的打印
void TestPrintChar(SeqStack* stack, char* msg)
{printf("[ %s ]\n", msg);if(stack == NULL){return;}printf("size = %d\n", stack -> size);printf("capacity = %d\n", stack -> capacity);int i = 0;for(; i < stack -> size; i++){printf("[%c] ", stack -> data[i]);}printf("\n");
}
(7). 顺序栈的出栈
void SeqStackPop(SeqStack* stack)
{if(stack == NULL){return;//非法输入}if(stack -> size == 0){return;//空栈}stack -> size--;
}
(8). 取栈顶元素
int SeqStackGetFront(SeqStack* stack, SeqStackType* value)
{if(stack == NULL || value == NULL){return -1;//非法输入}if(stack -> size == 0){return -1;//空栈}*value = stack -> data[stack -> size - 1];return 0;
}
(9). 测试代码
//以下为测试代码
void TestGetFront()
{TESTHEADER;SeqStack stack;SeqStackInit(&stack);SeqStackPush(&stack, 'a');SeqStackPush(&stack, 'b');SeqStackPush(&stack, 'c');SeqStackPush(&stack, 'd');TestPrintChar(&stack, "入栈四个元素");SeqStackType value;int ret = SeqStackGetFront(&stack, &value);printf("expected ret = 0, actual ret = %d\n", ret);printf("expected value = d, actual value = %c\n", value);
}void TestDestroy()
{TESTHEADER;SeqStack stack;SeqStackInit(&stack);SeqStackPush(&stack, 'a');SeqStackPush(&stack, 'b');SeqStackPush(&stack, 'c');SeqStackPush(&stack, 'd');TestPrintChar(&stack, "入栈四个元素");SeqStackDestroy(&stack);TestPrintChar(&stack, "销毁栈");
}void TestInit()
{TESTHEADER;SeqStack stack;SeqStackInit(&stack);TestPrintChar(&stack, "初始化栈");
}void TestPop()
{TESTHEADER;SeqStack stack;SeqStackInit(&stack);SeqStackPush(&stack, 'a');SeqStackPush(&stack, 'b');SeqStackPush(&stack, 'c');SeqStackPush(&stack, 'd');TestPrintChar(&stack, "入栈四个元素");SeqStackPop(&stack);SeqStackPop(&stack);TestPrintChar(&stack, "出栈两个元素");SeqStackPop(&stack);SeqStackPop(&stack);TestPrintChar(&stack, "再出栈两个元素");SeqStackPop(&stack);TestPrintChar(&stack, "对空栈进行出栈");}void TestPush()
{SeqStack stack;SeqStackInit(&stack);SeqStackPush(&stack, 'a');SeqStackPush(&stack, 'b');SeqStackPush(&stack, 'c');SeqStackPush(&stack, 'd');TestPrintChar(&stack, "入栈");
}int main()
{TestInit();TestPush();TestPop();TestGetFront();TestDestroy();return 0;
}