Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2538653
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-09-19 20:45:38

文件: link_queue.rar
大小: 14KB
下载: 下载
    链队列,也是经常使用的一种数据结构,开始的时候,我写程序,总出现错误。最后发现,你只要记住一条,队列头指针指向的元素为NULL,而队列尾指针指向最后一个元素。其实我这些代码,也都是根据书上的算法和C代码,经过自己整理的。如果有不懂的地方,多看书,多思考。我相信我们每个人都能把数据结构学好的。代码如下:

#include <stdio.h>
#define SIZE sizeof(LINKQUEUE)
#define LISTSIZE sizeof(LINKQUEUELIST)
#define PRINT_ITEM "%d "
#define SCANF_ITEM "%d"
typedef int ELEMTYPE;
const ELEMTYPE QUEUE_NULL = -9999;//定义当队列为NULL时,返回的数据元素的值

typedef struct qnode //定义链队列结构

{
    ELEMTYPE data;
    struct qnode * next;
}LINKQUEUE;
typedef struct
{
    LINKQUEUE * front,*rear;
    int length;
}LINKQUEUELIST;
typedef enum
{
    true = 1,
    false = 0
} bool;
LINKQUEUE * INITQUEUE(); //初始化链队列中的一个数据项

LINKQUEUE * CREATE(ELEMTYPE);//创建一个节点

LINKQUEUELIST * INITQUEUELIST();//初始化链队列

void ADDQUEUE(LINKQUEUELIST *,ELEMTYPE);//将元素插入到链队列中

ELEMTYPE GET_FRONT(LINKQUEUELIST *);//获取队头元素值

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

int CURRENT_SIZE(LINKQUEUELIST *);//当前链队列的元素个数

bool CLEAR(LINKQUEUELIST *); //清除队列

ELEMTYPE DELQUEUE(LINKQUEUELIST *); //删除队列头的元素,返回删除元素的值

void menu();//菜单

void print_enter(); //打印回车

void print_tab(); //打印tab

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

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

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

int select_menu_item();

void queue_add(LINKQUEUELIST *);
void queue_del(LINKQUEUELIST *);
void queue_clear(LINKQUEUELIST *);
void queue_empty(LINKQUEUELIST *);
void queue_get_front(LINKQUEUELIST *);
void queue_count(LINKQUEUELIST *);
ELEMTYPE add_input();
void print_queue_null();
void menu_method(LINKQUEUELIST *,void (* fun)(LINKQUEUELIST *));
int main(int argc, char *argv[])
{
    int sel_item;
    LINKQUEUELIST * queue = INITQUEUELIST(); //初始化链队列

    menu();
    do
    {
        sel_item = select_menu_item();
        switch(sel_item)
        {
            case -1:
                 break;
            case 0:
                 menu();
                 break;
            case 1:
                 menu_method(queue,queue_add);
                 break;
            case 2:
                 menu_method(queue,queue_del);
                 break;
            case 3:
                 menu_method(queue,queue_clear);
                 break;
            case 4:
                 menu_method(queue,queue_empty);
                 break;
            case 5:
                 menu_method(queue,queue_get_front);
                 break;
            case 6:
                 menu_method(queue,queue_count);
                 break;
            default:
                    menu();
                    break;
        }
    }while(sel_item != -1);
    return 0;
}

LINKQUEUE * INITQUEUE()
{
      LINKQUEUE * result = (LINKQUEUE *)malloc(SIZE);
      return result;
}

LINKQUEUE * CREATE(ELEMTYPE data)
{
      LINKQUEUE * result = INITQUEUE();
      result->data = data;
      return result;
}

LINKQUEUELIST * INITQUEUELIST()
{
    LINKQUEUELIST * result = (LINKQUEUELIST *)malloc(LISTSIZE);
    result->front = INITQUEUE();
    (result->front)->next = NULL;
    result->rear = result->front;
    result->length = 0;
    return result;
}

void ADDQUEUE(LINKQUEUELIST * queue,ELEMTYPE data)
{
    LINKQUEUE * item = CREATE(data);
    (queue->rear)->next = item;
    queue->rear = (queue->rear)->next;
    (queue->rear)->next = NULL;
    queue->length++;
}

