首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一道数据结构 栈的有关问题

2013-10-15 
一道数据结构 栈的问题//设单链表中存放着n个字符,设计算法,判断该字符串中是否有中心对称关系。例如:xyzzy

一道数据结构 栈的问题

//设单链表中存放着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;       
}

[解决办法]
 S.top=S.base=0; 这个不是清空操作!

热点排行