15,440
社区成员
发帖
与我相关
我的任务
分享
#include<queue>
#include<stdio.h>
using namespace std;
int main(){
queue<int>s;
int h,i;
for(h=0;h<10;h++){
scanf("%d",&i);
s.push(i);
}
for(h=0;h<5;h++){
printf("%d",s.front());
s.pop();
}
printf("%d %d %d",s.front(),s.back(),s.size());
h=0;
while (!s.empty())
{
printf("%d",s.front());
s.pop();
h++;
}
printf("%d",h);
return 0;
}
叶酱教我的实验一。bool Ispalindrome(SqStack *&s){
SqStack *q;
InitStack(q);
int i=0;
char e;
while(StackEmpty(s)){
GetTop(s,e);
Pop(s,e);
Push(q,e);
}
for(i=0;i<=MaxSize;i++){
if(q->data[i]!=s->data[i])
break;
}
if(i!=MaxSize)
return true;
else
return false;
}
InitStack(q):为函数的初始化
GetTop(s,e)取栈顶的元素,并赋值给e
根据的栈的“后进先出“原则,进行退栈操作,并将退栈元素进栈q,后比较q与s即可
bool palindrome( char *s )
{
int n,i;
n=strlen(s)-1;
for(i=0;i<=n/2;i++)
if(s[i]!=s[n-i])
return false;
return true;
}
用数组模拟栈,前一半字符进栈保存,在一个个出栈与后半字符进行比较
bool palindrome(char* s)
{
int mid, top = -1, i = 0;
char xn[200] = { 0 };
mid = strlen(s) / 2;
while (i != mid)
{
top++;
xn[top] = s[i];
i++;
}
if (strlen(s) % 2 == 1 && strlen(s) != 1)
i++;
while (i != strlen(s) - 1)
{
if (xn[top] != s[i])
return false;
else
{
top--;
i++;
}
}
return true;
}