DS Chain Team

Source: Internet
Author: User

DS Chain Team

Blockchain Overview

The queue in the chain is short for the chain team. A chain queue clearly requires two pointers (Head pointer and tail pointer) to indicate the head and tail of the team respectively to be uniquely identified. For ease of operation, we also add a header node to the chain queue as we add the header node to the single-chain table of a linear table and point the header pointer to the header node. Therefore, the judgment condition of the empty chain queue is that both the header pointer and the tail pointer point to the header node.

 

The operations of the chain queue are the insertion and deletion operations of a single-chain table.

 

Element x incoming queue

 

Element y in queue

 

Element x output queue

Basic operations

1. Prerequisites

# Include using namespace std; # include # define OK 1 # define ERROR 0 # define OVERFLOW-2 # define TRUE 1 # define FALSE 0 # define INFEASIBLE-1 typedef int Status; // re-define Status as int type typedef int QElemType; // re-define QElemType as int type typedef struct QNode {// re-define a node Structure QElemType data; struct QNode * next ;} QNode, * QueuePtr; typedef struct {// defines a chain team QueuePtr front; // The team head pointer QueuePtr rear; // team end pointer} LinkQueue; // a schema variable defined

1. initialize the queue.

<Span style = "font-size: 18px;"> </span> // 1 initialize the queue Status InitQueue (LinkQueue & Q) {Q. front = Q. rear = (QueuePtr) malloc (sizeof (QNode); if (! Q. front) {exit (OVERFLOW);} Q. front-> next = NULL; return OK ;}

2. Determine whether the blockchain team is empty.

// 2 check whether the Chain team is empty. Status QueueEmpty (LinkQueue Q) {return (Q. front = Q. rear );}

3. Length of the Chain Team

// 3 determine the queue length Status QueueLength (LinkQueue Q) {int I = 0; QueuePtr p = Q. front; while (p! = Q. rear) {I ++; p = p-> next;} return I ;}

4. Retrieve the element from the queue Header

// 4 retrieve the Header element Status GetHead (LinkQueue Q, QElemType & e) {if (Q. front = Q. rear) {return ERROR;} e = Q. front-> next-> data; return OK ;}

5. Clear the queue

// 5 clear the queue Status ClearQueue (LinkQueue & Q) {Q. rear = Q. front; QueuePtr p = Q. front-> next; Q. front-> next = NULL; while (p) {QueuePtr q = p-> next; free (p); p = q;} return OK ;}

6. Destroy a queue

// 6 destroy the queue Status DestroyQueue (LinkQueue & Q) {while (Q. front) {Q. rear = Q. front-> next; free (Q. front); Q. front = Q. rear;} return OK ;}

7 inbound queue

// 7 inbound queue Status EnQueue (LinkQueue & Q, QElemType e) {QueuePtr p = (QueuePtr) malloc (sizeof (QNode); if (! P) {exit (OVERFLOW);} p-> data = e; p-> next = NULL; Q. rear-> next = p; Q. rear = p; return OK ;}

8-out queue

// 8 output queue 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; if (Q. rear = p) {Q. rear = Q. front;} free (p); return OK ;}

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.