Data structure (C implementation) ------- ordered queue (non-cyclic queue)

Source: Internet
Author: User

In contrast to the stack, a queue is a first-in-first-out linear table. It only allows Insert at one end of the table, while deleting elements at the other end. This is consistent with the queue in our daily life, and the elements that first enter the queue first leave. In the queue, the end that can be inserted is called the end of the team, and the end that can be deleted is the head of the team.

Ordered queue: The ordered storage structure of the queue. Because the head and tail positions of the queue change, in the queue sequence storage structure, in addition to storing elements from the first to the end of the team in sequence with a set of address contiguous storage units, two front and rear pointers must also be attached to the first and last elements of the team.

For ease of operation, it is agreed that in a non-empty queue, the front pointer always points to the first element, while the rear at the end always points to the next position of the last element.

Type description of the ordered queue:
// Type description of the ordered queue # define maxsize 100 typedef int elemtype; typedef struct {elemtype * data; int front, rear;} sqqueue;

Basic operations:

1. initialize the ordered queue init_sqqueue (sqqueue * q ):

// Initialize the ordered queue void init_sqqueue (sqqueue * q) {q-> DATA = (sqqueue *) malloc (sizeof (sqqueue) * maxsize ); q-> front = Q-> rear = 0 ;}

2. Destroy the ordered queue destroy_sqqueue (sqqueue * q)

// Destroy the void destroy_sqqueue (sqqueue * q) {If (Q-> data) {free (Q-> data ); q-> front = Q-> rear = 0 ;}}

3. Clear the clear_sqqueue (sqqueue * q)

// Clear the void clear_sqqueue (sqqueue * q) {q-> front = Q-> rear = 0 ;}

4. Determine whether the ordered queue is empty. isempty_sqqueue (sqqueue * q)

// Determine whether the ordered queue is empty. Int isempty_sqqueue (sqqueue * q) {If (Q-> front = Q-> rear) return 1; else {return 0 ;}}

5. Determine whether the ordered queue is full. isfull_sqqueue (sqqueue * q)

// Determine whether the ordered queue is full int isfull_sqqueue (sqqueue * q) {If (Q-> rear = maxsize) return 1; elsereturn 0 ;}

Because no extra pointer is set, it is used to determine whether the sequence queue is full. In fact, it is a false overflow, not because the sequence queue is full. The reason is:

As the team enters and leaves the team, the entire queue moves backwards. As a result, the pointer at the end of the team has been moved to the end, and when there are elements in the queue, the team will be full, that is, overflow, in fact, the team is not really full at this time. This phenomenon is called "false overflow ".

6. Get the length of the ordered queue getlength_sqqueue (sqqueue * q)

// Obtain the length of the ordered queue int getlength_sqqueue (sqqueue * q) {return Q-> rear-Q-> front ;}

7. gethead_sqqueue (sqqueue * q, elemtype * X)

// Obtain the queue header void gethead_sqqueue (sqqueue * q, elemtype * X) {If (isempty_sqqueue (q) {printf ("the queue is empty! \ N "); exit (0);} else {* x = Q-> data [q-> front];}

8. getrear_sqqueue (sqqueue * q, elemtype * X)

// Obtain the void getrear_sqqueue (sqqueue * q, elemtype * X) {If (isempty_sqqueue (q) {printf ("the ordered queue is empty! \ N "); exit (0);} else {* x = Q-> data [q-> rear-1];}

9. en_sqqueue (sqqueue * q, elemtype X)

// Void en_sqqueue (sqqueue * q, elemtype X) {q-> data [q-> rear] = x; q-> rear ++ ;}

10. de_sqqueue (sqqueue * q, elemtype * X)

// Output sequence queue void de_sqqueue (sqqueue * q, elemtype * X) {If (isempty_sqqueue (q) {printf ("sequence queue is empty! \ N "); exit (0);} else {* x = Q-> data [q-> front]; q-> front ++ ;}}

11. Print the ordered queue print_sqqueue (sqqueue * q)

// Print the order queue void print_sqqueue (sqqueue * q) {int I = Q-> front; If (isempty_sqqueue (q) {printf ("the order queue is empty! \ N "); exit (0);} else {While (I <q-> rear) printf (" % d \ t ", q-> data [I ++]); printf ("\ n ");}}

Note:

In this order, the queue will have a "false overflow". As the queue enters and leaves, the entire queue will move backwards as a whole, so that the team's tail pointer has been moved to the end, when another element is added to the team, the team will be full, that is, overflow. In fact, the team is not really full.

To fully utilize the space and solve the "false overflow" problem of sequential queues, two methods can be used: one is to move the data forward and leave the empty storage unit at the end of the team; the other is to construct an ordered queue into a ring space, that is, data in the data partition of the queue [0 .... MAXSIZE-1] is seen as a loop structure that connects the head and end, so that data [0] is connected after data [MAXSIZE-1], which is a loop queue.







 

 

 

Data structure (C implementation) ------- ordered queue (non-cyclic queue)

Related Article

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.