Data structure, data structure and Algorithm

Source: Internet
Author: User

Data structure, data structure and Algorithm
Queue chain implementation

1. chained storage representation of a queue
The chain storage structure of a queue is referred to as a chain queue. It is a single-chain table that restricts the deletion operation on the header and the insertion operation at the end of the table.
Two different types of nodes are required: Data Element nodes, the first and last pointer nodes of the queue.
Pointer node type definition:

typedef struct link_queue{   QNode  *front ,  *rear ;}LinkQueue ;

2. blockchain operations and pointer changes
In fact, the operation of a chain is a single-chain table operation, but it is performed by deleting the table header and inserting it at the end of the table. Different pointers are modified during insertion and deletion.

3 basic operations of the chain Queue (1) initialize the chain queue Status Init_LinkQueue (LinkQueue * Q) {/* returns OK, otherwise ERROR */QNode * p; p = (QNode *) malloc (sizeof (QNode);/* Open the header node */if (p = NULL) return ERROR; p-> next = NULL; q-> front = Q-> rear = p; return OK ;}
(2) An Element e is inserted at the end of a known queue to change the team's tail pointer (Q. rear ). Status Insert_LinkQueue (LinkQueue * Q, ElemType e)/* insert Data Element e to the end of queue Q */{QNode * p; p = (QNode *) malloc (sizeof (QNode); if (! P) return ERROR;/* failed to apply for a new node, ERROR flag */p-> data = e; p-> next = NULL; /* form a new node */Q-> rear-> next = p; Q-> rear = p;/* Insert the new node to the end of the Team */return OK ;}
Status Delete_LinkQueue (LinkQueue * Q, ElemType * x) {QNode * p; if (Q-> front = Q-> rear) return ERROR; /* empty Team */p = Q-> front-> next;/* Get the first node of the Team */* x = p-> data; q-> front-> next = p-> next;/* modify the first pointer of the Team */if (p = Q-> rear) q-> rear = Q-> front;/* When the queue has only one node, you should avoid losing the team's tail pointer */free (p); return OK ;}
(4) revoke the void Destroy_LinkQueue (LinkQueue * Q)/* Forward the first element of the Q queue */{while (Q-> front! = NULL) {Q-> rear = Q-> front-> next;/* point the tail pointer to the first node of the queue */free (Q-> front ); /* each time a node is released * // * The first is the header node, and then the Element Node */Q-> front = Q-> rear ;}}

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.