数据结构:顺序队和链队(C语言)

队的特点是先进先出。

顺序队

#include<stdio.h>

typedef int XX;
#define MAXSIZE 8

typedef struct{
	XX *base;
	int top;
	int rear;
}queue;

//入队
void settop(queue &L,XX data){
	if((L.top+1)%MAXSIZE==L.rear){
		printf("队满了,无法入队。");
		return;
	}
	L.base[L.top] = data;
	L.top++;
	L.top=L.top%(MAXSIZE);
}

//出队
XX gettop(queue &L){
	if(L.rear==L.top){
		printf("队内没有元素。");
		return 0;
	}

	XX w;
	w=L.base[L.rear];
	L.rear++;
	L.rear=L.rear%MAXSIZE;
	return w;
}

//取对头元素
XX havetop(queue L){
	if(L.rear==L.top){
		printf("队内没有元素。");
		return 0;
	}
	XX w=L.base[L.rear];
	return w;
}
//显示队内元素
void appear(queue L){
	int i;
	i=L.rear;
	printf("队内元素为(先进的在前面):");
	while(i!=L.top){
		printf("%d\t",L.base[i]);
		i++;
		i=i%MAXSIZE;
	}
	printf("\n");
}

void main(){
	queue L;
	L.base=new XX[MAXSIZE+1];
	L.top=L.rear=0;
	printf("*********************************************\n");
	int size;
	XX data;
	printf("请输入您要入队的元素的个数:\n");
	scanf("%d",&size);
	printf("请输入入队元素:\n");
	for(int i=0;i<size;i++){
		scanf("%d",&data);
		settop(L,data);
	}
	printf("创建成功,");
	appear(L);
	printf("-------------------------------\n");
	int ii;
	printf("1入队\t2出队\t3取队头元素\t4显示队内元素\n");

	printf("请输入您想要的操作数:\n");
	scanf("%d",&ii);
	printf("-------------------------------\n");
	while(ii){
		if(ii==1){
			printf("请输入您想入队的元素:\n");
			scanf("%d",&data);
			settop(L,data);
			printf("\n入队成功,入队元素为:%d\n\n",data);
		}
		else if(ii==2) {
			XX hh=gettop(L);
			printf("\n出队成功,出队元素为:%d\n\n",hh);
		}
		else if(ii==3){
			XX hhh=havetop(L);
			printf("\n队头元素为:%d\n\n",hhh);
		}
		else if(ii==4){
			printf("\n");
			appear(L);
			printf("\n");
		}
		else
			break;
		printf("-------------------------------\n");
		printf("请输入您的下一步操作数字:\n");
		scanf("%d",&ii);
		printf("-------------------------------\n");
	}
}


链队

#include<stdio.h>
#include<stdlib.h>
typedef int XX;

typedef struct Lnode{
	XX data;
	struct Lnode *next;
}Lnode,*linkLnode;

typedef struct{
	linkLnode top;
	linkLnode tail;
}queue;


//入队
void settop(queue &L,XX data){
	Lnode *e;
	e=new Lnode;
	e->next=L.tail->next;
	L.tail->next=e;
	L.tail=e;
	e->data=data;
}

//出队
XX gettop(queue &L){
	Lnode *e;
	XX w;
	if(L.tail==L.top){
		printf("队内没有元素\n");
		return 0;}
	e=L.top->next;
	L.top->next=e->next;
	w=e->data;
	free(e);
	if(L.top->next==NULL)
		L.tail=L.top;
	return w;
}

//取队头
XX havetop(queue L){
	if(L.top==L.tail){
		printf("队内没有元素\n");
		return 0;}
	return L.top->next->data;
}


//显示队内元素
void appear(queue L){
	if(L.tail==L.top){
		printf("队为空。\n");
		return;}
	Lnode *e;
	e=L.top->next;
	printf("队内元素为:");
	while(e!=NULL){
		printf("%d\t",e->data);
		e=e->next;
	}
	printf("\n");
}

void main(){
	queue *L;
	L=new queue;
	L->top=L->tail=new Lnode;
	L->tail->next=NULL;
	printf("************************************\n");
	int size;
	XX data;
	printf("请输入你想入队的元素个数:\n");
	scanf("%d",&size);
	printf("请输入您要入队元素个数:\n");
	for(int i=0;i<size;i++){
		scanf("%d",&data);
		settop(*L,data);}
	printf("新建成功,");
	appear(*L);
	printf("-----------------------------------\n");
	int ii;
	printf("1入队\t2出队\t3取队头元素\t4显示队内元素\n");
	printf("请输入您想要的操作数:\n");
	scanf("%d",&ii);

	while(ii){
		if(ii==1){
			printf("请输入您想入队的元素:\n");
			scanf("%d",&data);
			settop(*L,data);
			printf("\n入队成功\n");
		}
		else if(ii==2){
		XX ww=gettop(*L);
		printf("取出的元素为:%d\n",ww);
		}
		else if(ii==3){
			XX www=havetop(*L);
			printf("队头元素为:%d\n",www);
		}
		else if(ii==4)
			appear(*L);
		else
			break;
		printf("-----------------------------------\n");
		printf("请输入您想要的操作数:\n");
		scanf("%d",&ii);
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值