Data structure Queue 1230

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.