小试牛刀,队列问题
程序代码:
#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)); }