数据结构(C语言)- 队子系统(实验)

循环顺序子队列

1、实验目的
(1)掌握队列的特点及其存储的方式
(2)用顺序结构实现一个队列
(3)掌握队列的初始化、插入、删除和显示队头等的基本操作
2、实验内容
(1)定义一个整型的循环顺序队列。
(2)编写实现进队、出队、显示队头元素等所有基本操作。
(3)设计如下选择式菜单,以选择菜单的方式进行操作。将上述各种算法实现

(仅供参考)在这里插入图片描述

//循环顺序队列
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef int Datatype;
typedef struct Circularqueue{
	Datatype Data[100];//队列的大小 
	int  front;//记录头元素位置的变量 
	int rear; //记录尾元素位置的变量 
}Circularqueue;
void InitCircularqueue(Circularqueue *queue){//初始化队列 
	queue->front=queue->rear=0; //头元素与尾元素等于0 
	if(queue->front==queue->rear){
		printf("Circular queue created successfully。");
	}else{
		printf("Circular queue creation failed!");
		exit(1);
	}
}
int Judge(Circularqueue *queue){//判断队空 
	if(queue->front==queue->rear){
		return 1;
	}else{
		return 0;
	}
} 
int Theteamisfull(Circularqueue *queue){//判断队满 
	if((queue->rear+1)%100==queue->front){//利用取余运算判断是否相等 
		return 0;
	}else{
		return 1;
	}
}
void Displayelements(Circularqueue *queue){//输出队中元素 
    int i=queue->front;
	if(Judge(queue)==1){
		printf("Circular queue is empty, no element\n");
	}else{
		printf("\nThe elements in the circular queue are:");
		while(i!=queue->rear){
			printf("%5d",queue->Data[i+1]);
			i++;
		} 
	}
}
void Jointheteam(Circularqueue *queue){//入队操作 
	char p[50];
	int k,i;
	k=i=0;
    static char Start[]="Start";
    static char End[]="End";
    if(Theteamisfull(queue)==0){
    	printf("Circular queue is full, cannot operate!");
    	k++;
	}else{
		printf("Enter “Start” to start, enter “End”to end the team:");
		gets(p);
		while(strcmp(p,Start)!=0){
			if(strcmp(p,End)==0){
				printf("You didn't join the team!");
				i++;
				break;
			}
			printf("\nInput error, please re-enter:");
			memset(p,0,sizeof p);
	        gets(p);
		} 
		if(strcmp(p,Start)==0){
			printf("\nPlease enter the entry element:");
			memset(p,0,sizeof p);
	        gets(p);
		}
		while(strcmp(p,End)!=0){
	        queue->rear=(queue->rear+1)%100;
	        queue->Data[queue->rear]=atoi(p);
	        printf("\nPlease enter the entry element:");
	        memset(p,0,sizeof p);
	        gets(p);
	        i++;
	        continue;  
		}
	}
	if(k+i==i&&i==1){
		printf("Failed to join the team!");
		if(k==1){
			printf("The team is full!");
		}
	}else{
		Displayelements(queue);
	}
} 
void Getoutoftheteam(Circularqueue *queue){//出队操作 
	char p[50];
	int k,i,x,l;
	k=i=0;
    static char Start[]="Start";
    static char End[]="End";
	if(Judge(queue)==1){
		printf("Circular queue is empty, no element\n");
		k++;
	}else{
        printf("Enter “Start” to start the team, enter “End” to end:"); 
        gets(p);
       while(strcmp(p,Start)!=0){
			if(strcmp(p,End)==0){
				printf("You are not out of the team!");
				exit(1);
			}
			printf("\nInput error, please re-enter:");
			memset(p,0,sizeof p);
	        gets(p);
		} 
		if(strcmp(p,Start)==0){
			printf("\nPlease input the element of out of team:");
			memset(p,0,sizeof p);
	        gets(p);
		}
		l=queue->front;
		while(strcmp(p,End)!=0){
			    x=(l+1)%100;
				if(queue->Data[x]==atoi(p)){
					queue->front=(queue->front+1)%100;
				    x=queue->Data[queue->front];
		            printf("\nPlease input the element of out of team:");
			        memset(p,0,sizeof p);
	                gets(p);
				    l++;
	                i++;
				 }else{
                   printf("It doesn't meet the rules! Please re-enter:");
				   memset(p,0,sizeof p);
	               gets(p);
				 }

	             continue;  
		}
	}
	if(k+i==i&&i==0){
	   printf("Failed to get out of the team!");
	}else{
	   Displayelements(queue);
	}
}
void Lineheadelement(Circularqueue *queue){//显示队头元素 
	int x;
      if(Judge(queue)==1){
	    printf("The team is empty! Unable to take the top operation!");
	  }else{
	    printf("The line head elements are:");
		x=queue->Data[(queue->front+1)%100];
		printf("%5d",x);
	  }
} 
void Menu(){
	printf("\n          Circular queue");
	printf("\n-----------------------------------------------------");
	printf("\n              1--Initial queue                       ");
	printf("\n              2--Queue operation                     ");
	printf("\n              3--Out-of-line operation               ");
	printf("\n              4--Show line header elements           ");
	printf("\n              5--Elements in the output team         "); 
	printf("\n              0--Exit                                ");
	printf("\n-----------------------------------------------------");
	printf("\nPlease enter the menu number (0-5):"); 
}
int main(){
	Circularqueue queue;
	char ch1,ch2,a;
	ch1='y';
		while(ch1=='y'||ch1=='Y'){
		Menu();
		scanf("%c",&ch2);
		getchar();
		switch(ch2){
			case '1':
			InitCircularqueue(&queue);
			break;
			case '2':
			Jointheteam(&queue);
			break;
			case '3':
			Getoutoftheteam(&queue);
			break;
			case '4':
			Lineheadelement(&queue);
			break;
			case '5':
			Displayelements(&queue);
			break;
				case '0':
			    ch1='n';
				break;
			default:
			    printf("Input error!!!!");  
		}
		if(ch2!='0'){
		    printf("\nPress enter to continue, press any key to return to the main menu!\n");
		}
			a=getchar();
		if(a!='\xA'){
				getchar();
				ch1='n';
		}
	}
				
}

