链队列的基本操作

#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1

typedef struct QNode
{
    int data;
    struct QNode *next;
}QNode, *QueuePtr;

typedef struct 
{
    QueuePtr front, rear;
}LinkQueue;

int InitQueue(LinkQueue &Q)
{
   Q.front = Q.rear = (QNode *)malloc(sizeof(QNode));
   if(!Q.front)exit(OVERFLOW);
   Q.front->next = NULL;
   return OK;
}//队列初始化

int DestroyQueue(LinkQueue &Q)
{
    while(Q.front)
    {
        Q.rear = Q.front->next;
        free(Q.front);
        Q.front = Q.rear;
    }
    return OK;
}//销毁队列

int ClearQueue(LinkQueue &Q)
{
    QNode *p = Q.front -> next;
    while(p)
    {
        p = p->next;
        free(Q.front->next);
        Q.front->next = p;
    }
    Q.rear = Q.front;
    return OK;
}//清空队列

int QueueEmpty(LinkQueue Q)
{
    if(Q.front == Q.rear)
        return TRUE;
    return FALSE;
}

int QueueLength(LinkQueue Q)
{
    if(Q.front == Q.rear)
        return 0;
    int j = 0;
    QNode *p = Q.front->next;
    while(p)
    {
        p = p->next;
        j++;
    }
    return j;
}//求队长

int GetHead(LinkQueue Q, int e)
{
    if(Q.front == Q.rear)
        return ERROR;
    e = Q.front->next->data;
    return  OK;
}//取队头元素

int EnQueue(LinkQueue &Q, int e)
{

    QNode *p = (QNode *)malloc(sizeof(QNode));
    if(!p)
        exit(OVERFLOW);
    p->data = e;
    p->next = NULL;
    Q.rear->next = p;
    Q.rear = p;
    return OK;
}//入队

int DeQueue(LinkQueue &Q, int &e)
{
    if(Q.front == Q.rear)
        return ERROR;
    QNode *p = Q.front->next;
    e = p->data;
    Q.front->next = p->next;
    if(Q.rear == p)
        Q.rear = Q.front;
    free(p);
    return OK;
}//出队

int QueueTraves(LinkQueue Q, void (*visit)(QNode node))
{
    if(Q.front == Q.rear)
    {
        printf("\n队列为空\n");
        return ERROR;
    }
    QNode *p = Q.front->next;
    printf("\n队列中的元素是:");
    while(p)
    {
        visit(*p);
        p = p->next;
    }
    return OK;
}//遍历队列

void PrintNode(QNode node)
{
    printf("%d ", node.data);
}//打印node数据


void PrintMenu()
{
    printf("\n      链队列基本操作        \n");
    printf("\n    -------------------     \n");
    printf("\n*      1-入队              *\n");
    printf("\n*      2-出队              *\n");
    printf("\n*      3-遍历              *\n");
    printf("\n*      4-清空队列          *\n");
    printf("\n*      5-求队列长度        *\n");
    printf("\n*      6-取队头元素        *\n");
    printf("\n*      0-退出        *\n");
    printf("\n    -------------------     \n");
    printf("请选择菜单号(0-6):");
    
}
int main()
{
    LinkQueue Q;
    InitQueue(Q);
    int e, z = 1, cdh;
    while(z)
    {
        do
        {
            PrintMenu();
            scanf("%d", &cdh);
            if(cdh < 0 || cdh > 6)
                   printf("菜单号输入错误,请重新输入!\n");
        }while(cdh < 0 || cdh > 6);
        switch(cdh)
            {
                case 0:
                    z = 0; break;
                case 1://入队
                    printf("请输入入队元素:");
                    scanf("%d", &e);
                    if(EnQueue(Q, e))
                            printf("\n元素%d入队成功!\n",e);
                    else
                        printf("\n元素%d入队失败!\n",e);
                    break;
                case 2://出队
                    if(DeQueue(Q, e))
                            printf("\n出队成功!出队元素是%d\n",e);
                    else
                        printf("\n出队失败!\n");
                    break;
                case 3://遍历
                    QueueTraves(Q, PrintNode);
                    putchar('\n');
                    break;
                 case 4://清空队列
                     if(ClearQueue(Q))
                             printf("\n清空队列成功!\n");
                     else
                         printf("\n清空队列失败!\n");
                     break;
                 case 5 ://求队长
                     printf("\n队列长度是:%d\n",QueueLength(Q));
                     break;
                 case 6 ://取队头元素
                     if(GetHead(Q, e))
                         printf("队头元素是:%d\n",e);
                     else
                         printf("取队头元素失败!\n");
                     break;
            }
    }
    return 0;
}

 

posted @ 2015-10-30 15:30  午夜阳光~  阅读(418)  评论(0编辑  收藏  举报