Data Structure --- chain queue in C Language

Source: Internet
Author: User

Data Structure --- chain queue in C Language

// Chain queue storage // Yang Xin # include
 
  
# Include
  
   
Typedef int QElemType; // defines the node typedef struct QNode {QElemType data; struct QNode * next;} QNode, * QueuePtr; // defines the pointer typedef struct {QueuePtr front; QueuePtr rear ;} linkQueue; // insert element e into the queue void en_Queue (LinkQueue * q, QElemType e) {QueuePtr temp = (QueuePtr) malloc (sizeof (QNode); if (temp) {temp-> data = e; temp-> next = NULL; q-> rear-> next = temp; q-> rear = temp ;}} // initialize the queue void init_Queue (LinkQueue * q) {q-> front = q-> rear = (QNode *) malloc (sizeof (QNode); q-> front-> next = NULL;} // create a queue void create_Queue (LinkQueue * q) {int n = 0; init_Queue (q); printf ("Enter the elements to enter the queue and end with 0! \ N "); while (scanf (" % d ", & n) {if (n = 0) break; en_Queue (q, n );}} // e element void de_Queue (LinkQueue * q, QElemType * e) {if (q-> front = q-> rear) return; queuePtr temp = q-> front-> next; if (q-> front-> next = q-> rear) q-> rear = q-> front; * e = temp-> data; q-> front-> next = temp-> next; free (temp );} // determine whether the queue is empty int is_Empty (LinkQueue * q) {if (q-> front = q-> rear) return 1; return 0 ;} // return queue int getlength_Queue (LinkQueue * q) {QueuePt R temp = q-> front; int I = 0; while (temp! = Q-> rear) {++ I; temp = temp-> next;} return I;} // clear the queue void clear (LinkQueue * q) {QueuePtr temp = q-> front-> next; while (temp) {QueuePtr tp = temp; temp = temp-> next; free (tp );} temp = q-> front; q-> front = q-> rear = NULL; free (temp);} // print the queue element void print_Queue (LinkQueue * q) {if (q-> front = q-> rear) return; QueuePtr temp = q-> front-> next; while (temp! = Q-> rear) {printf ("% d", temp-> data); temp = temp-> next;} printf ("% d ", temp-> data); printf ("\ n");} // the first data team void top_Queue (LinkQueue * q, QElemType * e) {if (q-> front = q-> rear) return; * e = q-> front-> next-> data;} int main () {int I = 0, k = 0, top = 0; int len; LinkQueue q; create_Queue (& q); top_Queue (& q, & top ); printf ("the element of the queue header is: % d \ n", top); len = getlength_Queue (& q); printf ("all elements in the traversal queue: \ n "); for (I = 0; I <len; I ++) {de_Queue (& q, & k); printf ("% d", k );} printf ("\ n"); clear (& q); return 0 ;}
  
 

 

 

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.