1、栈的定义
栈是一种线性存储结构,它具有如下特点:
(1)栈中的数据元素遵守”先进后出”或“后进先出”的原则。
(2)限定只能在栈顶进行插入和删除操作。
(3)表尾端为栈顶,另一端为栈底。
2、栈主要有以下几种基本操作:
(1)Push(): 向栈内压入一个元素;
(2)Pop(): 从栈顶弹出一个元素;
(3)Empty(): 如果栈为空返回true,否则返回false;
(4)GetTop(): 返回栈顶,但不删除元素;
(4)size(): 返回栈内元素的大小;
老师代码:
#include <stdio.h>
#include <malloc.h>
#define STACK_MAX_SIZE 10
/**
* Linear stack of integers. The key is data.
*/
typedef struct CharStack {
int top;
int data[STACK_MAX_SIZE]; //The maximum length is fixed.
} *CharStackPtr;
/**
* Output the stack.
*/
void outputStack(CharStackPtr paraStack) {
for (int i = 0; i <= paraStack->top; i ++) {
printf("%c ", paraStack->data[i]);
}// Of for i
printf("\r\n");
}// Of outputStack
/**
* Initialize an empty char stack. No error checking for this function.
* @param paraStackPtr The pointer to the stack. It must be a pointer to change the stack.
* @param paraValues An int array storing all elements.
*/
CharStackPtr charStackInit() {
CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(struct CharStack));
resultPtr->top = -1;
return resultPtr;
}//Of charStackInit
/**
* Push an element to the stack.
* @param paraValue The value to be pushed.
*/
void push(CharStackPtr paraStackPtr, int paraValue) {
// Step 1. Space check.
if (paraStackPtr->top >= STACK_MAX_SIZE - 1) {
printf("Cannot push element: stack full.\r\n");
return;
}//Of if
// Step 2. Update the top.
paraStackPtr->top ++;
// Step 3. Push element.
paraStackPtr->data[paraStackPtr->top] = paraValue;
}// Of push
/**
* Pop an element from the stack.
* @return The poped value.
*/
char pop(CharStackPtr paraStackPtr) {
// Step 1. Space check.
if (paraStackPtr->top < 0) {
printf("Cannot pop element: stack empty.\r\n");
return '\0';
}//Of if
// Step 2. Update the top.
paraStackPtr->top --;
// Step 3. Push element.
return paraStackPtr->data[paraStackPtr->top + 1];
}// Of pop
/**
* Test the push function.
*/
void pushPopTest() {
printf("---- pushPopTest begins. ----\r\n");
// Initialize.
CharStackPtr tempStack = charStackInit();
printf("After initialization, the stack is: ");
outputStack(tempStack);
// Pop.
for (char ch = 'a'; ch < 'm'; ch ++) {
printf("Pushing %c.\r\n", ch);
push(tempStack, ch);
outputStack(tempStack);
}//Of for i
// Pop.
for (int i = 0; i < 3; i ++) {
ch = pop(tempStack);
printf("Pop %c.\r\n", ch);
outputStack(tempStack);
}//Of for i
printf("---- pushPopTest ends. ----\r\n");
}// Of pushPopTest
/**
* Is the bracket matching?
*
* @param paraString The given expression.
* @return Match or not.
*/
bool bracketMatching(char* paraString, int paraLength) {
// Step 1. Initialize the stack through pushing a '#' at the bottom.
CharStackPtr tempStack = charStackInit();
push(tempStack, '#');
char tempChar, tempPopedChar;
// Step 2. Process the string.
for (int i = 0; i < paraLength; i++) {
tempChar = paraString[i];
switch (tempChar) {
case '(':
case '[':
case '{':
push(tempStack, tempChar);
break;
case ')':
tempPopedChar = pop(tempStack);
if (tempPopedChar != '(') {
return false;
} // Of if
break;
case ']':
tempPopedChar = pop(tempStack);
if (tempPopedChar != '[') {
return false;
} // Of if
break;
case '}':
tempPopedChar = pop(tempStack);
if (tempPopedChar != '{') {
return false;
} // Of if
break;
default:
// Do nothing.
break;
}// Of switch
} // Of for i
tempPopedChar = pop(tempStack);
if (tempPopedChar != '#') {
return true;
} // Of if
return true;
}// Of bracketMatching
/**
* Unit test.
*/
void bracketMatchingTest() {
char* tempExpression = "[2 + (1 - 3)] * 4";
bool tempMatch = bracketMatching(tempExpression, 17);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
tempExpression = "( ) )";
tempMatch = bracketMatching(tempExpression, 6);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
tempExpression = "()()(())";
tempMatch = bracketMatching(tempExpression, 8);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
tempExpression = "({}[])";
tempMatch = bracketMatching(tempExpression, 6);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
tempExpression = ")(";
tempMatch = bracketMatching(tempExpression, 2);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
}// Of bracketMatchingTest
/**
The entrance.
*/
void main() {
// pushPopTest();
bracketMatchingTest();
}// Of main
代码块:
1、头文件
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MAXSIZE 10
2、栈的结构
typedef struct Stack{
int top;
int data[MAXSIZE];
}*StackPtr;
3、输出栈的数据
void outputStack(StackPtr paraStack){
int i;
for(i=0;i<=paraStack->top;i++)
{
printf("%d ",paraStack->data[i]);
}
printf("\n");
}
4、初始化
int StackInit(){
StackPtr resultPtr = (StackPtr)malloc(sizeof(struct Stack)) ;
resultPtr->top = -1;
return resultPtr;
}
5、向栈堆中压入元素
void push(StackPtr paraStackPtr,int paraValue){
//判断栈堆是否已满
if(paraStackPtr->top >= MAXSIZE-1)
{
printf("空间已满,不能推送元素!\n");
return;
}
//更新栈顶
paraStackPtr->top++;
//压入元素
paraStackPtr->data[paraStackPtr->top] = paraValue;
}
6、从栈堆中弹出元素
int pop(StackPtr paraStackPtr){
//判断栈堆是否为空
if(paraStackPtr->top<0)
{
printf("栈堆为空,无法弹出元素!\n");
return;
}
//更新栈顶
paraStackPtr->top--;
//弹出元素
return paraStackPtr->data[paraStackPtr->top+1];
}
7、测试压入、弹出功能
void pushpopTest(){
printf("-------测试开始-------\n");
StackPtr tempStack = StackInit();
;
printf("创建空栈成功!\n");
printf("初始化后,栈堆是:");
outputStack(tempStack);
int i;
for(i=0;i<=10;i++)
{
printf("Pushing %d.\n", i);
push(tempStack, i);
outputStack(tempStack);
}
int j;
for (j = 0; j <= 11; j ++)
{
printf("Pop %d.\n", j);
pop(tempStack);
outputStack(tempStack);
}
printf("-------测试结束-------\n");
}
8、 括号匹配代码
bool bracketMatching(char* paraString, int paraLength) {
// 初始化,将#放入栈底
StackPtr tempStack = StackInit();
push(tempStack, '#');
char tempChar, tempPopedChar;
int i;
for (i = 0; i < paraLength; i++)
{
tempChar = paraString[i];
switch (tempChar)
{
case '(':
case '[':
case '{':
push(tempStack, tempChar);
break;
case ')':
tempPopedChar = pop(tempStack);
if (tempPopedChar != '(')
{
return false;
}
break;
case ']':
tempPopedChar = pop(tempStack);
if (tempPopedChar != '[')
{
return false;
}
break;
case '}':
tempPopedChar = pop(tempStack);
if (tempPopedChar != '{')
{
return false;
}
break;
default:
break;
}
}
tempPopedChar = pop(tempStack);
if (tempPopedChar != '#')
{
return false;
}
return true;
}
void bracketMatchingTest() {
char* tempExpression = "[2 + (1 - 3)] * 4";
bool tempMatch = bracketMatching(tempExpression, 17);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
tempExpression = "( [ ] )";
tempMatch = bracketMatching(tempExpression, 6);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
tempExpression = "()(])7*8+9[(()])";
tempMatch = bracketMatching(tempExpression, 8);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
tempExpression = "({}[])";
tempMatch = bracketMatching(tempExpression, 6);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
tempExpression = ")(";
tempMatch = bracketMatching(tempExpression, 2);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
}
9、 main函数
void main(){
pushpopTest();
bracketMatchingTest();
}
运行结果:
-------测试开始-------
创建空栈成功!
初始化后,栈堆是:
Pushing 0.
0
Pushing 1.
0 1
Pushing 2.
0 1 2
Pushing 3.
0 1 2 3
Pushing 4.
0 1 2 3 4
Pushing 5.
0 1 2 3 4 5
Pushing 6.
0 1 2 3 4 5 6
Pushing 7.
0 1 2 3 4 5 6 7
Pushing 8.
0 1 2 3 4 5 6 7 8
Pushing 9.
0 1 2 3 4 5 6 7 8 9
Pushing 10.
空间已满,不能推送元素!
0 1 2 3 4 5 6 7 8 9
Pop 0.
0 1 2 3 4 5 6 7 8
Pop 1.
0 1 2 3 4 5 6 7
Pop 2.
0 1 2 3 4 5 6
Pop 3.
0 1 2 3 4 5
Pop 4.
0 1 2 3 4
Pop 5.
0 1 2 3
Pop 6.
0 1 2
Pop 7.
0 1
Pop 8.
0
Pop 9.
Pop 10.
栈堆为空,无法弹出元素!
Pop 11.
栈堆为空,无法弹出元素!
-------测试结束-------
Is the expression '[2 + (1 - 3)] * 4' bracket matching? 1
Is the expression '( [ ] )' bracket matching? 0
Is the expression '()(])7*8+9[(()])' bracket matching? 0
Is the expression '({}[])' bracket matching? 1
Is the expression ')(' bracket matching? 0