Blockchain queue implementation

Source: Internet
Author: User
// ------------------------------ Queue ----------------------------------------//
// The opposite of a queue and a stack is a first-in-first-out (FIFO) linear table. It only allows Insert at one end of the table, while deleting elements at the other end
// The end that can be inserted is called a rear and the end that can be deleted is called a front)
// Add a header node to the chain queue and point the header pointer to the header node. Condition for determining an empty chain queue: both the header pointer and the tail pointer point to the header node.
// The operation of the chain queue is a special case of single-chain table insertion and deletion.

//-----------------------------------------------------------------------------//

Function. h

# Define status int # define overflow-2 # define true 1 # define false 0 # define OK 1 # define error 0 typedef int qelemtype; typedef struct qnode {qelemtype data; struct qnode * Next;} qnode, * queueptr; typedef struct {queueptr front; queueptr rear;} linkqueue; void visit_print (queueptr P); Status initqueue (linkqueue * q ); // construct status destroyqueue (linkqueue * q); // destroy status clearqueue (linkqueue * q); // clear status queueempty (linkqueue Q); // if it is null, true is returned, otherwise, return falsestatus queuelength (linkqueue Q); // return the queue length status gethead (linkqueue Q, qelemtype * E); // return the queue Header element status enqueue (linkqueue * Q, qelemtype E); // insert element e into the new team end element status dequeue (linkqueue * q, qelemtype * E); // If the queue is not an empty return Header element, otherwise, the returned errorstatus queuetraverse (linkqueue Q, void (* visit) (queueptr); // calls the visit () function for each element in the queue from the head of the team to the end of the team (), the operation fails once it fails.

Function. c

# Include "function. H "# include <stdio. h> # include <stdlib. h> void visit_print (queueptr p) {printf ("% d", p-> data);} status queueempty (linkqueue q) {If (Q. front = Q. rear) {printf ("the queue is empty! \ N "); Return OK;} else return error;} status initqueue (linkqueue * q) {q-> front = Q-> rear = (queueptr) malloc (sizeof (qnode); // generates a header node. The data field is empty if (! Q-> front) Exit (overflow); // allocation failed Q-> front-> next = NULL; Return OK;} status enqueue (linkqueue * q, qelemtype E) {queueptr P; P = (queueptr) malloc (sizeof (qnode); P-> DATA = E; P-> next = NULL; q-> rear-> next = P; q-> rear = P; // Return OK after the tail pointer;} status dequeue (linkqueue * q, qelemtype * E) {If (Q-> front = Q-> rear) Return Error; queueptr P = (queueptr) malloc (sizeof (qnode )); P = Q-> front-> next; * E = p-> Data; q-> front-> next = p-> next; // The first pointer moves back If (Q-> rear = P) q-> rear = Q-> front; free (p); Return OK;} status destroyqueue (linkqueue * q) {queueptr P = Q-> front = Q-> rear; free (p); Return OK ;} status clearqueue (linkqueue * q) {qelemtype * E = (qelemtype *) malloc (sizeof (qelemtype); While (Q-> front! = Q-> rear) {dequeue (Q, e);} Return OK;} int queuelength (linkqueue q) {int length = 0; while (Q. Front! = Q. rear) {q. front = Q. front-> next; length ++;} return length;} status gethead (linkqueue Q, qelemtype * E) {If (Q. front = Q. rear) Return Error; * E = Q. front-> next-> data; Return OK;} status queuetraverse (linkqueue Q, void (* visit) (queueptr) {If (Q. front = Q. rear) Return Error; qnode * P; For (P = Q. front-> next; P = p-> next) {visit (p);} printf ("\ n"); Return OK ;}

Main. c

// ------------------------------ Queue ------------------------------------------ // The queue is opposite to the stack and is a linear table of first-in-first-out (FIFO. It only allows insertion at one end of the table, while deleting elements at the other end // the end that allows insertion is called a rear and the end that allows deletion is called a front) // Add a header node to the chain queue and point the header pointer to the header node. Condition for determining an empty chain queue: the header pointer and the tail pointer point to the header node // The operation of the chain queue is a special case of the single-chain table insertion and deletion operations // rule // # include <stdio. h> # include <stdlib. h> # include "function. H "int main (void) {linkqueue Q; qelemtype E; int I; initqueue (& Q); queueempty (Q); for (I = 0; I <5; I ++) {scanf ("% d", & E); enqueue (& Q, e);} printf ("the queue are:"); queuetraverse (Q, visit_print); printf ("the queue's length is % d \ n", queuelength (q); gethead (Q, & E ); printf ("the head member of queue is % d \ n", e); dequeue (& Q, & E ); printf ("Delete the head member in queue is % d \ n", e); printf ("after Delete the head node, queue are:"); queuetraverse (Q, visit_print); printf ("the queue length is % d \ n", queuelength (q); clearqueue (& Q); queueempty (Q); Return 0 ;}

Running result:


Blockchain queue implementation

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.