顺序表实现优先级队列

来源:互联网 发布:内核linux 编辑:程序博客网 时间:2024/05/22 14:05

 问题:

客服坐席处理客户(手机号码,带有优先级)来电按照5个优先级别(级别为1~5)进行处理,不同优先级别的用户在对应级别的队列中等候,如果前一级别的队列为空,则后继队列中等待的客户优先级自动升高一级,例如级别3的队列为空,则级别4和5的队列中客户的优先级自动升高一级。请写出程序实现解决该问题。

 

分析:

采用5个队列来实现,每个队列表示一个优先级,队列最大长度为1000(仅示例)。

 

头文件:

#include <stdio.h>
#define QUEUELENGTH 1000       //队列的长度
#define SIZE 5                 //优先级队列的个数
#define MSISDNLENGTH 14        //手机号码长度

#define SUCCESS 0
#define ERROR -1

typedef struct{
    char MSISDN[MSISDNLENGTH];      //手机号码
} Customer;

typedef struct{
    Customer data[QUEUELENGTH];    //节点对象
 int length;                    //队列长度
 int pri;                       //优先级
} Queue;

Queue *serlist[SIZE];

 

main文件:

#include "orderTable.h"
#include <stdlib.h>
#include <string.h>

//根据优先级初始化队列
int init()
{
    int i, j;
    //初始化5个优先级队列
    for (i = 1; i < SIZE+1; i++)
    {
        serlist[i-1] = (Queue*)malloc(sizeof(Queue));
        for (j = 0; j < QUEUELENGTH; j++)
        {
             memset(serlist[i-1]->data[j].MSISDN, 0, MSISDNLENGTH);
        }

        serlist[i-1]->pri = i;
        serlist[i-1]->length = 0;
    }

    return SUCCESS;
}

 

//把客户插入指定队列的指定位置
int insert(Queue *q, Customer *cus, int pos)
{
    int j;
    if (0 > pos || pos > q->length)
    {
        printf("pos is invalid, pos = [%d]/n", pos);
        return ERROR;
    }
    if (pos > QUEUELENGTH)
    {
        printf("pos is over, pos = [%d]/n", pos);
        return ERROR;
    }
    for (j = q->length - 1; j>=pos; j--)
    {
        strncpy(q->data[j+1].MSISDN, q->data[j].MSISDN, MSISDNLENGTH);
    }
    strncpy(q->data[pos].MSISDN, cus->MSISDN, MSISDNLENGTH);
    q->length = q->length + 1;
    return SUCCESS;
}

 

//把客户从指定队列的指定位置获取出来
int del(Queue *q, Customer *cus, int pos)
{
    int j;
    if (0 > pos || pos > q->length)
    {
        printf("pos is invalid, pos = [%d]/n", pos);
        return ERROR;
    }

    strncpy(cus->MSISDN, q->data[pos].MSISDN, MSISDNLENGTH);
    for (j = pos; pos < q->length; pos++)
    {
        strncpy(q->data[j].MSISDN, q->data[j+1].MSISDN, MSISDNLENGTH);
    }
    q->length = q->length - 1;

    return SUCCESS;
}

 

int putCustomer(char *msisdn, int pri)
{
    Customer *cus = (Customer*) malloc (sizeof (Customer));

    //根据客户优先级插入对应的队列
    strncpy(cus->MSISDN, msisdn, MSISDNLENGTH);
    
    insert(serlist[pri-1], cus, serlist[pri-1]->length);
    return SUCCESS;
}

 

//获取一个需要服务的用户
int getCustomer(char *msisdn)
{
    int i;
    Customer *tmpCus = (Customer*)malloc(sizeof(Customer));
    for (i=0; i<SIZE; i++)
    {
        if (0 != serlist[i]->length)
        {
            del(serlist[i], tmpCus, 0);
            strncpy(msisdn, tmpCus->MSISDN, MSISDNLENGTH);
            if (NULL != tmpCus)
           {
               free(tmpCus);
           }
           return SUCCESS;
       }
    }
    if (NULL != tmpCus)
    {
        free(tmpCus);
    }
    return ERROR;
}

 

int main()
{
    int i, j;
    char msisdn[MSISDNLENGTH];
    //char aa[10];
    int pri;

    init();

    printf("/nPlease input msisdn:");
    scanf("%s",&msisdn);
    if (MSISDNLENGTH < strlen(msisdn))
   {
        printf("/nmsisdn is invalid, msisdn = [%s]/n", msisdn);
        exit(0);
    }
 
    printf("/nPlease input priority:");
    scanf("%d", &pri);
    if (pri < 1 || pri > SIZE)
    {
        printf("/npriority is invalid, priority = [%d]/n", pri);
        exit(0);
    }

    while(0 != strcmp("-q", msisdn))
    {
        putCustomer(msisdn, pri);

        printf("/nPlease input msisdn:");
        scanf("%s",&msisdn);
        if (MSISDNLENGTH < strlen(msisdn))
        {
              printf("/nmsisdn is invalid, msisdn = [%s]/n", msisdn);
              exit(0);
        }
  
         if (0 != strcmp("-q", msisdn))
        {
            printf("/nPlease input priority:");
            scanf("%d", &pri);
            if (pri < 1 || pri > SIZE)
            {
                printf("/npriority is invalid, priority = [%d]/n", pri);
                exit(0);
            }
         }
    }

    //打印出所有的值
    for (i = 0; i < SIZE; i++)
   {
        printf("service list[%d]'s data is:/n", i);
  
        for (j = 0; j < serlist[i]->length; j++)
       {
            printf("pos[%d]=[%s]/n", j+1, serlist[i]->data[j].MSISDN);
        }
        //gets(aa);
    }
 
    //获得当前需要服务的客户
    getCustomer(msisdn);
    printf("msisdn=[%s]/n", msisdn);

    //gets(aa);
    exit(0);
}

 

原创粉丝点击