1、編程序判斷一個字符序列是否是迴文,要求採用鏈式隊列和鏈式堆棧。
提示:設字符數組str中存放了要判斷的字符串。把字符數組中的字符逐個分別存入隊列和堆棧,然後逐個出隊列和退棧並比較出隊列的字符和退棧的字符是否相等,若全部相等則該字符序列是迴文,否則就不是迴文。
以下是程序部分代碼,請調試並補充使之正確運行。
1、隊列的頭文件
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct qnode
{
char data;
struct qnode *next;
} LQNode,*PLQNode;
typedef struct
{
LQNode *front; /*隊頭指針*/
LQNode *rear; /*隊尾指針*/
} LQueue,*PLQueue;
void QueueInitiate(LQueue *Q) /*初始化鏈式隊列Q*/
{
Q->rear = NULL; /*定義初始隊尾指針下標值*/
Q->front = NULL; /*定義初始隊頭指針下標值*/
}
void DestroyQ(LQueue Q)
{
LQNode *p, *p1;
p = Q.front;
while(p != NULL)
{
p1 = p;
p = p->next;
free(p1);
}
}
int QueueNotEmpty(LQueue Q)
/*判鏈式隊列Q非空否,非空則返回1,否則返回0*/
{
if(Q.front == NULL) return 0;
else return 1;
}
int QueueAppend(LQueue *Q, char x)
/*把數據元素值x插入鏈式隊列Q的隊尾,入隊列成功則返回1,否則返回0 */
{
LQNode *p;
if((p = (LQNode *)malloc(sizeof(LQNode))) == NULL)
{
printf("內存空間不足!");
return 0;
}
p->data = x;
p->next = NULL;
if(Q->rear != NULL) Q->rear->next = p;
Q->rear = p;
if(Q->front == NULL) Q->front = p;
return 1;
}
int QueueDelete(LQueue *Q, char *d)
/*刪除鏈式隊列Q的隊頭數據元素值到d ,出隊列成功則返回1,否則返回0*/
{
LQNode *p;
if(Q->front == NULL)
{
printf("隊列已空無數據元素出隊列! \n");
return 0;
}
else
{
*d = Q->front->data;
p = Q->front;
Q->front = Q->front->next;
if(Q->front == NULL) Q->rear = NULL;
free(p);
return 1;
}
}
int QueueGet(LQueue Q, char *d)
/*取鏈式隊列Q的當前隊頭數據元素值到d ,成功則返回1,否則返回0*/
{
if(Q.front == NULL)
{
printf("隊列已空無數據元素出隊列! \n");
return 0;
}
else
{
*d = Q.front->data;
return 1;
}
}
2、棧的頭文件
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct snode
{
char data;
struct snode *next;
} LSNode,*PLSNode;
void StackInitiate(LSNode **head)//傳入一個結構體指針的指針(棧的頭結點),爲棧開闢空間
/*初始化帶頭結點鏈式堆棧*/
{
if((*head = (LSNode *)malloc(sizeof(LSNode))) == NULL)
{
printf("沒有空間申請\n");
return;
}
(*head)->next = NULL;
}
int StackNotEmpty(LSNode *head)
/*判堆棧是否非空,非空返回1;空返回0*/
{
if(head->next == NULL) return 0;
else return 1;
}
int StackPush(LSNode *head, char x)
/*把數據元素x插入鏈式堆棧head的棧頂作爲新的棧頂 */
{
LSNode *p;
if((p = (LSNode *)malloc(sizeof(LSNode))) == NULL)
{
printf("內存空間不足無法插入! \n");
return 0;
}
p->data = x;
p->next = head->next; /*新結點鏈入棧頂*/
head->next = p; /*新結點成爲新的棧頂*/
return 1;
}
int StackPop(LSNode *head, char *d)
/*出棧並把棧頂元素由參數d帶回*/
{
LSNode *p = head->next;
if(p == NULL)
{
printf("堆棧已空出錯!");
return 0;
}
head->next = p->next; /*刪除原棧頂結點*/
*d = p->data; /*原棧頂結點元素賦予d*/
free(p); /*釋放原棧頂結點內存空間*/
return 1;
}
int StackTop(LSNode *head, char *d)
/*取棧頂元素並把棧頂元素由參數d帶回*/
{
LSNode *p = head->next;
if(p == NULL)
{
printf("堆棧已空出錯!");
return 0;
}
*d = p->data;
return 1;
}
void DestroyL(LSNode *head)
{
LSNode *p, *p1;
p = head;
while(p != NULL)
{
p1 = p;
p = p->next;
free(p1);
}
}
3、測試文件
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include"LinStack.h"
#include"LinQueue.h"
int i;
void HuiWen(char str[])
{
//第一步:將str依次存到棧中
PLSNode PStack = (PLSNode)malloc(sizeof(LSNode));
StackInitiate(&PStack);
for(i=0;str[i]!='\0'&&StackPush(PStack, str[i])==1;i++);
//第二步:將str依次存到隊列中
PLQueue PQ = (PLQueue)malloc(sizeof(LQueue));
QueueInitiate(PQ);
for(i=0;str[i]!='\0'&&QueueAppend(PQ, str[i])==1;i++);
//第三步:依次彈出比較
char c1,c2;
for(i=0;str[i]!='\0';i++){
QueueDelete(PQ,&c1);
StackPop(PStack,&c2);
if(c1!=c2){
printf("不是迴文數\n");
DestroyQ(*PQ);
DestroyL(PStack);
return;
}
}
printf("是迴文數\n");
DestroyQ(*PQ);
DestroyL(PStack);
}
void main(void)
{
char str1[]="abcdedcba";
char str2[]="ABCDEABCDE";
HuiWen(str1);
HuiWen(str2);
}