Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize chain queue in C language

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to implement chain queue in C language. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The implementation of the chained storage structure of the queue is more complex than that of the circular queue, but there is no limit of queue fullness.

Header file declaration

# include # include / * implementation of linked storage of queues * [single linked list of leading nodes] * [- similar to linked stacks, the implementation of linked storage of queues will not be full] / data type typedef int ElemType;// definition node typedef struct SqQueueNode {ElemType data;// data domain struct SqQueueNode* next; / / pointer domain} SqQueueNode;// definition queue typedef struct SqQueueLink {SqQueueNode* front;// header pointer SqQueueNode* rear / / queue tail pointer} SqQueueLink;// initializes queue void InitQueueLink (SqQueueLink* Q); / / determines queue empty int EmptyQueueLink (SqQueueLink Q); / / queue joining operation void EnQueueLink (SqQueueLink* QMagieElemType e); / / dequeue operation void DeQueueLink (SqQueueLink QMagna ElemType * e); / / get queue length int LengthQueueLink (SqQueueLink Q); / / print queue void printSqQueueLink (SqQueueLink Q); / / get queue head element void GetHeadLink (SqQueueLink qfield ElemType * e)

Function realization

# include "SqQueueLink.h" / / initialize queue void InitQueueLink (SqQueueLink* Q) {/ / create header node SqQueueNode* pNode= (SqQueueNode*) malloc (sizeof (SqQueueNode)); pNode- > next=NULL;// pointer field is empty [data field does not store anything] / / initialize queue-[make the head and tail pointers point to the head node] Q-> front=pNode; Q-> rear=pNode;} / / determine the empty int EmptyQueueLink (SqQueueLink Q) {return q.front==q.rear } / / queue operation void EnQueueLink (SqQueueLink * Q rear- ElemType e) {/ / create a new data element node SqQueueNode* newNode= (SqQueueNode*) ElemType e (sizeof (SqQueueNode)); newNode- > data=e;// specify data field newNode- > next=NULL;// pointer field left empty / / queue operation [join queue from end of queue] Q-> rear- > next=newNode; Q-> rear=newNode;} / / dequeue operation void DeQueueLink (SqQueueLink Q element ElemType * e) {/ / [dequeue from head of queue] / / whether to empty if (q.front==q.rear) return; pfront.front-> next;// to get the head node * eqfronp-> data; / / make the header pointer point to the next node q.front-> next=p- > next;// if there is only one node in the original queue, point both the tail pointer and the header pointer to the same node-empty if (q.rear==p) q.rearquarq.front; / / release the original header node free (p). } / / get queue length int LengthQueueLink (SqQueueLink Q) {/ / Auxiliary pointer SqQueueNode* pNode=q.front- > next; int count=0; / / get queue length while (pNoderoomroomq.rear) {count++; pNode=pNode- > next;} return count;} / / print queue void printSqQueueLink (SqQueueLink Q) {/ / Auxiliary pointer SqQueueNode* pairq.front-> next; while (pqueue q.rear) {printf ("% 4d", p-> data); pprinter-> next } printf ("\ n");} / / get the queue header element void GetHeadLink (SqQueueLink QMagneElemTypee) {/ / determine whether the queue is empty if (q.front==q.rear) return; / / get the value of the queue header element * eQualq.front-> next- > data;}

Function test

# include "SqQueueLink.h" int main (int argc,char** argv) {/ / declare queue SqQueueLink sqLink; int i; ElemType data; / / initialize queue InitQueueLink (& sqLink); / / determine whether the queue is empty printf ("is Empty?%d\ n", EmptyQueueLink (sqLink)); / / queue operation for (iqueue 0 / I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report