文章的算法实例阅读需要一定的c基础,在涉及算法之前会先实现栈的顺序结构与链式结构,希望能帮到你复习栈的知识
文中几个问题的解决,依赖最基础的数据结构,所以,最好的方式是自己实现一遍最基础的栈结构,最好能调试栈的本质方法
至少保障最基本的栈结构能独立实现,这不是一件很难的事情
如果不动手操作一番,这些算法往往就是过眼烟云尔
我自己不是那类聪明人,能传达的经验就是,正儿八经的自己多敲几遍,然后再以这些基础分析涉及到的几个算法问题
慢慢的你会习惯,习惯着用计算机的思维方式去考虑遇到的问题,但是又会在计算机思维的基础上,发挥我们人脑特有的抽象演化跟想象力,最终得出解决问题的方案。
栈的顺序结构实现
// 初始化顺序结构SeqStack
bool initSeqStack(struct SeqStack *sStack) {
sStack->top = -1;
return true;
}
bool seqStackEmpty(struct SeqStack *sStack) {
return sStack->top <= -1;
}
void configSeqStackData(struct SeqStack *sStack, int *array, int n) {
if (n > INIT_SEQSTACK_CAPACITY) {
n = INIT_SEQSTACK_CAPACITY;
}
for (int i = 0; i < n; i++) {
union SeqStackNode mStackNode;
mStackNode.data = array[i];
sStack->nodes[++(sStack->top)] = mStackNode;
}
}
void configSeqStackCh(struct SeqStack *sStack, char *array, int n) {
if (n > INIT_SEQSTACK_CAPACITY) {
n = INIT_SEQSTACK_CAPACITY;
}
for (int i = 0; i < n; i++) {
union SeqStackNode mStackNode;
mStackNode.ch = array[i];
sStack->nodes[++(sStack->top)] = mStackNode;
}
}
// 栈顶元素
union SeqStackNode *topSeqStack(struct SeqStack *sStack) {
if (sStack->top < 0) {
return NULL;
}
return &sStack->nodes[sStack->top];
}
// 栈长度
int lengthSeqStack(struct SeqStack *sStack) {
return sStack->top + 1;
}
// 压栈
bool pushSeqStack(struct SeqStack *sStack, union SeqStackNode *node) {
if (sStack->top >= INIT_SEQSTACK_CAPACITY - 1) {
printf("栈已满,请pop栈.....\n");
return false;
}
sStack->top = sStack->top + 1;
sStack->nodes[sStack->top] = *node;
return true;
}
// 出栈
union SeqStackNode *popSeqStack(struct SeqStack *sStack) {
if (sStack->top < 0) {
return NULL;
}
sStack->top = sStack->top - 1;
return &sStack->nodes[sStack->top + 1];
}
// 遍历
void traverseSeqStack1(struct SeqStack *sStack) {
if (sStack->top < 0) {
return;
}
printf("\n========== stack - s ========== \n");
for (int i = 0; i < sStack->top + 1; i++) {
printf(" <<< %i", sStack->nodes[i].data);
}
printf("\n========== stack - e ========== \n");
}
void traverseSeqStack2(struct SeqStack *sStack) {
if (sStack->top < 0) {
return;
}
printf("\n========== stack - s ========== \n");
for (int i = 0; i < sStack->top + 1; i++) {
printf(" <<< %c)", sStack->nodes[i].ch);
}
printf("\n========== stack - e ========== \n");
}
// 测试 顺序结构 stack 基本功能
void testSeqStack(void) {
struct SeqStack sStack;
initSeqStack(&sStack);
int array[10] = {1, 2, 3, 5, 7, 9, 6, 4, 2, 0};
configSeqStackData(&sStack, array, 10);
union SeqStackNode node1;
node1.data = 30;
printf("--- 压栈: %i---\n", node1.data);
pushSeqStack(&sStack, &node1);
traverseSeqStack1(&sStack);
printf("seq stack 长度: %i\n", lengthSeqStack(&sStack));
printf("--- 出栈---\n");
union SeqStackNode *node2 = popSeqStack(&sStack);
printf("出栈节点 : %i\n", node2->data);
traverseSeqStack1(&sStack);
printf("seq stack 长度: %i\n", lengthSeqStack(&sStack));
}
栈的链式结构实现
// 初始化 链式结构SeqStack
bool initLinkStack(struct LinkStack *lStack) {
lStack->count = 0;
lStack->top = NULL; // top 指向头节点
return NULL;
}
void configLinkStackData(struct LinkStack *lStack, int *array, int n) {
for (int i = 0; i < n; i++) {
union SeqStackNode mStackNode;
mStackNode.data = array[i];
struct LinkStackNode *lStackNode = (struct LinkStackNode *)malloc(sizeof(struct LinkStackNode *));
lStackNode->item = mStackNode;
lStackNode->next = NULL;
if (lStack->top == NULL) {
lStack->top = lStackNode;
} else {
lStackNode->next = lStack->top;
lStack->top = lStackNode;
}
lStack->count = lStack->count + 1;
}
}
void configLinkStackCh(struct LinkStack *lStack, char *array, int n) {
for (int i = 0; i < n; i++) {
union SeqStackNode mStackNode;
mStackNode.data = array[i];
struct LinkStackNode *lStackNode = (struct LinkStackNode *)malloc(sizeof(struct LinkStackNode *));
lStackNode->item = mStackNode;
lStackNode->next = NULL;
if (lStack->top == NULL) {
lStack->top = lStackNode;
} else {
lStackNode->next = lStack->top;
lStack->top = lStackNode;
}
lStack->count = lStack->count + 1;
}
}
// 栈顶元素
union SeqStackNode topLinkStack(struct LinkStack *lStack) {
return lStack->top->item;
}
// 栈长度
int lengthLinkStack(struct LinkStack *lStack) {
return lStack->count;
}
// 压栈
bool pushLinkStack(struct LinkStack *lStack, union SeqStackNode *item) {
struct LinkStackNode *lStackNode = (struct LinkStackNode *)malloc(sizeof(struct LinkStackNode *));
lStackNode->item = *item;
lStackNode->next = NULL;
if (lStack->top == NULL) {
lStack->top = lStackNode;
} else {
lStackNode->next = lStack->top;
lStack->top = lStackNode;
}
lStack->count = lStack->count + 1;
return true;
}
// 出栈
union SeqStackNode popLinkStack(struct LinkStack *lStack) {
struct LinkStackNode *q = lStack->top;
lStack->top = q->next;
lStack->count = lStack->count - 1;
union SeqStackNode mStackNode = q->item;
free(q);
return mStackNode;
}
// 遍历
void traverseLinkStack1(struct LinkStack *lStack) {
struct LinkStackNode *q = lStack->top;
printf("\n========== link stack - s ========== \n");
while (q != NULL) {
printf(" <<< %i", q->item.data);
q = q->next;
}
printf("\n========== link stack - e ========== \n");
}
void traverseLinkStack2(struct LinkStack *lStack) {
struct LinkStackNode *q = lStack->top;
printf("\n========== link stack - s ========== \n");
while (q != NULL) {
printf(" <<< %c", q->item.ch);
q = q->next;
}
printf("\n========== link stack - e ========== \n");
}
void testLinkStack(void) {
struct LinkStack lStack;
initLinkStack(&lStack);
int array[10] = {1, 2, 3, 5, 7, 9, 6, 4, 2, 0};
configLinkStackData(&lStack, array, 10);
union SeqStackNode node1;
node1.data = 30;
printf("--- 压栈: %i---\n", node1.data);
pushLinkStack(&lStack, &node1);
traverseLinkStack1(&lStack);
printf("link stack 长度: %i\n", lengthLinkStack(&lStack));
printf("--- 出栈---\n");
union SeqStackNode node2 = popLinkStack(&lStack);
printf("出栈节点 : %i\n", node2.data);
traverseLinkStack1(&lStack);
printf("link stack 长度: %i\n", lengthLinkStack(&lStack));
printf("--- 出栈---\n");
union SeqStackNode node3 = popLinkStack(&lStack);
printf("出栈节点 : %i\n", node3.data);
traverseLinkStack1(&lStack);
printf("link stack 长度: %i\n", lengthLinkStack(&lStack));
}