课程作业五
对栈的学习
定义
- 操作的特殊性: 栈必须按"后进先出"的规则进行操作
- 数据读取:栈只能从头部取数据 也就最先放入的需要遍历整个栈最后才能取出来,而且在遍历数据的时候还得为数据开辟临时空间,保持数据在遍历前的一致性。
- 特点:栈(Stack)是限定只能在表的尾部一端进行插入和删除操作的线性表。
成员函数:
- push(a)//将元素a压入栈顶
- pop()//删除栈顶元素
- top()//返回栈顶元素
- size()//返回栈中元素的个数
- empty()//如果栈为空则返回true 否则返回false
对栈的使用
- 上次pta有一道题目判断回文串的可以用栈的知识解决。代码如下
#include "stdio.h"
char stack[100];
int top;
void initstack()
{
top=0;
}
bool isempty()
{
return top==0;
}
bool isfull()
{
return top>=100;
}
bool push(char x)
{
if(isfull()) return false;
stack[top++]=x;
return true;
}
bool pop(char &x)
{
if(isempty()) return false;
x=stack[--top];
return true;
}
int main()
{
char str[81],c;
int i=0;
while((c=getchar())!='@')
{
str[i++]=c;
push(c);
}
i=0;
while(!isempty())
{
pop(c);
if(c!=str[i++])
{
printf("No");
return 0;
}
}
printf("Yes");
}
第四次作业代码