Queues can be implemented either in a linked list or in a sequential table. In contrast to the stack, the stack is usually implemented by sequential tables, and the queue is commonly used to implement the list, which is referred to as the chain queue.
typedef struct QNODE
{
Elemtype data;
struct Qnode *node;
}qnode, *queueprt;
typedef struct
{
Queueprt front, rear; Team head, Tail hands
}linkqueue;
Point the team head pointer to the head node of the chain queue, while the tail pointer points to the endpoint. The head node is not necessary.
Create a queue to accomplish two tasks, one is to create a head node in memory, and the other is to point the head and tail pointers of the queue to the resulting header node, because this is an empty queue.
Initqueue (Linkqueue *q)
{
Q->front=q->rear= (queueptr) malloc (sizeof (Qnode));
if (!q->fronot)
Exit (0);
q->front->next=null;
}
Into the queue operation:
Insertqueue (Linkqueue *q, Elemtype e)
{
Queueptr p;
p= (queuptr) malloc (sizeof (Qnode));
if (p==null)
Exit (0);
p->data=e;
p=>next=null;
q->rear->next=p;
q->rear=p;
}
The out queue is the first element in the queue is moved out, the team head pointer does not change, change the head node of the next pointer. If the original queue has only one element, you should handle the pair of tail pointers.
Deletequeue (linkqueue *q,elemtype *e)
{
Queueptr p;
if (Q->front = = q->rear)
Return
p=q->front->next;
*e=p->data;
q->front->next=p->next;
if (q->rear==p)
q->rear=q->front;
Free (p);
}
Because the chain queue is built in the dynamic area of the memory, it should be destroyed in time when a queue is no longer useful.
Destroyqueue (Linkqueue *q)
{
while (Q->front)
{
q->rear=q->front->next;
Free (Q->front);
q->front=q->rear;
}
}
Data structure Queue 1230