C language implementation of blockchain queue

Source: Internet
Author: User
# Include <stdio. h> # include <stdlib. h> # define error 0 # define OK 1 typedef int status; typedef int elemtype; typedef struct lnode {elemtype data; lnode * Next;} lnode, * link; typedef struct queue {link head; Link tail; int Len;} queue; Status Init (queue & qq) {QQ. head = (Link) malloc (sizeof (lnode); If (qq. head = NULL) Return Error; QQ. tail = QQ. head; QQ. head-> next = NULL; QQ. len = 0; Return OK;} status Ge Ttop (queue & QQ, elemtype & E) {If (qq. head = QQ. tail) Return Error; E = QQ. head-> next-> data; Return OK;} status enqueue (queue & QQ, elemtype e) {link temp; temp = (Link) malloc (sizeof (lnode )); if (temp = NULL) Return Error; temp-> DATA = E; QQ. tail-> next = temp; QQ. tail = temp; QQ. len ++; QQ. tail-> next = NULL; Return OK;} status dequeue (queue & QQ, elemtype & E) {If (qq. head = QQ. tail) Return Error; E = QQ. head-> next-> data; Link P = QQ. head-> next; free (p); P = NULL; QQ. head-> next = QQ. head-> next; If (qq. head-> next = NULL) // when the last element leaves the queue, the tail node Pointer Points to the first node QQ. tail = QQ. head; Return OK;} status destroyqueue (queue & qq) {link Q, TMP; q = QQ. head-> next; free (qq. head); QQ. head = NULL; while (Q! = NULL) {TMP = Q-> next; free (Q); q = TMP;} TMP = NULL; Return OK;} int main () {queue QQ; int m, i; Init (qq); printf ("How many Nums? \ N "); scanf (" % d ", & M); for (I = 0; I <m; I ++) {int e; scanf ("% d", & E); enqueue (QQ, e) ;}int D; gettop (QQ, d); printf ("% d \ n ", d); for (I = 0; I <m; I ++) {int e; dequeue (QQ, e); printf ("% d", e );} printf ("\ n"); System ("pause"); Return 0 ;}

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.