文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>数据结构-顺序队列[源代码]

数据结构-顺序队列[源代码]

时间:2010-09-17  来源:chengxiaopeng

文件: seq_queue.rar
大小: 20KB
下载: 下载

    数据结构中的队列,主要有初始化队列、入队、出队、判断队列是否为空、获取队头元素、清空队列、获取队列中的元素个数等方法。我将其组成成一个可以使用的系统。供大家参考学习用。代码如下:

#include <stdio.h>
#define SIZE sizeof(SEQQUEUE)
#define MAXSIZE 5
#define PRINT_ITEM "%d "
#define SCANF_ITEM "%d"
typedef int bool; //定义bool类型

const bool true = 1;
const bool false = 0;
typedef int ELEMTYPE; //元素类型

const ELEMTYPE QUEUE_NULL = -9999; //定义当队列为空,返回的值

typedef struct //定义顺序队列

{
    ELEMTYPE data[MAXSIZE];
    int front; //队头指针

    int rear; //队尾指针

}SEQQUEUE;

SEQQUEUE * INITQUEUE();//初始化空队列

bool EMPTY(SEQQUEUE *);//判断队列是否为空

bool FULL(SEQQUEUE *);//判断队列是否已满

bool ADDQUEUE(SEQQUEUE *,ELEMTYPE);//进行元素入队操作,true:入队成功;false: 队列已满,不能入队

ELEMTYPE DELQUEUE(SEQQUEUE *);//从队头删除一个元素,返回队头元素值;如果队列为空,则返回QUEUE_NULL

ELEMTYPE GET_FRONT(SEQQUEUE *);//获取队列头的元素值,如果队列为空,返回QUEUE_NULL

ELEMTYPE GET_REAR(SEQQUEUE *);//获取队尾元素值,如果队列元素为空,返回QUEUE_NULL

bool CLEAR(SEQQUEUE *);//清空队列;true:操作成功;false:队列为空

int CURRENT_SIZE(SEQQUEUE *);//求队列中的元素的个数的函数


void menu();//定义菜单

void print_tab(); //打印tab键

void print_enter(); //打印回车

void print_str(char *); //打印字符串

void print_info(char *); //打印信息

void print_menu_item(char *,char *); //打印菜单每一项


void queue_add(SEQQUEUE *);//入队

void queue_del(SEQQUEUE *);//出队

void queue_empty(SEQQUEUE *);//是否空队

void queue_get_front(SEQQUEUE *);//获取队头元素

void queue_clear(SEQQUEUE *);//清空队列

void queue_count(SEQQUEUE *);//队列元素个数

void queue_method(SEQQUEUE *,void (* fun)(SEQQUEUE *));//定义函数指针

int get_item();//获取选项的值

ELEMTYPE get_input();//获取输入的元素值。

void print_queue_full();//打印队列满的信息。

void print_queue_null();//队列是null

int main(int argc,char *aggv[])
{
    SEQQUEUE * queue = INITQUEUE();
    menu();
    int item_value = 0;
    do
    {
        item_value = get_item();
        switch(item_value)
        {
            case -1:
                 break;
            case 0:
                 menu();
                 break;
            case 1:
                 queue_method(queue,queue_add);
                 break;
            case 2:
                 queue_method(queue,queue_del);
                 break;
            case 3:
                 queue_method(queue,queue_empty);
                 break;
            case 4:
                 queue_method(queue,queue_get_front);
                 break;
            case 5:
                 queue_method(queue,queue_clear);
                 break;
            case 6:
                 queue_method(queue,queue_count);
                 break;
            default:
                 menu();
                 break;
        }
    }while (item_value != -1);
    return 0;
}

SEQQUEUE * INITQUEUE()
{
     SEQQUEUE * queue = malloc(SIZE);
     queue->front = 0;
     queue->rear = 0;
}

bool EMPTY(SEQQUEUE * queue)
{
     bool result;
     if (queue->front == queue->rear)
     {
         result = true;
     }
     else
     {
         result = false;
     }
     return result;
}

bool FULL(SEQQUEUE * queue)
{
     bool result;
     if (queue->front == (queue->rear + 1) % MAXSIZE)
     {
         result = true;
     }
     else
     {
         result = false;
     }
}

bool ADDQUEUE(SEQQUEUE * queue,ELEMTYPE data)
{
    bool result;
    if (FULL(queue))
    {
        result = false;
    }
    else
    {
        queue->rear = (queue->rear + 1) % MAXSIZE; //指向下一个单元

        queue->data[queue->rear] = data;
        result = true;
    }
    return result;
}

