Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the basic operation of chain queue in C language data structure?

Shulou Source: shulou.com Published: 2022-06-02 04:49:53 10月05日 Update

This article will explain in detail the basic operation of chain queue in the data structure of C language, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

1. Definition of queues

Queue is another kind of limited linear table, which only allows elements to be inserted at one end of the table and deleted at the other end, so queues have the feature of first-in, first-out (FIFO). In a queue, the end that is allowed to insert is called the end of the queue (rear), and the end that allows deletion is called the head of the queue (front). Suppose the team is listed as Q = (A1 ~ a2, … An), so A1 is the head of the line and an is the end of the line. The elements in the queue are according to A1, A2, … If you enter the queue in the order of an, you must exit the queue in the same order, that is to say, only in A1, A2, … An can exit the queue only after an-1 and an have all left the queue.

two。 Representation and implementation of queues

Chain queues can be defined as follows:

# define TRUE 1#define FALSE 0typedef struct QNode {QElemType data; struct QNode * next;} QNode, * QueuePtr;typedef struct {QueuePtr front; QueuePtr rear;} LinkQueue; (1) initialize the operation Status InitQueue (LinkQueue & Q) {Q.front = Q.rear = (Queueptr) malloc (sizeof (QNode)); if (! Q.front) exit (OVERFLOW); Q.front-> next = NULL; return OK } (2) destroy queue Status DestroyQueue (LinkQueue & Q) {while (Q.front) {Q.rear = Q.front-> next; free (Q.front); Q.front = Q.rear;} return OK;} (3) queue operation Status EnQueue (LinkQueue & Q, QelemType e) {p = (QueuePtr) malloc (sizeof (QNode)); if (! p) exit (OVERFLOW) P-> data = e; p-> next = NULL; Q.rear-> next = p; Q.rear = p; return OK;} (4) team operation Status DeQueue (LinkQueue & Q, QelemType & e) {if (Q.front = = Q.rear) return ERROR; packs Q.front-> next; eBay-> data; Q.front-> next = p-> next If (Q.rear = = p) Q.rear = Q.front; free (p); return OK;} Appendix complete Code: # includeusing namespace std;#define OK 1#define FALSE 0typedef int QElemType;typedef int Status;typedef struct QNode {QElemType data; struct QNode * next;} QNode,*QueuePtr;typedef struct {QueuePtr font; QueuePtr near;} LinkQueue;Status InitQueue (LinkQueue & Q) {Q.font = (QueuePtr) malloc (sizeof (QNode)) If (! Q.font) exit (FALSE); Q.Font-> next=NULL; Q.neartire Q.Font; return OK;} Status QueueEmpty (LinkQueue Q) {if (Q.font-> next) return OK; return FALSE;} Status EnQueue (LinkQueue & QMJ QElemType e) {QueuePtr p = (QueuePtr) malloc (sizeof (QNode)); p-> data=e; Q.near-> next= p; Q.near = Q.near-> next; p-> next=NULL; return OK } Status DeQueue (LinkQueue & QMagol QElemType & e) {if (! Q.font-> next) return FALSE; QueuePtr p; pendant Q.font-> next; estrangp-> data; Q.Font-> next=p- > next; if (Q.near==p) Q.nearhammer Q.font; / / when it is the last element, Q.fontNull is Q.nearcircle Q.font free (p); return OK;} Status ClearQueue (LinkQueue & Q) {QueuePtr p; pendant Q.font-> next; QueuePtr q While (p) {qroomp; pairp-> next; Q.font-> next=p; free (Q);} Q.nearhammer Q.font; return OK;} void menu () {cout

Tags: Queues elements one end basic operations data data structures structures languages content articles more knowledge articles good successful that is code advanced only Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Microsoft MariaDB Shulou Tech Info Huawei