栈和栈的应用

#pragma once
#include <stdio.h>

void StackInit(Stack* s)
{
s->_array = (DataType*)malloc(sizeof(DataType) * 3);
s->_capacity = 3;
s->_top = 0;
}

// 入栈
void StackPush(Stack* s, DataType data)
{
DataType* tmp;
int newCapacity = s->_capacity * 2;
if (NULL == s) //未初始化,退出
return;
if (s->_top == s->_capacity) //如果栈已满,就扩容
{
tmp = (DataType*)realloc(s->_array, newCapacity * sizeof(DataType));
if (NULL == tmp)
{
printf("扩容失败\n");
return;
}
s->_array = tmp;
s->_capacity = newCapacity;
}
s->_array[s->_top++] = data;
}

// 出栈
void StackPop(Stack* s)
{
if (NULL == s)
return;
if (0 == s->_top)
return;
else
s->_top--;
}

// 获取栈顶元素
DataType StackTop(Stack* s)
{
if (NULL == s)
exit(EXIT_FAILURE);
return s->_array[s->_top - 1];
}

// 有效元素的个数
int StackSize(Stack* s)
{
if (NULL == s)
return -1;
return s->_top;
}

// 检测栈是否为空
int StackEmpty(Stack* s)
{
if (NULL == s)
exit(EXIT_FAILURE);
if (0 == s->_top)
return 1;
else
return 0;
}

//打印栈
void PrintfStack(Stack s)
{
int i = 0;
if (0 == s._top)
return;
for (; i<s._top; i++)
{
printf("%d ", s._array[i]);
}
printf("\n");
}


//括号匹配
void MatchBrackets(char* ptr)
{
int i = 0;
Stack s;
StackInit(&s); //创建动态栈
while (ptr[i])
{
if (ptr[i] != '(' && ptr[i] != ')'
&& ptr[i] != '[' && ptr[i] != ']'
&& ptr[i] != '{' && ptr[i] != '}') //i位置上字符不是括号,判下一个
{
i++;
continue;
}
else if (ptr[i] == '('
|| ptr[i] == '['
|| ptr[i] == '{') //i位置上字符是左括号---入栈
{
StackPush(&s, ptr[i]);
i++;
}
else
{ //i位置上是右括号-->与栈顶比较
if (0 == StackSize(&s)) //栈为空,匹配失败
{
printf("匹配失败!\n");
return;
}
if ((ptr[i] == ')' && '(' == StackTop(&s)) ||
(ptr[i] == ']' && '[' == StackTop(&s)) ||
(ptr[i] == '}' && '{' == StackTop(&s))) //匹配成功,出栈
{
StackPop(&s);
i++;
}
else
{
printf("匹配失败!\n");
return;
}
}
}
if (StackEmpty(&s))
printf("匹配成功!\n");
else
printf("匹配失败!\n");
}
//找字符串中的空格的下标
int FindSpace(char* ptr, int count)
{
int i = 0;
int _count = 0;
do
{
if (ptr[i] == ' ')
_count++;
i++;
} while (count - _count);
return i - 1;
}

//逆波兰表达式求值
int GetValueRPN(char* ptr)
{
int i = 0, count = 1;
char c;
int left = 0, right = 0;
Stack s;
StackInit(&s);
while (ptr[i])
{
c = ptr[i];
if (c >= '0' && c <= '9') //i位置是数字,入栈
StackPush(&s, atoi(&ptr[i]));
else //i位置是操作符,出栈
{
switch (ptr[i])
{
case '+':
right = StackTop(&s);
StackPop(&s);
left = StackTop(&s);
StackPop(&s);
StackPush(&s, left + right);
break;
case '-':
right = StackTop(&s);
StackPop(&s);
left = StackTop(&s);
StackPop(&s);
StackPush(&s, left - right);
break;
case '*':
right = StackTop(&s);
StackPop(&s);
left = StackTop(&s);
StackPop(&s);
StackPush(&s, left*right);
break;
case '/':
right = StackTop(&s);
StackPop(&s);
left = StackTop(&s);
StackPop(&s);
if (0 == right)
{
printf("除数出错!\n");
return -1;
}
StackPush(&s, left / right);
break;
default:
break;
}
}
i = FindSpace(ptr, count); //找第count个空格的下标
i++;
count++;
}
return StackTop(&s);
}

////////////////////////////////////////////////////
void testStack()
{
Stack s;
StackInit(&s);
StackPush(&s, 1);
StackPush(&s, 2);
StackPush(&s, 3);
StackPush(&s, 4);
StackPush(&s, 5);
PrintfStack(s);
printf("%d\n", StackSize(&s));
printf("%d\n", StackTop(&s));
}


//GetValueRPN
void testGetValueRPN()
{
int result = 0;
char a[] = "12 3 4 + * 6 - 8 2 / +";
result = GetValueRPN(a);
printf("result = %d \n", result);
}

猜你喜欢

转载自www.cnblogs.com/lx1997/p/8876171.html