Implementation of chain queue

Source: Internet
Author: User

Today, we need to use the data structure of the queue for AIS parsing, so I wrote a test myself. Now I will give it to you!

 

The code is included in the attachment.

 

# Include <stdio. h>
# Include <stdlib. h>
# Define max_size 50
Typedef struct qnode
{
Int data; // data domain
Struct qnode * Next; // points to the next element
} Qnode, * queueptr;

Typedef struct
{
Queueptr front; // queue header pointer
Queueptr rear; // The end pointer of the queue.
Int length; // Queue Length
} Linkqueue;

// Construct an empty queue
Void initqueue (linkqueue & Q)
{
Q. Front = Q. Rear = (queueptr) malloc (sizeof (qnode ));
If (! Q. Front)
{
Exit (1 );
}
Q. Front-> next = NULL;
Q. Length = 0;
Return;
}

// Destroy the queue
Void destroyqueue (linkqueue & Q)
{
While (Q. Front)
{
Q. Rear = Q. Front-> next;
If (Q. Front)
{
Free (Q. Front );
}
Q. Front = Q. rear;
}
Q. Length = 0;
}

// Insert elements into the queue
Void enqueue (linkqueue & Q, int e)
{
If (Q. Length = max_size)
{
Printf ("the queue is full! Element cannot be inserted! ");
}
Else
{
Queueptr P = (queueptr) malloc (sizeof (qnode ));
If (! P)
{
Exit (1 );
}
P-> DATA = E;
P-> next = NULL;

Q. Rear-> next = P; // point to the newly inserted Node
Q. Rear = P;
Q. Length ++; // Add 1 to the number of elements
}
}

// Output queue, from the queue Header
Void dequeue (linkqueue * q, int e)
{
// The queue cannot be deleted because it is empty.
If (Q-> front = Q-> rear)
{
Printf ("the queue is empty and cannot be deleted! ");
}

Else
{
Queueptr P = Q-> front-> next; // retrieve the queue Header
E = p-> data;
Q-> front-> next = p-> next; // delete node P
Q-> length --; // The number of elements minus 1

// If the queue has only one element
If (Q-> rear = P)
{
Q-> rear = Q-> front;
}
If (P! = NULL)
{
Free (P );
P = NULL;
}
}
}

// Calculate the queue length
Int getlength (linkqueue * q)
{
Return Q-> length;
}

// Retrieve the queue Header element
Int gethead (linkqueue * q)
{
If (0 = Q-> length)
{
Printf ("the queue is empty! \ N ");
}
Else
{
Return Q-> front-> next-> data;
}
}

// Obtain the end element of the queue
Int gettail (linkqueue * q)
{
If (0 = Q-> length)
{
Printf ("the queue is empty! \ N ");
}
Else
{
Return Q-> rear-> data;
}
}
Void main ()
{
Linkqueue Q;
Initqueue (Q );
Enqueue (Q, 1 );
Enqueue (Q, 2 );
Enqueue (Q, 3 );
// Dequeue (& Q, 0 );

Int Len = getlength (& Q );
Printf ("the queue has several elements in total: % d, % d, % d \ n", Len, gethead (& Q), gettail (& Q ));
Int daxiao = sizeof (qnode );
}

 

 

 

The program running result is as follows:

The queue has several elements in total: 3, 1, 3
Press any key to continue

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.