ELEMTYPE DELQUEUE(SEQQUEUE * queue)
{
    ELEMTYPE result = QUEUE_NULL;
    if (! EMPTY(queue))
    {
          queue->front = (queue->front + 1) % MAXSIZE;
          result = queue->data[queue->front];
    }
    return result;
}

ELEMTYPE GET_FRONT(SEQQUEUE * queue)
{
    ELEMTYPE result = QUEUE_NULL;
    if (! EMPTY(queue))
    {
          int index = (queue->front + 1) % MAXSIZE;
          result = queue->data[index];
    }
    return result;
}

ELEMTYPE GET_REAR(SEQQUEUE * queue)
{
    ELEMTYPE result = QUEUE_NULL;
    if (! EMPTY(queue))
    {
          result = queue->data[queue->rear];
    }
    return result;
}

bool CLEAR(SEQQUEUE * queue)
{
     bool result = false;
     if(! EMPTY(queue))
     {
          queue->front = 0;
          queue->rear = 0;
          result = true;
     }
     return result;
}

int CURRENT_SIZE(SEQQUEUE * queue)
{
    int result;
    if (queue->front == queue->rear)
    {
        result = 0;
    }
    else if (queue->rear > queue->front)
    {
         result = queue->rear - queue->front;
    }
    else
    {
        result = queue->rear + MAXSIZE - queue->front;
    }
    return result;
}


void menu()
{
     print_enter();
     print_tab();print_info("sequeue queue manage system");print_enter();
     print_menu_item("1","add element into queue");
     print_menu_item("2","delete a element from front");
     print_menu_item("3","judge queue is empty");
     print_menu_item("4","get front element value");
     print_menu_item("5","clear queue");
     print_menu_item("6","get queue element count");
     print_menu_item("0","return menu");
     print_menu_item("-1","exit system");
}

void print_tab()
{
     printf("\t");
}

void print_enter()
{
     printf("\n");
}

void print_str(char * ch)
{
     while(*ch)
     {
         printf("%c",*ch++);
     }
}

void print_info(char * info)
{
     print_tab();
     print_str(info);
     print_enter();
}
 
void print_menu_item(char * item,char * desc)
{
     print_tab();
     print_str(item);
     print_tab();
     print_str(desc);
     print_enter();
}

ELEMTYPE get_input()
{
         ELEMTYPE result;
         print_tab();
         printf("please input :");
         scanf(SCANF_ITEM,&result);
         return result;
}
int get_item()
{
    int result;
    print_tab();
    printf("select menu :");
    scanf("%d",&result);
    return result;
}

void print_queue_full()
{
     print_info("error:queue is full.");
}

void print_queue_null()
{
     print_info("error:queue is null.");
}

void queue_add(SEQQUEUE * queue)
{
     ELEMTYPE data = get_input();
     bool result = ADDQUEUE(queue,data);
     if(result)
     {
         print_info("add element success.");
     }
     else
     {
         print_queue_full();
     }
}

void queue_del(SEQQUEUE * queue)
{
     ELEMTYPE result = DELQUEUE(queue);
     if (result != QUEUE_NULL)
     {
         print_info("del head queue is success.");
     }
     else
     {
         print_queue_null();
     }
}

void queue_empty(SEQQUEUE * queue)
{
     bool result = EMPTY(queue);
     if (result)
     {
         print_info("queue is empty.");
     }
     else
     {
         print_info("queue is not empty.");
     }
}
void queue_get_front(SEQQUEUE * queue)
{
     ELEMTYPE result = GET_FRONT(queue);
     if (result == QUEUE_NULL)
     {
         print_info("error:queue is null.");
     }
     else
     {
         print_tab();
         printf("the queue front element is : ");
         printf(PRINT_ITEM,result);
         print_enter();
     }
}

void queue_clear(SEQQUEUE * queue)
{
     bool result = CLEAR(queue);
     if (result)
     {
         print_info("queue clear is success.");
     }
     else
     {
         print_info("queue is empty");
     }
}
void queue_count(SEQQUEUE * queue)
{
     int count = CURRENT_SIZE(queue);
     print_tab();
     printf("the element count is : %d",count);
     print_enter();
}
void queue_method(SEQQUEUE * queue,void (* fun)(SEQQUEUE * src_queue))
{
     (* fun)(queue);
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载