一、 实验目的
1. 熟悉栈的顺序和链式存储结构
2. 掌握栈的基本运算
3. 能够利用栈的基本运算完成栈应用的运算
二、 实验内容
1. 设单链表中存放有n个字符,试编写算法,判断该字符串是否有中心对称的关系,例如xyzzyx是中心对称的字符串。(提示:将单链表中的一半字符先依次进栈,然后依次出栈与单链表中的另一半字符进行比较。)
//seqstack.h
//本文件为第一题的头文件,包含node(节点),linklist(单链表),seqstack(顺序栈)三个类
#include<iostream>
using namespace std;
template<class T>
struct node //node类
{
T data;
node<T> * next;
};
template<class T> //linklist类
class linklist
{
private:
node<T> * first; //单链表的头指针
node<T> * p;
public:
linklist(); //无参数构造函数
linklist(T a[],int n); //有参数构造函数,元素数量为n
~linklist(){} //析构函数
int length(); //求长度
T get(int i); //得到第i个元素的值
int locate(T x); //查找值为x的元素的序号
void insert(int i,T x); //在位置i插入x
T Delete(int i); //删除第i个节点
void printlist(); //显示元素
};
template<class T>
linklist<T>::linklist() //无参数构造函数
{
first=new node<T>;
p= new node<T>;
first->next=NULL;
p=first->next;
}
template<class T>
linklist<T>::linklist(T a[],int n)//有参数构造函数,元素数量为n
{
node<T> *s;
first=new node<T>;
p= new node<T>;
first->next=NULL;
for(int i=0;i<n;i++)
{
s=new node<T>;
s->data=a[i];
s->next=first->next;
first->next=s;
}
}
template<class T>
int linklist<T>::length()//求长度
{
int count=0;
p=first->next;
while(p!=NULL)
{
p=p->next;
count++;
}
return count;
}
template<class T>
T linklist<T>::get(int i)//得到第i个元素的值
{
int count=1;
p=first->next;
while(p!=NULL&&count<i)
{
p=p->next;
count ++;
}
return p->data;
}
template<class T>
int linklist<T>::locate(T x)//查找值为x的元素的序号
{
int count=1;
p=first->next;
while(p!=NULL)
{
if(p->data==x)return count;
p=p->next;
count++;
}
return 0;
}
template<class T> //在位置i插入x
void linklist<T>::insert(int i,T x)
{
int count=0;
p=first;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
s=new node<T>;
s->data=x;
s->next=p->next;
p->next=s;
}
template<class T> //删除位置i
T linklist<T>::Delete(int i)
{
p=first;
int count=0;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
q=new node;int x;
q=p->next;
x=q->data;
p->next=q->next;
delete q;
return x;
}
template<class T> //输出
void linklist<T>::printlist()
{
p=first->next;
while(p!=NULL)
{
cout<<p->data;
p=p->next;
}
}
const int size=10; //size为顺序栈的存储大小
template<class T>
class seqstack //seqstack类
{
private:
T data[size];
int top;
public:
seqstack(){top=-1;}
~seqstack(){}
void push(T x); //入顺序栈
T pop(); //出顺序栈
int hui(T a[],int n); //判断字符串是不是对称
int dui(linklist<T> a); //判断单链表的内容是不是对称
};
template<class T>
void seqstack<T>::push(T x) //入顺序栈
{
data[++top]=x;
}
template<class T>
T seqstack<T>::pop() //出顺序栈
{
T x;
x=data[top--];
return x;
}
template<class T>
int seqstack<T>::hui(T a[],int n) //判断字符串是不是对称
{
for(int i=0;i<n/2;i++)
{push(a[i]);}
for(int j=0;j<n/2;j++)
{if(pop()!=a[n/2+1+j])return 0;}
return 1;
}
template<class T>
int seqstack<T>::dui(linklist<T> a) //判断单链表的内容是不是对称
{
int n=a.length();
for(int i=1;i<=n/2;i++)
{push(a.get(i));}
for(int j=1;j<=n/2;j++)
{if(pop()!=a.get(n-n/2+j))return 0;}
return 1;
}
//main.cpp
#include<iostream>
#include"seqstack.h"
using namespace std;
int main()
{
char test[10]="xxyyxx"; //测试字符串
seqstack<char> m; //顺序栈
linklist<char> a(test,6); //生成内容为测试字符串的单链表
int b;
b=m.dui(a); //判断单链表是不是对称
if(b==1)cout<<test<<"对称"<<endl; //输出结果
else cout<<test<<"不对称"<<endl;
return 0;
}