数据结构(队列)

文章介绍了队列的基本概念,包括队头、队尾以及先进先出(FIFO)原则。它展示了如何使用队列进行数字解码,以及利用双端队列来检查字符串是否为回文。通过示例代码,阐述了队列的插入、删除操作以及队列满和队列空的判断方法。
摘要由CSDN通过智能技术生成

目录

基本操作:

 队列应用(解码)

双端队列


队列(Queue)。队列简称队。是一种操作受限的线性表,只允许在表的一端进行插入,而在表的另一端进行删除。向队列中插入元素称为入队或进队;删除元素称为出队或离队。其操作特性为先进先出(First In First Out,FIFO),并且只允许在队尾进,队头出。

队头(Front):允许删除的一端,又称队首

队尾(Rear):允许插入的一端

空队列:不包含任何元素的空表

基本操作:

#include <stdio.h>

#define SIZE  100

char queue[SIZE];
int head = 0;
int tail = 0;

void enqueue(char c)
{
	queue[tail] = c;
	tail = (tail+1) % SIZE;
	
}

char dequeue()
{
	char c;
	c = queue[head];
	head = (head+1) % SIZE;

	return c;
}

char queue_full()
{
	
	return (tail+1)%SIZE == head;	
}
char  queue_empty()
{
	return head==tail;	
}


int main()
{
	char c;
	int i;
	c='A';
	for(i=0;i<3;i++){
	
		if(!(queue_full())){
			enqueue(c++);
		}
	}
	
	while(!(queue_empty())){
	
		putchar(dequeue());
	}

	printf("\n");

	return 0;
}

 队列应用(解码)

解码,对于一串数字,将第一个数字删除,第二个数字排至这串数字的末尾,将第三个数字删除,将第四个数字排至末尾,循环下去直到这串数字都删除了。删除的数字就是真正的密码。

#include <stdio.h>
#include <string.h>
#define SIZE  100

char queue[SIZE];
int head = 0;
int tail = 0;

void enqueue(char c)
{
	queue[tail] = c;
	tail = (tail+1) % SIZE;
	
}

char dequeue()
{
	char c;
	c = queue[head];
	head = (head+1) % SIZE;

	return c;
}

char queue_full()
{
	
	return (tail+1)%SIZE == head;	
}
char  queue_empty()
{
	return head==tail;	
}


int main()
{	int len;
	int i,num;
	
	char code[10];
	printf("Please enter the code:\n");	
	gets(code);
	len=strlen(code);
	
	for(i=0;i<len;i++){
		if(!queue_full()){
			enqueue(code[i]);
		}
	}
	i=0;
	while(!queue_empty()){
		if(!queue_empty()){
			code[i++]=dequeue();
		}
		if(!queue_full()){
			num=dequeue();
			enqueue(num);
		}
	
	}
	len=strlen(code);
	printf("The decoded code: ");
	for(i=0;i<len;i++){
		printf("%c",code[i]);
	}
	printf("\n");
	return 0;
}

双端队列

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



#define SIZE  100

char queue[SIZE];
int head = 0;
int tail = 0;

void tail_enqueue(char c)
{
	queue[tail] = c;
	tail = (tail+1) % SIZE;
	
}
char tail_dequeue()
{
	
	tail=(tail-1+SIZE) % SIZE;
	
	return queue[tail];
}

char head_dequeue()
{
	char c;
	c = queue[head];
	head = (head+1) % SIZE;

	return c;
}

void head_enqueue(char c)
{
	head=(head-1+SIZE)% SIZE;
	queue[head]=c;
}

char queue_full()
{
	
	return (tail+1)%SIZE == head;	
}
char  queue_empty()
{
	return head==tail;	
}

int palindrome(char *pt)
{
	int i,len;
	char c1,c2;
	len=strlen(pt);

	for(i=0;i<len;i++){
	
		if(!queue_full()){
			tail_enqueue(pt[i]);
		}
	}

	while(!queue_empty()){
		
		c1=head_dequeue();
		if(!queue_empty()){
		
			c2=tail_dequeue();
		}else{
			break;
		}

		if(c1 == c2){
			continue;
		}else{
			return 0;
		}

		
	}
	return 1;



}



int main()
{
	char str[100];
	printf("Please enter the palindrome number:");
	gets(str);
	
	if(palindrome(str)){
		printf("This is a palindrome!\n");
	}else{
		printf("This is not a palindrome!\n");
	}



	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈学弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值