3.2链式队列的表示与实现

3.2链式队列的表示与实现

使用链栈和链队进行字符串回文(对称)判断。

代码实现:

test.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef char DataType;
//链式堆栈
typedef struct snode {
	DataType data;
	struct snode* next;
}LSNode;
//只有队尾的链式循环队列
typedef struct QNode {
	DataType data;
	struct QNode* next;
}LQNode,*LinkQueue;

//链栈初始化
void InitStack(LSNode **head) 
{
	if (!(*head=(LSNode *)malloc(sizeof(LSNode))))
	{
		printf("分配结点不成功!");
		exit(-1);
	}
	else
	{
		(*head)->next = NULL;
	}
}

//链栈判空
int StackEmpty(LSNode* head)
{
	if (head->next==NULL)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

//链栈进栈
int PushStack(LSNode* head, DataType e)
{
	LSNode* s;
	if (!(s=(LSNode *)malloc(sizeof(LSNode))))
	{
		exit(-1);
	}
	else
	{
		s->data = e;
		s->next = head->next;
		head->next = s;
		return 1;
	}
}

//链栈出栈
int PopStack(LSNode *head,DataType *e)
{
	LSNode* s = head->next;
	if (StackEmpty(head))
	{
		return 0;
	}
	else {
		head->next = s->next;
		*e = s->data;
		free(s);
		return 1;
	}
}

//循环链队初始化
void InitQueue(LinkQueue *rear)
{
	if (!(*rear=(LQNode *)malloc(sizeof(LQNode))))
	{
		exit(-1);
	}
	else {
		(*rear)->next = *rear;
	}
}

//循环链队的判空
int QueueEmpty(LinkQueue rear)
{
	if (rear->next==rear)
	{
		return 1;
	}
	else {
		return 0;
	}
}

//循环链队的入队
int EnQueue(LinkQueue *rear,DataType e)
{
	LQNode* s;
	s = (LQNode *)malloc(sizeof(LQNode));
	if (!s)
	{
		exit(-1);
	}
	s->data = e;
	s->next = (*rear)->next;
	(*rear)->next = s;
	*rear = s;
	return 1;
}


//循环链队的出队
int DeQueue(LinkQueue* rear, DataType* e)
{
	LQNode* f, * p;
	if (*rear==(*rear)->next)
	{
		return 0;
	}
	else
	{
		f = (*rear)->next;
		p = f->next;
		if (p==*rear)
		{
			*rear = (*rear)->next;
			(*rear)->next = *rear;
		}
		else
		{
			f->next = p->next;
		}
		*e = p->data;
		free(p);
		return 1;
	}
}

void main()
{
	LinkQueue LQueue1, LQueue2;
	LSNode* LStack1, * LStack2;
	char str1[] = "ABCDCBA";
	char str2[] = "ABCBCAB";
	char q1, s1, q2, s2;
	int i;
	InitQueue(&LQueue1);
	InitQueue(&LQueue2);
	InitStack(&LStack1);
	InitStack(&LStack2);
	for ( i = 0; i < strlen(str1); i++)
	{
		EnQueue(&LQueue1, str1[i]);
		EnQueue(&LQueue2, str2[i]);
		PushStack(LStack1,str1[i]);
		PushStack(LStack2,str2[i]);
	}
	printf("字符序列1:\n");
	printf("出队序列  出栈序列\n");
	while (!StackEmpty(LStack1))
	{
		DeQueue(&LQueue1, &q1);
		PopStack(LStack1, &s1);
		printf("%5c", q1);
		printf("%10c\n",s1);
		if (q1!=s1)
		{
			printf("字符序列1不是回文!!!\n");
			return;
		}
	}
	printf("字符序列1是回文!!!\n");

	printf("字符序列2:\n");
	printf("出队序列  出栈序列\n");
	while (!StackEmpty(LStack2))
	{
		DeQueue(&LQueue2, &q2);
		PopStack(LStack2, &s2);
		printf("%5c", q2);
		printf("%10c\n", s2);
		if (q2 != s2)
		{
			printf("字符序列2不是回文!!!\n");
			return;
		}
	}
	printf("字符序列2是回文!!!\n");
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值