一道数据结构 栈的问题
//设单链表中存放着n个字符,设计算法,判断该字符串中是否有中心对称关系。例如:xyzzyx、xyzyx都算是中心对称的字符串。
#include <iostream>
using namespace std;
typedef struct node
{
char data;
struct node *next;
}node,*Linklist;
typedef struct Sqstatic
{
char *base;
char *top;
int stacksize;
}Sqstatic;
void Init(Linklist &L,int n)
{
L=new node;
L->next=NULL;
node *a=L;
cout<<"输入字符"<<endl;
for(int i=0;i<n;i++)
{
node *p=new node;
cin>>p->data;
a->next=p;
p->next=NULL;
a=a->next;
}
}
void Initstack(Sqstatic &S)
{
S.base=new char[100];
S.top=S.base=0;
S.stacksize=100;
}
void Push(Sqstatic & S,char e)
{
*S.top=e;
S.top++;
}
char Pop(Sqstatic & S)
{
--S.top;
chare=*S.top;
return e;
}
int main()
{
Linklist head;
Sqstatic A;
int n;
cout<<"请输入链表的长度:"<<endl;
cin>>n;
Init(head,n);
node *a=head;
Initstack(A);
for(a=head->next;a!=NULL;a=a->next)
{
Push(A,a->data);
}
for(a=head->next;a!=NULL;a=a->next)
{
char m;
m=Pop(A);
if(a->data!=m)
{
cout<<"不是中心对称的"<<endl;
break;
}
}
if(a==NULL)
cout<<"是中心对称的"<<endl;
return 0;
}
{
S.base=new char[100];
S.top=S.base=0; ///你给一个指针赋0作甚? 改成 S.top=S.base;就OK了
S.stacksize=100;
}