zoukankan      html  css  js  c++  java
  • 循环线性队列的基本操作(带实验数据)

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    bool overflow=0;
    struct Queue {
    	int date[300];
    	int head;
    	int tail;
    	int len;
    }queue;
    
    void InitQueue(Queue &Q)
    {
    	Q.head=0;
    	Q.tail=0;
    	Q.len=0;
    	cout<<"Initial successfully!"<<endl;
    }
    
    void DestroyQueue(Queue &Q)
    {
    	Q.head=-1;
    	Q.tail=-1;
    	Q.len=-1;
    	memset(Q.date,0,sizeof(Q.date));
    	cout<<"Destroy succeed."<<endl;
    }
    
    void ClearQueue(Queue &Q)
    {
    	if (Q.len==-1) {
    		cout<<"Queue isn't exist."<<endl;
    	}
    	else {
    		Q.head=0;
    		Q.tail=0;
    		Q.len=0;
    		memset(Q.date,0,sizeof(Q.date));
    		cout<<"Clear successfully!"<<endl;
    	}
    }
    
    void QueueEmpty(Queue &Q)
    {
    	if (Q.len==-1)
    	cout<<"Queue isn't exist."<<endl;
    	else if (Q.len==0)
    	cout<<"TRUE"<<endl;
    	else
    	cout<<"FALSE"<<endl;
    }
    
    void QueueLength(Queue &Q)
    {
    	if (Q.len==-1)
    	cout<<"Queue isn't exist."<<endl;
    	else 
    	cout<<"Queue length is "<<Q.len<<" ."<<endl;
    }
    
    void GetHead(Queue &Q,int &e)
    {
    	if (Q.len==0) {
    		cout<<"Queue is empty."<<endl;
    	}
    	else {
    		e=Q.date[Q.head];
    		cout<<e<<endl;
    	}
    }
    
    void EnQueue(Queue &Q,int e)
    {
    	Q.date[Q.tail++]=e;
    	if (Q.tail==300) {
    		overflow=!overflow;	//Խ���ж� 
    	}
    	Q.tail%=300;
    	Q.len++;
    	cout<<"Insert successfully!"<<endl;
    	if (Q.len==300) {
    		cout<<"The queue is full."<<endl;
    	}
    }
    
    void DeQueue(Queue &Q,int &e)
    {
    	e=Q.date[Q.head];
    	Q.date[Q.head++]=0;
    	if (Q.head==300) {
    		overflow=!overflow;	//Խ���ж� 
    	}
    	Q.head%=300;
    	Q.len--;
    	cout<<"Delete succeed.The value is "<<e<<" ."<<endl;
    	if (Q.len==0)
    	cout<<"Queue is empty."<<endl;	
    }
    
    void QueueTraverse(Queue &Q)
    {
    	if (overflow==1) {
    		for (int i=Q.head;i<300;i++) {
    			cout<<Q.date[i]<<" ";
    		}
    		for (int i=0;i<Q.tail;i++) {
    			cout<<Q.date[i]<<" ";
    		}
    		cout<<endl;
    	}
    	else {
    		for (int i=Q.head;i<Q.tail;i++) {
    			cout<<Q.date[i]<<" ";
    		}
    		cout<<endl;
    	}
    }
    
    int main()
    {
    	cout<<"Please enter what you want to do."<<endl
    	<<"1.Initial a queue."<<endl
    	<<"2.Destroy the queue."<<endl
    	<<"3.Clear the queue."<<endl
    	<<"4.Is the queue empty?"<<endl
    	<<"5.Length of the queue."<<endl
    	<<"6.The first number of the queue."<<endl
    	<<"7.Insert a number."<<endl
    	<<"8.Delete a number."<<endl
    	<<"9.Print the queue."<<endl;
    	
    	queue.len=-1;
    	int n,e;
    	while (cin>>n) {
    		switch (n) {
    			case 1:
    				InitQueue(queue);
    				for (int i=0;i<300;i++) {
    					EnQueue(queue,i);					
    				}
    				break;
    			case 2:
    				DestroyQueue(queue);
    				break;
    			case 3:
    				ClearQueue(queue);
    				break;
    			case 4:
    				QueueEmpty(queue);
    				break;
    			case 5:
    				QueueLength(queue);
    				break;
    			case 6:
    				if (queue.len==-1)
    				cout<<"Queue isn't exist."<<endl;
    				else
    				GetHead(queue,e);
    				break;
    			case 7:
    				if (queue.len==-1)
    				cout<<"Queue isn't exist."<<endl;
    				else  {
    					cout<<"Please enter the number you want to insert."<<endl;
    					cin>>e;
    					EnQueue(queue,e);	
    				}
    				break;
    			case 8:
    				if (queue.len==-1)
    				cout<<"Queue isn't exist."<<endl;
    				else if (queue.len==0)
    				cout<<"Queue is empty."<<endl;
    				else
    				DeQueue(queue,e);
    				break;
    			case 9:
    				if (queue.len==-1)
    				cout<<"Queue isn't exist."<<endl;
    				else if (queue.len==0)
    				cout<<"Queue is empty."<<endl;
    				else
    				QueueTraverse(queue);
    				break;
    		}
    	}
    	
    	return 0;
    }
    
  • 相关阅读:
    转载:《TypeScript 中文入门教程》 16、Symbols
    转载:《TypeScript 中文入门教程》 15、可迭代性
    在 docker 中(linux 系统)运行 sql server
    遇到一个在 sql server 查询中,条件语句不区分大小写的问题
    设置 MySQL 的时区
    js 控制浏览器全屏显示
    使用 bat 获取当前系统 ip 地址并生成快捷方式
    在 WinForm 中使用 Anchor 对控件进行定位
    在 WinForm 中获取嵌入的资源
    在 WinForm 窗体设计器中引用 x64 格式的程序集会发生错误
  • 原文地址:https://www.cnblogs.com/xyqxyq/p/10211369.html
Copyright © 2011-2022 走看看