ELEMTYPE GET_FRONT(LINKQUEUELIST * queue)
{
    ELEMTYPE result = QUEUE_NULL;
    if (EMPTY(queue) == false)
    {
        result = (queue->front)->next->data;
    }
    return result;
}

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

int CURRENT_SIZE(LINKQUEUELIST * queue)
{
    return queue->length;
}

bool CLEAR(LINKQUEUELIST * queue) //依次将front指向元素销毁即可

{
     bool result = false;
     if (EMPTY(queue) == false)
     {
         do
         {
             DELQUEUE(queue);
         }while((queue->front)->next != NULL);
         result = true;
     }
     return result;
}

ELEMTYPE DELQUEUE(LINKQUEUELIST * queue)
{
    ELEMTYPE result = QUEUE_NULL;
    LINKQUEUE * del;
    if (EMPTY(queue) == false)
    {
        del = (queue->front)->next;
        (queue->front)->next = del->next;
        if (del->next == NULL)
        {
            queue->rear = queue->front;
        }
        result = del->data;
        queue->length--;
        free(del);
    }
    return result;
}

void menu()
{
     print_enter();
     print_tab();print_info("link queue manage system");print_enter();
     print_menu_item("1","add element to queue");
     print_menu_item("2","delete front element");
     print_menu_item("3","clear queue");
     print_menu_item("4","judge queue is empty");
     print_menu_item("5","get queue front elment value");
     print_menu_item("6","get queue element count");
     print_menu_item("0","return menu");
     print_menu_item("-1","exit system");
}
void print_enter()
{
     printf("\n");
}
void print_tab()
{
     printf("\t");
}
void print_str(char * ch)
{
     while(*ch)
     {
         printf("%c",*ch++);
     }
}
void print_info(char * str)
{
     print_tab();
     print_str(str);
     print_enter();
}
void print_menu_item(char * num,char * desc)
{
     print_tab();
     print_str(num);
     print_tab();
     print_str(desc);
     print_enter();
}

int select_menu_item()
{
    int result;
    print_tab();
    print_str("select menu:");
    scanf("%d",&result);
    return result;
}
ELEMTYPE add_input()
{
    ELEMTYPE result;
    print_tab();
    print_str("input data:");
    scanf(SCANF_ITEM,&result);
    return result;
}

void queue_add(LINKQUEUELIST * queue)
{
    ELEMTYPE data = add_input();
    ADDQUEUE(queue,data);
    print_info("add element is ok.");
}
void queue_del(LINKQUEUELIST * queue)
{
     ELEMTYPE result = DELQUEUE(queue);
     if (result != QUEUE_NULL)
     {
         print_tab();
         printf("delete element ");
         printf(PRINT_ITEM,result);
         printf(" is ok.");
         print_enter();
     }
     else
     {
         print_queue_null();
     }
}
void queue_clear(LINKQUEUELIST * queue)
{
     bool result = CLEAR(queue);
     if (result == true)
     {
         print_info("clear queue is ok.");
     }
     else
     {
         print_queue_null();
     }
}
void queue_empty(LINKQUEUELIST * queue)
{
     bool result = EMPTY(queue);
     if (result == true)
     {
         print_info("queue is empty");
     }
     else
     {
         print_info("queue is not empty");
     }
}
void queue_get_front(LINKQUEUELIST * queue)
{
     ELEMTYPE result = GET_FRONT(queue);
     if (result != QUEUE_NULL)
     {
         print_tab();
         printf("the front elemet is ");
         printf(PRINT_ITEM,result);
         printf(".");
         print_enter();
     }
     else
     {
         print_queue_null();
     }
}
void queue_count(LINKQUEUELIST * queue)
{
     int result = CURRENT_SIZE(queue);
     print_tab();
     printf("the count is : %d",result);
     print_enter();
}
void print_queue_null()
{
     print_info("error:queue is null.");
}

void menu_method(LINKQUEUELIST * queue,void (* fun)(LINKQUEUELIST * src_queue))
{
     (* fun)(queue);
}


阅读(4126) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~