一个长度为n的串采用大小为4的块链结构存储,设计算法判断给定的串是否中心对称。
对于这个题目我有两个思路一是用顺序表,每次申请单位为4的一个空间。
代码如下:
//此代码为队列操作改的,有些注释和函数请忽略;
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;
#define MAX 20
typedef char* Type;
char e[4];
char s;
typedef struct {
Type *base;
int front;
int rear;
int tag;
int count=0;
}Sq;
void InitSq(Sq &QQ);
int Getlen(Sq QQ);
int Isempty(Sq QQ);
void InSq(Sq &QQ);
void OutSq(Sq &QQ);
void GetHead(Sq QQ);
void OutSq_one(Sq &QQ);
int main()
{
int len,index=0;
char a[20*4];
Sq Q;
InitSq(Q);
//input
printf("please input the number of Q\n");
cin>>len;
getchar();
for(int i=0;i<len/4+1;i++)
{
scanf("%s",e); //必须四个一组输入,每次敲回车,剩下不足四个也一块输入,以回车结束
InSq(Q);
}
//judge
for(int i=0;i<(len/2);i++)
{
OutSq_one(Q);
a[index++]=s;
}
if(len%2!=0) //如果是奇数就弹出中间那个
OutSq_one(Q);
while(--index>=0)
{
OutSq_one(Q);
if(s!=a[index])
{
printf("NO\n");
return 0;
}
}
printf("YES\n");
return 0;
}
void OutSq_one(Sq &QQ)
{
if(QQ.front == QQ.rear && QQ.tag==0)
{
printf("the queue is empty\n");
return;
}
s=QQ.base[QQ.front][QQ.count++];
if(QQ.count==4)
{
QQ.count=0;
free(QQ.base[QQ.front]);
// e=Q.base[Q.front];
QQ.front=(QQ.front+1)%MAX;
if(QQ.front == QQ.rear)
QQ.tag=0;
}
}
void InitSq(Sq &QQ)
{
QQ.base=(Type *)malloc(MAX * sizeof(Type));
if(!QQ.base)
return ;
QQ.front=QQ.rear=QQ.tag=QQ.count=0;
}
int Getlen(Sq QQ)
{
if(!QQ.tag) //分情况讨论,tag==0,长度肯定为0;
return 0;
else
{
if(QQ.rear==QQ.front)
return MAX;
return (QQ.rear-QQ.front+MAX)%MAX;
}
}
int Isempty(Sq QQ)
{
if(QQ.front==QQ.rear && QQ.tag==0)
return 1;
return 0;
}
void InSq(Sq &QQ)
{
if (QQ.front == QQ.rear && QQ.tag==1) //tag与队头队尾必须同时满足
{
printf("%d\n",QQ.tag);
printf("the queue is full\n");
return ;
}
QQ.tag=1;
// Q.base=(Type )malloc(MAX * sizeof(Type));
QQ.base[QQ.rear]=(Type )malloc(4 * sizeof(Type));
strcpy(QQ.base[QQ.rear],e);
// Q.base[Q.rear] = e;
QQ.rear =(QQ.rear+1)%MAX;
}
void OutSq(Sq &QQ)
{
if(QQ.front == QQ.rear && QQ.tag==0)
{
printf("the queue is empty\n");
return;
}
strcpy(e,QQ.base[QQ.front]);
free(QQ.base[QQ.front]);
// e=Q.base[Q.front];
QQ.front=(QQ.front+1)%MAX;
if(QQ.front == QQ.rear)
QQ.tag=0;
}
void GetHead(Sq QQ)
{
if(QQ.front == QQ.rear && QQ.tag==0) return;
strcpy(e, QQ.base[QQ.front]);
// e = Q.base[Q.front];
}
思路二,在单链表的每一个节点中的数据域改为data[4];
代码如下:
//个别注释请忽略
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
typedef char ElemType;
typedef struct LNode{
ElemType data[4];
struct LNode *next;
}LNode,*LinkList;
char s;
int count;
void print(LinkList &head) {
if(head == NULL)
return;
LNode* node = head->next;
printf("numbers of L\n");
for(int i = 1; node != NULL; i++) {
// puts(node->data);
printf("%s",node->data);
node = node->next;
}
}
LinkList crea_last(LinkList &L,int n)
{
L = new LNode;
L->next = NULL;
LNode *endNode = L;
for(int i=0;i<n;i++)
{
LNode *p=new LNode;
gets(p->data);
p->next=NULL;
endNode->next = p;
endNode = p;
}
print(L);
return L;
}
void Out(LinkList &L)
{
LinkList p;
p=L->next;
s=p->data[count];
count++;
if(count==4)
{
count=0;
L->next=p->next;
free(p);
}
}
int main()
{
int n=0;
int index=0;
LNode *head;
char a[100];
count=0;
printf("\t\tplease input the number of L: ");
scanf("%d",&n);
getchar();
LinkList L = crea_last(head,n/4+1);
for(int i=0;i<n/2;i++)
{
Out(L);
a[index++]=s;
}
if(n%2!=0)
Out(L);
printf("\n");
for(int i=index-1;i>=0;i--)
{
Out(L);
if(a[i]!=s)
{
printf("NO\n");
return 0;
}
}
printf("OK\n");
return 0;
}