第一章实验目的
1.掌握线性表的链式存储结构的表示和实现方法。
2.掌握单链表基本操作的算法实现。
3.了解单链表的应用。
第二章 实验环境
硬件环境要求:
PC机(单机)
使用的软件名称、版本号以及模块:
DEV C++、VS2010等。
第三章 实验内容
本实验主要介绍常见的数据结构Stack。
第四章实验步骤
#include <iostream>
#include <string>
using namespace std;
#define ERROR 0
#define OK 1
#define OVERFLOW -2
#define MAXSIZE 100
typedef char SElemType;
typedef struct {
SElemType* base;
SElemType* top;
int stacksize;
}SqStack;
int InitStack(SqStack& S); //构造一个空栈
bool StackEmpty(SqStack& S); //判断栈是否为空
SElemType GetTop(SqStack& S); //得到栈顶元素
int Push(SqStack& S, SElemType e); //入栈
int Pop(SqStack& S, SElemType e); //出栈
bool Match(SqStack& S); //判断括号是否合法
int main()
{
SqStack s;
InitStack(s);
Match(s) ? cout << "括号合法!" << endl : cout << "括号不合法!" << endl;
return 0;
}
int InitStack(SqStack& S)
{
S.base = new SElemType[MAXSIZE];
if (!S.base) exit(OVERFLOW);
S.top = S.base;
S.stacksize = MAXSIZE;
return OK;
}
bool StackEmpty(SqStack& S)
{
return S.top == S.base ? true : false;
}
SElemType GetTop(SqStack& S)
{
if (S.top != S.base)
return *(S.top - 1);
return ERROR;
}
int Push(SqStack& S, SElemType e)
{
if (S.top - S.base == S.stacksize)
return ERROR;
*S.top++ = e;
return OK;
}
int Pop(SqStack& S, SElemType e)
{
if (S.top == S.base)
return ERROR;
e = *--S.top;
return OK;
}
bool Match(SqStack& S)
{
int flag = 1;
string str;
cin >> str;
for (auto c : str)
{
if (c == '(')
Push(S, c);
else
{
if (!StackEmpty(S) && GetTop(S) == '(')
Pop(S, '(');
else
{
flag = 0;
break;
}
}
}
if (StackEmpty(S) && flag)
return true;
return false;
}
运行结果:
输入:()
输入:(((())))
第五章实验感悟
我觉得数据结构很难。