设单链表中存放着n个字符,设计算法,判断该字符串中是否有中心对称关系
#include<iostream.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define OK 1
typedef struct node
{
char data;
struct node *next;
} *linklist;
typedef struct{
char *base;
char *top;
int stacksize;
}SqStack;
void CreateList(linklist &L,int n)
{
L=(linklist)malloc(sizeof(node));
L->next=NULL;
for(int i=n;i>0;--i){
struct node *p;
p=(linklist)malloc(sizeof(node));
cin>>p->data;
p->next=L->next;
L->next=p;
}
}
int InitStack(SqStack &S)
{
S.base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));
// S.base=new char[];
if(!S.base) exit(0);//内存分配失败
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
void ListPush(SqStack S,linklist L)
{
struct node *p;
InitStack(S);
p=L->next;
while(p)
{
*S.top=p->data;
p=p->next;
S.top++;
}
}
int judge(SqStack S,linklist L,int n)
{
int m=0;//m用来计算比较的次数
struct node *p;
p=L->next;
while(p)
{
if(*S.top==p->data)
{
p=p->next;
S.top--;
m++;
//return 1;
}
else
{
break;
}
//return 0;
//return m==n?1:0;
}
return m;//我返回m主要是看在比较过程中m有没有变化,单m总是不变
// cout<<m<<endl;
}
void main()
{
int n,H;
SqStack S;
linklist L;
cout<<"输入要判断的字符串个数n"<<endl;
cin>>n;
cout<<"输入n个字符"<<endl;
CreateList(L, n);
InitStack(S);
ListPush(S, L);
// judge(S,L,n);
H=judge(S,L,n);
cout<<H<<endl;
if(H)
cout<<"字符串中心对称"<<endl;
else
cout<<"字符串中心不对称"<<endl;
}
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define OK 1
typedef struct node
{
char data;
struct node *next;
} *linklist;
typedef struct{
char *base;
char *top;
int stacksize;
}SqStack;
void CreateList(linklist &L,int n)
{
L=(linklist)malloc(sizeof(node));
L->next=NULL;
for(int i=n;i>0;--i){
struct node *p;
p=(linklist)malloc(sizeof(node));
cin>>p->data;
p->next=L->next;
L->next=p;
}
}
int InitStack(SqStack &S)
{
S.base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));
// S.base=new char[];
if(!S.base) exit(0);//内存分配失败
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
void ListPush(SqStack S,linklist L)
{
struct node *p;
InitStack(S);
p=L->next;
while(p)
{
*S.top=p->data;
p=p->next;
S.top++;
}
}
int judge(SqStack S,linklist L,int n)
{
int m=0;//m用来计算比较的次数
struct node *p;
p=L->next;
while(p)
{
if(*S.top==p->data)
{
p=p->next;
S.top--;
m++;
//return 1;
}
else
{
break;
}
//return 0;
//return m==n?1:0;
}
return m;//我返回m主要是看在比较过程中m有没有变化,单m总是不变
// cout<<m<<endl;
}
void main()
{
int n,H;
SqStack S;
linklist L;
cout<<"输入要判断的字符串个数n"<<endl;
cin>>n;
cout<<"输入n个字符"<<endl;
CreateList(L, n);
InitStack(S);
ListPush(S, L);
// judge(S,L,n);
H=judge(S,L,n);
cout<<H<<endl;
if(H)
cout<<"字符串中心对称"<<endl;
else
cout<<"字符串中心不对称"<<endl;
}
...全文
相关推荐
数据结构实验
第一题:栈的应用 设单链表中存放着 n 个字符,试编写算法,判断该字符串是否有中心对称关系,如 aba,xyzzyx 都是中序对称的字符串。 第二题:队列的算法 假设以带头结点的循环链表表示队列,并且只设一个指针指向队尾(注意不设头指针),试编写相应的置空队、入队、出队的算法。设队列中存放着 n 个字符,试编写算法,判断该字符串是否有中心对称关系,如 aba,xyzzyx 都是中序对称的字符串。
判断字符串是否中心对称
本程序判断字符串是否中心对称,绝对可用!
编写算法,利用栈判断所给字符是否具有中心对称关系。要求有尽可能少的时间完成判断。
编写算法,利用栈判断所给字符是否具有中心对称关系。要求有尽可能少的时间完成判断。
编写算法,利用栈判断所给字符串是否具有中心对称关系(回文).doc
编写算法,利用栈判断所给字符串是否具有中心对称关系(回文).doc
中心对称字符串问题.pdf
中心对称字符串问题.pdf



