栈的基本操作:
头文件“Stack.h”
# pragma once
# include "Stack.h"
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
# include <string.h>
# define maxsize 100
typedef char DataType;
typedef struct Stack
{
DataType array[maxsize];
int top;
}stack;
//初始化
void InitStack(stack *p);
//销毁
void Destory(stack *p);
//判断是否为空
int IsEmpty(stack *p);
//压栈
void Push(stack *p, DataType data);
//出栈
void Pop(stack *p);
//返回栈顶的值
DataType Top(stack *p);
//栈的大小
int StackSize(stack *p);
//显示
void Show(stack *p);
//测试
void StackTest();
Stack.c文件
# include "Stack.h"
//初始化
void InitStack(stack *p)
{
assert(p);
p->top = 0;
}
//销毁
void Destory(stack *p)
{
assert(p);
p->top = 0;
}
//判断是否为空
int IsEmpty(stack *p)
{
return p->top == 0 ? 1 : 0;
}
//压栈
void Push(stack *p, DataType data)
{
assert(p != NULL);
if (p->top >= maxsize)
printf("栈溢出\n");
p->array[p->top++] = data;
}
//出栈
void Pop(stack *p)
{
assert(p != NULL);
if (p->top == 0)
{
printf("栈为空\n");
return;
}
p->top--;
}
//返回栈顶的值
DataType Top(stack *p)
{
assert(p != NULL);
if (p->top == 0)
printf("栈为空\n");
return p->array[--(p->top)];
}
//返回栈顶的值
DataType GetTop(stack *p)
{
assert(p != NULL);
if (p->top == 0)
printf("栈为空\n");
return p->array[p->top - 1];
}
//栈的大小
int StackSize(stack *p)
{
return p->top;
}
//显示
void Show(stack *p)
{
int i = 0;
for (; i < p->top; i++)
printf("%d ", p->array[i]);
printf("\n");
}
//测试
void StackTest()
{
stack s;
InitStack(&s);
Push(&s, 1);
Push(&s, 2);
Push(&s, 3);
Push(&s, 4);
Push(&s, 5);
Show(&s);
DataType a = Top(&s);
printf("%d\n", a);
Show(&s);
}
test.c 文件
# include "Stack.h"
int main()
{
StackTest();
system("pause");
return 0;
}
面试题
1.括号匹配问题
//括号匹配问题
void Match(char *a)
{
stack s;
InitStack(&s);//初始化栈
char c;
int i= 0, b = 1;//b用来标记是否匹配
while ( b == 1)
{
if (a[i] == '\0')
break;
if (a[i] == '(' || a[i] == '[' || a[i] == '{')
Push(&s, a[i]);
else if (a[i] == ')')
{
c = Top(&s);
if (c != '(')
b = 0;
}
else if (a[i] == ']')
{
c = Top(&s);
if (c != '[')
b = 0;
}
else if (a[i] == '}')
{
c = Top(&s);
if (c != '{')
b = 0;
}
i++;
}
if (b&&IsEmpty(&s))
printf("匹配\n");
else
printf("不匹配\n");
}
2.最小栈
头文件MiniStack.h
# pragma once
# include "MinStack1.h"
# include "Stack.h"
# include <stdio.h>
# include <stdlib.h>
typedef int ElemType1;
//用一个栈
typedef struct MinStack1
{
stack data;
}MinStack1;
//初始化
void MinInit(MinStack1 *p);
//销毁
void MinDestory(MinStack1 *p);
//入栈
void MinPush(MinStack1 *p, ElemType1 data);
//出栈
void MinPop(MinStack1 *p);
//返回栈顶的值
ElemType1 MinTop(MinStack1 *p);
//返回最小值
ElemType1 Min(MinStack1 *p);
MiniStack.c文件:
# include "MinStack1.h"
//初始化
void MinInit1(MinStack1 *p)
{
assert(p != NULL);
InitStack(&(p->data));
}
//销毁
void MinDestory1(MinStack1 *p)
{
assert(p != NULL);
Destory(&(p->data));
}
//入栈
void MinPush1(MinStack1 *p, ElemType1 data)
{
assert(p != NULL);
DataType a, min;//a存放栈的值 min存放最小值
if (IsEmpty(&(p->data)))
{
a = data;
min = data;
Push(&(p->data), a);
Push(&(p->data), min);
}
else
{
if (data < GetTop(&(p->data)))
{
min = data;
a = data;
Push(&(p->data), a);
Push(&(p->data), min);
}
else
{
min = GetTop(&(p->data));
a = data;
Push(&(p->data), a);
Push(&(p->data), min);
}
}
}
//出栈
void MinPop1(MinStack1 *p)
{
assert(p != NULL);
if (p->data.top == 0)
{
printf("栈为空\n");
}
Pop(&(p->data));
Pop(&(p->data));
}
//返回栈顶的值
ElemType1 MinTop1(MinStack1 *p)
{
assert(p != NULL);
if (p->data.top == 0)
{
printf("栈为空\n");
}
return GetTop2(&(p->data));
}
//返回最小值
ElemType1 Min1(MinStack1 *p)
{
assert(p != NULL);
if (p->data.top == 0)
{
printf("栈为空\n");
}
return GetTop(&(p->data));
}
//测试
void MinStack1Test()
{
MinStack1 s;
MinInit1(&s);
MinPush1(&s, 7);
MinPush1(&s, 6);
MinPush1(&s, 5);
MinPush1(&s, 1);
MinPush1(&s, 1);
MinPush1(&s, 2);
MinPush1(&s, 1);
Show(&(s.data));
MinPop1(&s);
printf("栈顶的值:%d\n", MinTop1(&s));
printf("最小值:%d\n", Min1(&s));
}
test.c
//# include "MinStack.h"
# include "MinStack1.h"
int main()
{
MinStackTest();
//MinStack1Test();
system("pause");
return 0;
}
3.两个队列实现一个栈
头文件
# pragma once
# include "StackBy2Queue.h"
# include "Queue.h"
typedef struct Stack1
{
queue d1;
queue d2;
int size;
}stack1;
//初始化
void StackInit(stack1 *p);
//销毁
void StackDestory(stack1 *p);
//入栈
void StackPush(stack1 *p,DataType data);
//出栈
void StackPop(stack1 *p);
//返回栈顶的值
DataType StackTop(stack1 *p);
//栈的大小
int StackSize1(stack1 *p);
具体实现代码
# include "StackBy2Queue.h"
//初始化
void StackInit(stack1 *p)
{
assert(p != NULL);
QueueInit(&(p->d1));
QueueInit(&(p->d2));
}
//销毁
void StackDestory(stack1 *p)
{
assert(p != NULL);
QueueDestory(&(p->d2));
QueueDestory(&(p->d1));
}
//入栈
void StackPush(stack1 *p,DataType data)
{
assert(p != NULL);
//先判断哪个队列不为空,放到该队列,第一个元素在q2
if (!QueueIsEmpty(&p->d1))
{
QueuePush(&p->d1, data);
}
else
{
QueuePush(&p->d2, data);
}
p->size++;
}
//出栈
void StackPop(stack1 *p)
{
assert(p != NULL);
//当d1为空,把d2的前size - 1个元素倒入d1
if (QueueIsEmpty(&p->d1) && QueueIsEmpty(&p->d2))
printf("栈为空\n");
if (QueueIsEmpty(&p->d1))
{
while (QueueSize(&p->d2) > 1)
{
QueuePush(&p->d1, QueueTop(&p->d2));
QueuePop(&p->d2);
}
QueuePop(&p->d2); //移除d2最后一个元素
}
else //d2为空
{
while (QueueSize(&p->d1) > 1)
{
QueuePush(&p->d2, QueueTop(&p->d1));
QueuePop(&p->d1);
}
QueuePop(&p->d1);
}
p->size--;
}
//返回栈顶的值
DataType StackTop(stack1 *p)
{
assert(p != NULL);
if (QueueIsEmpty(&p->d1) && QueueIsEmpty(&p->d2))
printf("栈为空\n");
//当d1为空,把d2的前size - 1个元素倒入d1
if (QueueIsEmpty(&p->d1))
{
while (QueueSize(&p->d2) > 1)
{
QueuePush(&p->d1, QueueTop(&p->d2));
QueuePop(&p->d2);
}
return QueueTop(&p->d2); //返回d2最后一个元素
}
else //d2为空
{
while (QueueSize(&p->d1) > 1)
{
QueuePush(&p->d2, QueueTop(&p->d1));
QueuePop(&p->d1);
}
return QueueTop(&p->d1);
}
}
//栈的大小
int StackSize1(stack1 *p)
{
return p->size;
}
//测试
void Stack1Test()
{
stack1 s;
QueueInit(&s);
QueuePush(&s, 1);
QueuePush(&s, 2);
QueuePush(&s, 3);
QueuePush(&s, 4);
QueuePush(&s, 5);
QueuePush(&s, 6);
printf("%d\n", QueueTop(&s));
}
4.两个栈实现一个队列
头文件:
# pragma once
# include "QueueBy2Stack.h"
# include "Stack.h"
typedef int ElemType;
typedef struct Queue1
{
stack input;
stack output;
//队列的有效元素个数
int size;
}queue1;
//初始化
void QueueInit1(queue1 *p);
//销毁
void QueueDestory1(queue1 *p);
//入队
void QueuePush1(queue1 *p,ElemType data);
//出队
void QueuePop1(queue1 *p);
//队头的值
ElemType QueueTop1(queue1 *p);
//打印
void QueueShow1(queue1 *p);
具体实现代码
# include "QueueBy2Stack.h"
//初始化
void QueueInit1(queue1 *p)
{
assert(p != NULL);
InitStack(&(p->input));
InitStack(&(p->output));
p->size = 0;
}
//销毁
void QueueDestory1(queue1 *p)
{
assert(p != NULL);
Destory(&(p->output));
Destory(&(p->input));
p->size = 0;
}
//入队
void QueuePush1(queue1 *p, ElemType data)
{
assert(p != NULL);
//如果output为空直接入栈input
//intput为空但是output不为空,此时入队操作,应该把栈output中的元素移回intput,再执行input入栈
if (IsEmpty(&(p->output)))
{
Push(&(p->input), data);
}
else
{
while (!IsEmpty(&(p->output)))
{
ElemType a = Top(&(p->output));
Push(&(p->input), a);
}
Push(&(p->input), data);
}
p->size++;
}
//出队
void QueuePop1(queue1 *p)
{
//判断input是否为空,不为空则全部压入output栈,然后output栈pop
assert(p != NULL);
if (p->size==0)
printf("队列为空\n");
while (p->input.top!=0)
{
ElemType a = Top(&(p->input));
Push(&(p->output), a);
}
Pop(&(p->output));
p->size--;
}
//队头的值
ElemType QueueTop1(queue1 *p)
{
assert(p != NULL);
if (p->size==0)
printf("队列为空\n");
while (!IsEmpty(&(p->input)))
{
ElemType a = Top(&(p->input));
Push(&(p->output), a);
}
return GetTop(&(p->output));
}
//打印
void QueueShow1(queue1 *p)
{
assert(p != NULL);
if (p->size == 0)
printf("队列为 空\n");
while (p->input.top != 0)
{
ElemType a = Top(&(p->input));
Push(&(p->output), a);
}
Show(&(p->output));
}
//测试
void Queue1Test()
{
queue1 s;
QueueInit1(&s);
QueuePush1(&s, 1);
QueuePush1(&s, 2);
QueuePush1(&s, 3);
QueuePush1(&s, 4);
QueuePush1(&s, 5);
QueuePush1(&s, 6);
QueuePush1(&s, 7);
QueueShow1(&s);
printf("%d\n", s.size);
QueuePop1(&s);
QueueShow1(&s);
printf("%d\n",QueueTop1(&s));
}
5.元素出栈入栈的合法性
void IsJudge(char *in, char *out)
{
stack q;
InitStack(&q);
assert(in&&out);
int i = 0;
int j = 0;
if (strlen(in) != strlen(out))
{
printf("不合法\n");
return;
}
for (; i<strlen(in); i++)//将入栈序列压入栈
{
if (IsEmpty(&q)|| GetTop(&q) != out[j])//栈为空或者栈顶元素不等于出栈序列
{
Push(&q, in[i]);
}
else//相等时将栈顶元素出栈,并将出栈序列向后移动一位
{
Pop(&q);
j++;
}
}
while (!IsEmpty(&q))//压栈结束后,再比较出栈序列剩下的
{
if (GetTop(&q)!= out[++j])
{
printf("不合法\n");
break;
}
else
Pop(&q);
}
if (IsEmpty(&q))//循环比较后,如果栈为空,则出栈序列是合法的。
printf("合法\n");
}
一个数组实现两个栈
1.奇偶数组
头文件
# pragma once
# include "TwoStackBy1Array1.h"
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <assert.h>
# define CAPACITY 4
typedef int DataType;
//奇偶栈
typedef struct Stack2
{
DataType *array;
int capacity;
int top1;
int top2;
}stack2;
//初始化
void InitStack2(stack2 *p);
//销毁
void DestoryStack2(stack2 *p);
//增容
void ExpandIfRequire(stack2 *p);
//入栈
void PushStack2(stack2 *p,DataType data, int a);//a表示是哪一个
//出栈
void PopStack2(stack2 *p, int a);
//返回栈顶的值
DataType TopStack2(stack2 *p, int a);
//栈的大小
int SizeStack2(stack2 *p, int a);
//判断是否为空
int IsEmpty2(stack2 *p, int a);
//打印
void Show2(stack2 *p, int a);
具体实现代码
# include "TwoStackBy1Array1.h"
//初始化
void InitStack2(stack2 *p)
{
assert(p!=NULL);
p->capacity = CAPACITY;
p->array = (DataType *)malloc(sizeof(DataType)*p->capacity);
p->top1 = 0;
p->top2 = 1;
}
//销毁
void DestoryStack2(stack2 *p)
{
assert(p != NULL);
p->top1 = 0;
p->top2 = 1;
free(p->array);
}
//增容
void ExpandIfRequire(stack2 *p)
{
if (p->top1 != p->capacity&&p->top2 != p->capacity + 1)
return;
int oldcapacity = p->capacity;
p->capacity *= 2;
DataType *newarray = (DataType *)malloc(sizeof(DataType)*p->capacity);
assert(newarray != NULL);
memcpy(newarray, p->array, sizeof(DataType)*oldcapacity);
free(p->array);
p->array = newarray;
}
//入栈
void PushStack2(stack2 *p,DataType data, int a)
{
assert(p != NULL);
ExpandIfRequire(p);
if (a == 1)
{
p->array[p->top1] = data;
p->top1 += 2;
}
else if (a == 2)
{
p->array[p->top2 ] = data;
p->top2 += 2;
}
}
//出栈
void PopStack2(stack2 *p, int a)
{
assert(p != NULL);
if (a == 1)
{
p->top1 -=2;
}
else if (a == 2)
{
p->top2 -=2;
}
}
//返回栈顶的值
DataType TopStack2(stack2 *p, int a)
{
assert(p != NULL);
if (a == 1)
{
return p->array[p->top1 - 2];
}
else if (a == 2)
{
return p->array[p->top2 - 2];
}
}
//栈的大小
int SizeStack2(stack2 *p, int a)
{
assert(p != NULL);
if (a == 1)
{
return p->top1 / 2;
}
else if (a == 2)
{
return p->top2 / 2;
}
}
//判断是否为空
int IsEmpty2(stack2 *p, int a)
{
assert(p != NULL);
if (a == 1)
{
return p->top1 == 0 ? 1 : 0;
}
else if (a == 2)
{
return p->top2 == 1 ? 1 : 0;
}
}
//打印
void Show2(stack2 *p, int a)
{
if (a == 1)
{
int i = 0;
for (; i < p->top1; i+=2)
printf("%d ", p->array[i]);
printf("\n");
}
else if (a == 2)
{
int i = 1;
for (; i < p->top2;i+=2)
printf("%d ", p->array[i]);
printf("\n");
}
}
//测试
void Test()
{
stack2 s;
InitStack2(&s);
PushStack2(&s, 1, 1);
PushStack2(&s, 1, 2);
PushStack2(&s, 2, 1);
PushStack2(&s, 2, 2);
PushStack2(&s, 3, 1);
PushStack2(&s, 3, 2);
PushStack2(&s, 4, 1);
PushStack2(&s, 4, 2);
Show2(&s,1);
Show2(&s,2);
}
双头栈
头文件
# pragma once
# include "2StackBy1Array.h"
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
# define maxsize 100
# define MAXSIZE 200
typedef int DataType;
//双头栈
typedef struct TwoStack
{
DataType array[MAXSIZE];
int top1;
int top2;
}twostack;
//初始化
void InitTwoStack(twostack *p);
//销毁
void DestoryTwoStack(twostack *p);
//入栈
void PushTwoStack(twostack *p, DataType data, int a);
//出栈
void PopTwoStack(twostack *p, int a);
//返回栈顶的值
DataType TopTwoStack(twostack *p, int a);
//栈的大小
int SizeTwoStack(twostack *p, int a);
//判断是否为空
int EmptyTwoStack(twostack *p, int a);
//显示
void ShowTwoStack(twostack *p, int a);
具体实现代码
# include "2StackBy1Array.h"
//初始化
void InitTwoStack(twostack *p)
{
assert(p != NULL);
p->top1 = 0;
p->top2 = MAXSIZE-1;
}
//销毁
void DestoryTwoStack(twostack *p)
{
assert(p != NULL);
p->top1 = 0;
p->top2 = MAXSIZE-1;
}
//入栈
void PushTwoStack(twostack *p, DataType data, int a)//a 表示是哪一个
{
assert(p != NULL);
if (a ==1)
{
if (p->top1 >= maxsize)
{
printf("栈溢出\n");
return;
}
p->array[p->top1++] = data;
}
else if (a == 2)
{
if (p->top2 < maxsize)
{
printf("栈溢出\n");
return;
}
p->array[p->top2--] = data;
}
}
//出栈
void PopTwoStack(twostack *p, int a)
{
assert(p != NULL);
if (a == 1)
{
if (p->top1 == 0)
printf("栈为空\n");
p->top1--;
}
else if (a == 2)
{
if (p->top2 == MAXSIZE-1)
printf("栈为空\n");
p->top2++;
}
}
//返回栈顶的值
DataType TopTwoStack(twostack *p, int a)
{
assert(p != NULL);
if (a == 1)
{
if (p->top1 == 0)
printf("栈为空\n");
return p->array[p->top1 - 1];
}
else if (a == 2)
{
if (p->top2== MAXSIZE-1)
printf("栈为空\n");
return p->array[p->top2+1];
}
}
//栈的大小
int SizeTwoStack(twostack *p, int a)
{
assert(p != NULL);
if (a == 1)
{
return p->top1;
}
else if (a == 2)
{
return MAXSIZE - (p->top2)-1;
}
}
//判断是否为空
int EmptyTwoStack(twostack *p, int a)
{
assert(p != NULL);
if (a == 1)
{
return p->top1 == 0 ? 1 : 0;
}
else if (a == 2)
{
return p->top2 == MAXSIZE-1 ? 1 : 0;
}
}
//显示
void ShowTwoStack(twostack *p, int a)
{
assert(p != NULL);
if (a == 1)
{
int i = 0;
for (; i < p->top1; i++)
printf("%d ", p->array[i]);
printf("\n");
}
else if (a == 2)
{
int i = MAXSIZE-1;
for (; i >(p->top2); i--)
printf("%d ", p->array[i]);
printf("\n");
}
}
//测试
void Test()
{
twostack s;
InitTwoStack(&s);
PushTwoStack(&s, 66, 1);
PushTwoStack(&s, 1, 2);
PushTwoStack(&s, 2, 1);
PushTwoStack(&s, 66, 2);
PushTwoStack(&s, 3, 1);
PushTwoStack(&s, 3, 2);
PushTwoStack(&s, 4, 1);
PushTwoStack(&s, 4, 2);
ShowTwoStack(&s, 1);
ShowTwoStack(&s, 2);
printf("%d", SizeTwoStack(&s, 2));
printf("%d", TopTwoStack(&s, 2));
}