The realization of data structure by C language: sequential queue, chain queue and circular queue

Time:2020-10-2

Source: http://blog.seclibs.com/ Data…, chain queue, circular queue-c language implementation/

In the last article, we talked about the related contents of queues. In addition to the sequential queues, chained queues and circular queues mentioned in this article, we also mentioned blocking queues and concurrent queues. These two queues have not yet been implemented due to their capabilities. We will fill in the holes later.

Back to the implementation of the three queues, the first is the sequential queue, which is based on the array. Based on the implementation of the original array, two structural members, head and tail, are added to identify the queue head and tail.

The realization of data structure by C language: sequential queue, chain queue and circular queue

The others have not changed much. The code is as follows

If you need to download the code, please move to the end of the article

The realization of data structure by C language: sequential queue, chain queue and circular queue


Next, let’s talk about the chain queue. There are some differences between the implementation of the chain queue and the previous implementation. Because both the linked list and the queue need pointers, we need to pay special attention to the definition of two structures here, and we also need to focus on understanding the meaning in the later code implementation.

When defining the structure, I use typedef struct and struct. I can feel the difference between them in the code.

The realization of data structure by C language: sequential queue, chain queue and circular queue

Because there is a linked list in it, you need to pay attention to releasing the memory space of the out of queue element when you are out of the queue. There are not too many changes for other elements. The code is as follows

If you need to download the code, please go to the end of the article

The realization of data structure by C language: sequential queue, chain queue and circular queue


Finally, we talk about circular queue. The difficulty of circular queue implementation is similar to that of sequential queue. The difficulty lies in how to return the variable to 0 and start again when the queue is full once. Here we still use the formula used in the previous article to determine when the queue is full. When the head is increased to the maximum, how to return 0, head = (head + 1)% size, you can think about it carefully. The others are all one In the same way.

The code is as follows, you can think about it yourself

If you need to download the code, please move to the end of the article

The realization of data structure by C language: sequential queue, chain queue and circular queue


code:

Sequential queue: GitHub

Linked queue: GitHub

Circular queue: GitHub

First official account and personal blog

Official account: mindless sleepwalking (wuxinmengyi)

The realization of data structure by C language: sequential queue, chain queue and circular queue

Blog: http://blog.seclibs.com/