Basic blockchain queue operations

Source: Internet
Author: User

# Include <stdio. h> // blockchain queue Storage Structure
# Include <stdlib. h>
# Define overflow-2
Typedef int qelemtype;
Typedef struct qnode {// No qnode is added here. The error is reported ??? Why ???
Qelemtype data;
Struct qnode * next;
} Qnode, * queueptr;
Typedef struct {
Queueptr front;
Queueptr rear;
} Linkqueue;
Void initqueue (linkqueue * q)
{// Construct an empty queue Q
Q-> front = Q-> rear = (queueptr) malloc (sizeof (qnode ));
If (! Q-> front) Exit (overflow );
Q-> front-> next = NULL;
Printf ("the queue is successfully constructed.../N ");
}
Void destroyqueue (linkqueue * q)
{// Destroy the queue
While (Q-> front)
{
Q-> rear = Q-> front-> next;
Free (Q-> front );
Q-> front = Q-> rear;
}
Printf ("the queue is destroyed successfully !!! /N ");
Exit (0 );
}
Void enqueue (linkqueue * q, qelemtype E)
{// Insert a new team-end element whose Element E is Q
Queueptr P;
Printf ("OK now which number do you want input/N ");
Scanf ("% d", & E );
P = (queueptr) malloc (sizeof (qnode ));
If (! P) Exit (overflow );
P-> DATA = E;
P-> next = NULL;
Q-> rear-> next = P;
Q-> rear = P;
Printf ("successfully inserted element/N ");
}
Void dequeue (linkqueue * q, qelemtype E)
{// If the queue is not empty, delete the queue Header element of Q and use e to return its value.
Queueptr P;
If (Q-> front = Q-> rear) {printf ("the queue is empty !!! /N cannot be deleted "); exit (0 );}
P = Q-> front-> next;
E = p-> data;
Q-> front-> next = p-> next;
If (Q-> rear = P) q-> rear = Q-> front;
Free (P );
Printf ("deleted successfully! /N ");
}
Void printqueue (linkqueue * q)
{
If (Q-> front = Q-> rear) {printf ("sorry the queue is empty"); exit (0 );}
While (Q-> front! = Q-> rear)
{
Printf ("% d", Q-> front-> next-> data );
Q-> front = Q-> front-> next;
}
Printf ("/nprint is over/N ");
}
Int main ()
{
Linkqueue Q;
Qelemtype E;
Initqueue (& Q );
Int I;
Printf ("Please put you num what you want to do:/N1: destroy the queue/N2: enqueue/N3: dequeue/N4: printqueue/N ");
Scanf ("% d", & I );
While (I)
{
Switch (I)
{
Case 1: destroyqueue (& Q); break; // destroyqueue (linkqueue * q)
Case 2: enqueue (& Q, e); break;
Case 3: dequeue (& Q, e); break;
Case 4: printqueue (& Q); break;
}
Printf ("You can choose again for/N1: Destroy/N2: enqueue/N3: dequeue/N4: printqueue/N ");
Scanf ("% d", & I );
}
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.