链队子系统

1、实验目的
(1)掌握队列的特点及其存储的方式
(2)用链式结构实现一个队列
(3)掌握队列的初始化、插入、删除和显示队头等的基本操作
2、实验内容
(1)定义一个整型的循环顺序队列。
(2)编写实现进队、出队、显示队头元素等所有基本操作。
(3)设计如下选择式菜单,以选择菜单的方式进行操作。将上述各种算法实现
(仅供参考)在这里插入图片描述

//链队
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int Data;
typedef struct Chainteam{
	Data data;//数据域 
	struct Chainteam *next;//指针域 
}Chainteam;
typedef struct{
	Chainteam *front ,*rear;//头,尾指针 
}LinkQueue;
LinkQueue *Initializethechain(){
	LinkQueue *Team;
	Chainteam *P;
	Team=(LinkQueue *)malloc(sizeof(LinkQueue));
	P=(Chainteam *)malloc(sizeof(Chainteam));
	Team->front=P;
	Team->rear=P;
	if(Team->front==Team->rear){
		printf("Initialize the chain team successfully.");
	}else{
		printf("Initialization chain failed! Please check and try again.");
	}
	return Team;
}
int JudgmentChain(LinkQueue *Team){
	if(Team->front==Team->rear){
		return 0;
	}else{
		return 1;
	}
}
void Output(LinkQueue *Team){
	Chainteam *q=Team->front->next;
	if(JudgmentChain(Team)==0){
		printf("Air team! ");
	}else{
		printf("\nElements in the team:");
		while(q!=NULL){
			printf("%5d",q->data);
			q=q->next;
		}
	}
}
void Jointhechainteam(LinkQueue *Team){
	int j;
	j=0;
	Chainteam *p;
	char q[50];
	static char Start[]="Start";
	static char End[]="End";
	printf("Enter “Start” out of the chain, enter “End”to end:");
	gets(q);
	while(strcmp(q,Start)!=0){
		if(strcmp(q,End)==0){
			printf("\nYou didn't get out of the team!");
			break;
		}
		printf("\nInput error, please enter “Start”to start joining the queue:");
		memset(q,0,sizeof q);
	    gets(q);
	}
	if(strcmp(q,Start)==0){
		printf("\nEnter the queue element:");
		memset(q,0,sizeof q);
	    gets(q);
	}
	while(strcmp(q,End)!=0){
		p=(Chainteam *)malloc(sizeof(Chainteam));
		p->data=atoi(q);
		p->next=NULL;
		Team->rear->next=p;
		Team->rear=p;
	    printf("\nEnter the queue element:");
	    memset(q,0,sizeof q);
	    gets(q);
	    j++;
	}
	if(j!=0){
		Output(Team);
	}
	else{
		printf("Failed to join the team!");
	}
}
void Outofthechainteam(LinkQueue *Team){
   int j,x;
	j=0;
	Chainteam *p;
	char q[50];
	static char Start[]="Start";
	static char End[]="End";
	printf("Enter “Start” out of the chain, enter “End”to end:");
	gets(q);
	while(strcmp(q,Start)!=0){
		if(strcmp(q,End)==0){
		     printf("You didn't go out of the team.");
		     break;
		}
		printf("Input error, enter “Start”to start queuing:");
		memset(q,0,sizeof q);
	    gets(q);
	}
	if(strcmp(q,Start)==0){
		printf("Open input and outbound elements:");
		memset(q,0,sizeof q);
	    gets(q);
	}
	while(strcmp(q,End)!=0){
		if(Team->front->next->data==atoi(q)){
			p=Team->front->next;
		    x=p->data;
		    Team->front->next=p->next;
		    if(p->next=NULL){
		    	Team->rear=Team->front;
			}
			free(p);
		    printf("Open input and outbound elements:");
		    memset(q,0,sizeof q);
	        gets(q);
		    j++;
			continue;  
		}
		printf("Do not meet the queuing rules, please re-enter:");
		memset(q,0,sizeof q);
	    gets(q);
		
	}
	if(j!=0){
		Output(Team);
	}else
	{
		printf("Failed to get out of the team!");
	}
}
void Teamleader(LinkQueue *Team){
	int x;
    if(JudgmentChain(Team)==0){
         printf("The team is empty! No team leader!");
	}else{
		x=Team->front->next->data;
		printf("The line head elements are:%5d",x);
	}
}
void Destruction(LinkQueue *Team){
    while(Team->front){
    	Team->rear=Team->front->next;
    	free(Team->front);
    	Team->front=Team->rear;
	}
	if(Team->front==NULL){
	printf("Queue destroyed successfully.");
	}else
	{
		printf("Queue destroy failed!");
	}	
}
void Menu(){
	printf("\n          Chain team");
	printf("\n-----------------------------------------------------------");
	printf("\n              1--Initialize the chain                      ");
	printf("\n              2--Queue operation                           ");
	printf("\n              3--Out-of-line operation                     ");
	printf("\n              4--Show line header elements                 ");
	printf("\n              5--Elements in the output team               "); 
	printf("\n              6--Destroy queue                             ");
	printf("\n              0--Exit                                      ");
	printf("\n-----------------------------------------------------------");
	printf("\nPlease enter the menu number (0-5):"); 
}
int main(){
	LinkQueue *Team;
	char ch1,ch2,a;
	ch1='y';
		while(ch1=='y'||ch1=='Y'){
		Menu();
		scanf("%c",&ch2);
		getchar();
		switch(ch2){
				case '1':
					Team=Initializethechain();
					break;
				case '2':
					Jointhechainteam(Team);
					break;
				case '3':
					 Outofthechainteam(Team);
					 break;
				case '4' :
					 Teamleader(Team);
					 break;
				case '5':
				      Output(Team);
				      break;
				case '6':
					  Destruction(Team);
					  break; 
				case '0':
			          ch1='n';
				      break;
			default:
			    printf("Input error!!!!");  
		}
		if(ch2!='0'){
		    printf("\nPress enter to continue, press any key to return to the main menu!\n");
		}
			a=getchar();
		if(a!='\xA'){
				getchar();
				ch1='n';
		}  
	}
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页