顺序栈的实现和c++模板

stack栈的基本操作:
q.push(); 向栈压入一个元素
q.top(); 返回栈顶元素
q.pop(); 删除栈顶元素,无返回值
q.size(); 返回栈中元素的个数

q.empty(); 判断栈是否为空,真则返回true,假则返回false


顺序栈判断回文

#include 
#include 
#include 
using namespace std;
int main(){
	char jk[100], st[100];
	int len, i, top, mid;
	scanf("%s", jk+1);
	len = strlen(jk+1);
	mid = len/2;
	top = 0;
	for(i = 1; i <= mid; ++i)
		st[++top] = jk[i];
	if(len%2 == 0) ++mid;
	else mid += 2;
	for(i = mid; i <= len; ++i){
		if(top <= 0 || st[top] != jk[i]) break;
		--top;
	}
	if(top == 0 && i == len+1) printf("YES\n");
	else printf("NO\n");
	return 0;
}



c++模板判断回文

#include 
#include 
#include 
#include 
using namespace std;
int main(){
	char jk[100];
	stack st;
	int len, i, mid;
	scanf("%s", jk+1);
	len = strlen(jk+1);
	mid = len/2;
	for(i = 1; i <= mid; ++i)
		st.push(jk[i]);
	if(len%2 == 0) ++mid;
	else mid += 2;
	for(i = mid; i <= len; ++i){
		if(st.empty() || st.top() != jk[i]) break;
		st.pop();
	}
	if(st.empty() && i == len+1) printf("YES\n");
	else printf("NO\n");
	return 0;
}

加油~

你可能感兴趣的:(杂杂的)