基数排序的设计实现 队列应用

//Queue.h实现
const int MaxQSize = 50;
struct Person{
    char name[20];
    char sex;//F M
};
typedef int DataType;
class Queue{
    private:
    
        int front,rear,count;
        DataType qlist[MaxQSize];
    public:
        Queue();
        void QInsert(const DataType& item);
        DataType QDelete(void);
        void ClearQueue(void);
        //访问队列
        DataType QFront(void) const;
        //检测队列状态
        int QLength(void) const;
        int QEmpty(void) const;
        int QFull(void) const;
        

};



//Queue.cpp实现
#include <iostream>
#include <stdlib.h>
#include"Queue.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

Queue::Queue(){
    front = 0;
    rear = 0;
    count = 0;
}
void Queue::QInsert(const DataType& item){
    if(count == MaxQSize){
        cerr <<"Queue overflow"<<endl;
        exit(1);
    }
    count++;
    qlist[rear]=item;
    rear=(rear+1)%MaxQSize;
}

DataType Queue::QDelete(){
    DataType temp;
    //若队列为空退出程序
    if(count==0){
        cerr<<"delteing from an empty queue"<<endl;
        exit(1);
    }
    temp = qlist[front];
    count--;
    front = (front+1)%MaxQSize;
    return temp;
}
void Queue::ClearQueue(){
    rear = front = count = 0;
}
int Queue::QLength() const{
    return count;
}
int Queue::QFull() const{
    return count==MaxQSize;
}

int Queue::QEmpty()const{

    return count==0;
}
DataType Queue::QFront()const{
    if(count != 0){
        return qlist[front];
    }else{
        return qlist[front];//不会 不知道返回的是什么
    }
    
}




//main.cpp实现

#include <iostream>
#include<cmath>
#include<ctime>
#include<cstdlib>
typedef int DataType;
#include"Queue.h"
using namespace std;
//自定义类型区分一个数的个位数和十位数
enum DigitKind{
    ones,tens
};
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
/*将数组中的数放入10个下标从0-9 的队列之一。用户定义的类型DigitKind表明这次分发根据的是个位数概述十位数*/
void Distribute(int L[],Queue digitQueue[], int n, DigitKind kind)
{
    int i;
    //循环处理n个元素的数组
    for( i = 0; i < n; i++){
        if( kind == ones)
            digitQueue[L[i]%10].QInsert(L[i]);
        else
            //按十位数插入到相应的队列
            digitQueue[L[i]/10].QInsert(L[i]);
    }
        
}


//从队列中取到元素并送回数组中
void Collect(Queue digitQueue[], int L[])
{
    int i=0,digit = 0;
    for(digit = 0; digit < 10; digit++){
        //收集队列中的元素并返回数组中
        while(!digitQueue[digit].QEmpty())
            L[i++] = digitQueue[digit].QDelete();
        
    }
 }

//扫描n元素数组 并输出,每行输出10个数
void PrintArray(int L[], int n){
    int i =0;
    while(i < n){
        cout.width(5);//输出5个空格
        cout<<L[i];//输出相应元素
        if(++i%10 == 0)//每10个数后换行
            cout<<endl;
    }
    cout<<endl;
}

int Random(int m, int n)
{
        int pos, dis;
        if(m == n)
        {
            return m;
        }
        else if(m > n)
        {
            pos = n;
            dis = m - n + 1;
            return rand() % dis + pos;
        }
        else
        {
            pos = m;
            dis = n - m + 1;
            return rand() % dis + pos;
        }
}

int main(int argc, char** argv) {
    //用来暂存数据的10个队列
    Queue digitQueue[10];
    //50个整数的数组
    int L[50];
    int i = 0;
    int item;
    srand((int)time(NULL));
   //提供随机数
    //50个范围在0-99随机数初始化数组
    for(i = 0; i < 50; i++){
        L[i] = Random(0,99);
    }
    //把它们个位数分到10个队列中去 收集回来并输出
    Distribute(L,digitQueue,50,ones);
    Collect(digitQueue,L);
    PrintArray(L,50);
    //将他们十位数分到10个队列中去,收集回来打印拍好顺序的数组
    Distribute(L,digitQueue,50,tens);
    Collect(digitQueue,L);
    PrintArray(L,50);
    return 0;
}

 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值