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;
    }
    
  • 相关阅读:
    【NET】File操作练习笔记
    【微信小程序】分包的使用和预下载
    【微信小程序】组件Component常用案例
    MVCC多版本并发控制
    数据存储引擎
    seata 分布式事务 -- seata-three工程完整代码
    seata 分布式事务 -- seata-two工程完整代码
    seata 分布式事务 -- seata-one 工程完整代码
    seata 分布式事务 -- 准备工作
    seata 分布式事务 -- TCC模式
  • 原文地址:https://www.cnblogs.com/xyqxyq/p/10211369.html
Copyright © 2011-2022 走看看