数据结构学习---利用栈实现行编辑程序和括号匹配算法

相关头文件及声明

首先为了在C++实现栈的存储和使用,应当先对栈与其基本函数做一些定义。

c1.h 引入一些最基本的头文件并定义函数结果状态代码

 #include<string.h>
 #include<ctype.h>
 #include<malloc.h> // malloc()等
 #include<limits.h> // INT_MAX等
 #include<stdio.h> // EOF(=^Z或F6),NULL
 #include<stdlib.h> // atoi()
 #include<io.h> // eof()
 #include<math.h> // floor(),ceil(),abs()
 #include<process.h> // exit()
 #include<iostream> // cout,cin
 // 函数结果状态代码
 #define TRUE 1
 #define FALSE 0
 #define OK 1
 #define ERROR 0
 #define INFEASIBLE -1
 // #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行
 typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
 typedef int Boolean; // Boolean是布尔类型,其值是TRUE或FALSE

c1-1.h 针对栈做一些特殊定义和声明

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef char SElemType;

typedef struct {
	SElemType *base;
	SElemType *top;
	int stacksize;
}SqStack;

function.h相关函数的声明

Status InitStack(SqStack &S);

Status DestroyStack(SqStack &S);

Status ClearStack(SqStack &S);

Boolean StackEmpty(SqStack &S);

Status GetTop(SqStack S, SElemType &e);

Status Push(SqStack &S, SElemType e);

Status Pop(SqStack &S, SElemType &e);

Status StackTraverse(SqStack S, void(*visit)(SElemType *loc));

void parenthesis();

void LineEdit();

function.cpp 栈的相关函数实现

using namespace std;
#include"c1.h"
#include"c1-1.h"

void prints(SElemType *loc);

SElemType* p;
Status InitStack(SqStack &S) {
	S.base = (SElemType * )malloc(STACK_INIT_SIZE * sizeof(SElemType));
	if (!S.base) exit(OVERFLOW);
	S.top = S.base;  //set empty stack
	S.stacksize = STACK_INIT_SIZE;
	return OK;
}

Status DestroyStack(SqStack &S) {
	free(S.base);
	return OK;
}

Status ClearStack(SqStack &S) {
	if(S.top == S.base) exit(ERROR);
	S.top = S.base;
	return OK;
}

Boolean StackEmpty(SqStack &S) {
	if(S.top == S.base) return TRUE;
	return FALSE;
}

Status GetTop(SqStack S, SElemType &e) {
	if (S.top == S.base) return ERROR;
	e = *(S.top-1);
	return OK;
}

Status Push(SqStack &S, SElemType e) {
	if (S.top - S.base >= S.stacksize) {
		S.base = (SElemType * )realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
		if (!S.base) exit(OVERFLOW);
		S.top = S.base + S.stacksize;
		S.stacksize += STACKINCREMENT;
	}
	*S.top++ = e;
	return OK;
}

Status Pop(SqStack &S, SElemType &e) {
	if (S.top == S.base) return ERROR;
	e = *--S.top;
	return OK;
}

Status StackTraverse(SqStack S, void(*visit)(SElemType *loc)){
	if (S.top == S.base) return ERROR;
	for (p = S.base; p != S.top; p++) visit(p);
	return OK;
}

行编辑程序的实现

void LineEdit() {
	SqStack S;
	InitStack(S);
	char ch = getchar();
	while (ch != EOF) {
		while (ch != EOF && ch != '\n') {
			switch (ch) {
				case '#': Pop(S, ch); break;
				case '@': ClearStack(S); break;
				default : Push(S, ch); break;
			}
			ch = getchar();
		}
		if(StackTraverse(S, prints)) {
		cout<<endl;
		ClearStack(S);
	}
		if (ch != EOF) ch = getchar();
	}
	DestroyStack(S);
}

括号匹配算法的实现

void parenthesis() {
	SqStack S1;
	char n, e;
	InitStack(S1);
	cout<<"Input the parentheses:";
	while(cin>>n) {
		if(n == '[' || n == '{') Push(S1, n);
		else {
			if(!GetTop(S1, e))  goto part1;
			if(n == ']' && e == '[') Pop(S1, e);
			else if(n == '}' && e == '{') Pop(S1, e);
			else goto part1;
		}
	}
	cout<<endl;
	if(StackEmpty(S1) )cout<<"Matched"<<endl<<endl;
	else 
	part1:
	cout<<"Not Matched"<<endl<<endl;
}

bo1-1.h 辅助函数

using namespace std;
#include"c1.h"
#include"c1-1.h"

void prints(SElemType *loc){
	cout<<*loc;
}

main.cpp 主函数进行运行测试

using namespace std;
#include"c1.h"
#include"c1-1.h"
#include"Function.h"


int main() {
	
	LineEdit();
	parenthesis();
	
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值