注册 登录
编程论坛 数据结构与算法

小试牛刀,队列问题

JJ是也 发布于 2012-05-21 18:22, 430 次点击
程序代码:
#include
#include

typedef struct LQNode
{
    int data;
    struct LQNode* next;
}LQNode, *LinkedQNode;

typedef struct
{
    struct LQNode* front,* rear;
}LQueue, *LinkedQueue;

LinkedQueue LinkedQueueInit ()
{
    LinkedQueue Q;
    LQNode* p;
    Q = (LinkedQueue) malloc (sizeof(LQueue));
    p = (LQNode*) malloc (sizeof(LQNode));
    p->next = NULL;
    Q->front = Q->rear = p;
    return Q;
}

void LinkedQueueIn (LinkedQueue Q, int x)
{
    LQNode* p;
    p = (LQNode*) malloc (sizeof(LQNode));
    p->data = x;
    p->next = NULL;
    Q->rear->next = p;
    Q->rear = p;
}

int LinkedQueueOut (LinkedQueue Q)
{
    LQNode* p;
    int x;
    if (Q->front=Q->rear)
    {
        p = Q->front->next;
        Q->front->next = p->next;
        x = p->data;
        free (p);
        if (Q->front->next==NULL)
            Q->rear = Q->front;
        return x;
    }
}

void main ()
{

    LinkedQueue Q;
    Q = LinkedQueueInit ();
    LinkedQueueIn (Q, 10);
    printf ("%d\n", LinkedQueueOut(Q));

}
1 回复
#2
JJ是也2012-05-21 18:23
请大神打救打救 T_T
1