Ⅰ 用數據結構的棧和隊列 寫 迴文判斷
用棧實現了判斷迴文數的操作,即把字元串依次入棧,然後出棧並依次和字元數組比較是否相等,從而判斷字元序列是否迴文數,代碼如下:
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#defineEMPTY0
#defineFULL10000
#defineMAX10000
typedefchardata;
typedefstructelem{
datad;
structelem*next;
}elem;
typedefstructstack{
intcnt;
elem*top;
}stack;
voidinitialize(stack*stk);
voidpush(datad,stack*stk);
datapop(stack*stk);
boolempty(conststack*stk);
boolfull(conststack*stk);//棧操作函數
voidinitialize(stack*stk)
{
stk->cnt=0;
stk->top=NULL;
}
boolempty(conststack*stk)
{
returnstk->cnt==EMPTY;
}
boolfull(conststack*stk)
{
returnstk->cnt==FULL;
}
voidpush(datad,stack*stk)
{
elem*p;
if(!full(stk))
{
p=(elem*)malloc(sizeof(elem));
p->d=d;
p->next=stk->top;
stk->top=p;
stk->cnt++;
}
}
datapop(stack*stk)
{
datad;
elem*p;
if(!empty(stk))
{
d=stk->top->d;
p=stk->top;
stk->top=stk->top->next;
stk->cnt--;
free(p);
}
returnd;
}
intmain(void)
{
datainput[MAX];
stacktemp;
inti=0;
intflag=0;
initialize(&temp);//初始化臨時棧
scanf("%s",&input);//輸入字元串
while(input[i]!='@')
{//字元串入棧
push(input[i],&temp);
i++;
}
while(!empty(&temp))
{//字元依次出棧和字元數組比較,判斷是否迴文數
if(temp.top->d==input[flag])
{
pop(&temp);
flag++;
}
else
{
printf("此字元序列不是迴文數! ");
break;
}
}
if(empty(&temp))
printf("此字元序列是迴文數! ");
return1;
}
運行結果:
Ⅱ 用C語言編譯數據結構的迴文問題。。在線等。急
教你個簡單的方法,不管是迴文的字元串還是數字都可以用。
拿字元串舉例:abccba,從前往後讀是abccba,那從後往前讀也是abccba,所以,將字元串逆轉,如果和原字元串一樣,那就是迴文的,如果不一樣,就不是迴文的。
比你用棧和隊列簡單多了,用棧和隊列還容易出錯。
逆轉字元串應該沒問題吧,有問題的話繼續M我。
Ⅲ 用PASCAL語言求迴文數問題
ls顯然錯。。比如1122,按照你的操作順序的話就是1進1出2進2出,最終也能得到空棧,但1122顯然不是迴文。。
這個是我寫的程序,因為不知道數據范圍,所以用了string,如果數據太大的話建議改成ansistring。。
var a:char; s1,s2:string;
begin
a:=#0;
while a<>'#' do
begin
read(a);
if a='#' then break;
s1:=s1+a; s2:=a+s2;
end;
if s1=s2 then writeln('It is a palin word') else writeln('It is not a palin word');
end.
這個程序就是比較正序和倒序是否相同,如果相同則為迴文,否則不為迴文。。
沒編譯過,直接在上面拍的,不過相信不會錯吧。。
Ⅳ 演算法與數據結構實驗:迴文判斷
#include<iostream>
using namespace std;
struct queue{
queue *front;
queue *rear;
queue *next;
char data;
};
#define maxsize 100;
#define T 10;
struct sqstack{
char *base;
char *top;
int stacksize;
};
sqstack inistack(sqstack &s){
s.base=new char [10];
if(!s.base) cout<<"error";
s.top=s.base;
s.stacksize=maxsize;
return s;
}
sqstack push(sqstack &s,char e){
if(s.top-s.base>=s.stacksize){
s.base=new char[10];
if(!s.base) cout<<"error";
s.top=s.base+s.stacksize;
s.stacksize+=T;
}
*s.top=e;
s.top++;
return s;
}
sqstack pop(sqstack &s, char e){
if(s.top==s.base)cout<<"error";
e=*s.top;
-- s.top;
return s;
}
queue iniqueue(queue &q){
q.front=q.rear=new queue;
if(!q.front) cout<<"error";
q.front->next=NULL;
return q;
}
queue inqueue(queue &q,char e){
queue *p;
p=new queue;
if(!p) cout<<"error";
p->data=e;
q.rear->next=p;
q.rear=p;
q.rear->next=NULL;
return q;
}
queue dlqueue(queue &q,char e){
queue* p;
p=new queue[10];
if(q.front==q.rear)cout<<"error";
else{
p=q.front->next;
e=p->data;
q.front->next=p->next;
if(q.rear==p)q.rear=q.front;
delete p;
}
return q;
}
char gethead(queue &q){
char e;
if(q.rear==q.front) cout<<"error";
else e=(q.front->next)->data;
return e;
}
char gettop(sqstack &s){
char e;
if(s.top==s.base){
cout<<"error";
}
e=*(s.top-1);
return e;
}
int main(){
int i;
sqstack s;
queue h;
char ch[5] ,x,y;
inistack(s);
iniqueue(h);
for(i=1;i<=5;i++){
cin>>ch[i];
push(s,ch[i]);
inqueue(h,ch[i]);
}
for(int j=1;j<=5;j++){
x=gettop(s);
y=gethead(h);
if(x!=y) cout<<"\tfalse\t"<<endl;
else cout<<"\tequal\t"<<endl;
pop(s,x);
dlqueue(h,y);
}
system("pause");
return 0;
}
Ⅳ 用棧和隊列判斷迴文數(數據結構題)
#include<iostream> #include<stack> using namespace std; int main() { stack<char>S; int top,i; char q[100]; while(cin>>q) { top=-1; for(i=0;q[i]!='&';i++) { if(q[i]=='@') { cout<<"No"<<endl; break; } else { S.push(q[i]); ++top; } } if(q[i]!='@') { i++; while(q[i]!='@') { if(q[i]==S.top()&&q[i]!='&') { top--; S.pop(); i++; } else { cout<<"No"<<endl; break; } } if(top==-1&&q[i]=='@') cout<<"Yes"<<endl; } } return 0; }
Ⅵ C語言編迴文數
#include <stdio.h>
#include <stdlib.h>
int huiwen(char *str) /* 迴文子函數 */
{
int len=strlen(str);
int i=0;
for(i=0; i<len/2; i++) {
if(*(str+i) != *(str+len-1-i)) return 1;
}
return 0;
}
int main()
{
int i=0;
char str[5];
int hw6[10000] = {0};
int cnt=0;
int max=0;
printf("所有迴文數:\n");
for(i=9999; i>=100; i--) {
memset(str, 0, 5);
sprintf(str, "%d", i);
if(huiwen(str) == 0) {
printf("%d\n", i); /* 這里把所有迴文數列印出來 */
if(i % 6 == 0) {
hw6[cnt++] = i; /* 記錄下被6整除的迴文數 */
if(max == 0)
max = i; /* 最大被6整除的迴文數,只會被執行一次 */
}
}
}
printf("能被6整除的迴文數:\n");
for(i=0; i<cnt; i++) {
printf("%d\n", hw6[i]);
}
printf("最大迴文數: %d\n",max);
}
Ⅶ C語言 關於判斷迴文數的程序
1、首先打開vc6.0,新建一個控制台項目,添加頭文件。
Ⅷ 數據結構C語言版新手,請教啊~!
//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
typedef struct ls{
char d;
struct ls *next;
} LS;
LS *push_back(LS *a,char c)
{
LS *t;
if (a==NULL) {
a=malloc(sizeof(LS));
a->d=c;
a->next =NULL;
//return a;
}
else
a->next=push_back(a->next ,c);
return a;
}
LS *fz(LS *a)
{
LS *m,*l=NULL;
while (a!=NULL)
{
m=a;
a=a->next ;
m->next=l;
l=m;
}
return m;
}
int main(void)
{
LS *a=NULL,*b=NULL;
int i;
char c;
for (c=getchar(); c!='\n'; c=getchar()) {
if (c!=' ') {
a=push_back(a,c);
b=push_back(b,c);
}
}
b=fz(b);
while (a!=NULL)
{
LS *t;
if (a->d!=b->d) {
break;
}
t=a;
a=a->next;
free(t);
t=b;
b=b->next ;
free(t);
}
if (a!=NULL) {
printf("非迴文");
}
else printf("迴文");
return 0;
}
//---------------------------------------------------------------------------