Basic C language implementation of the "ADT" queue

Source: Internet
Author: User

Queue.h

1 #ifndef _queue_h_2 #define_queue_h_3#include <stdbool.h>4 #defineMaxqueue 105 6 //Defining queue Types7typedefintItem;8 9 //node of the queueTentypedefstructnode one { a Item item; -     structNode *next; - }node; the  -typedefstructQueue - { -Node * front;//record the first item of a queue +Node * rear;//record the last item in a queue -     intItems//number of record items + }queue; a  at voidInitqueue (Queue *pq); - BOOLQueueisfull (ConstQueue *pq); - BOOLQueueisempty (ConstQueue *pq); - intQueueitemcount (ConstQueue *pq); - BOOLEnQueue (item item, Queue *pq); - BOOLDeQueue (Item * pitem, Queue *pq); in voidEmptythequeue (Queue *pq); -  to #endif

use_q.c/* function Function Implementation */

1#include <stdio.h>2#include <stdlib.h>3#include"queue.h"4 5 Static voidCopytonode (item item, Node * Pn)//copy content to a node6 {7Pn->item =item;8 }9 Static voidCopytoitem (Node * pn, Item * pi)//copies the item contents of an item to an item variableTen { one*pi = pn->item; a } -  - voidInitqueue (Queue * Pq)//Initialize Queue the { -Pq->front = Pq->rear = NULL;//Initialize the Queue's end-to-end pointers to null -Pq->items =0;//Initialize the number of items to 0 - } + BOOLQueueisfull (ConstQueue *Pq) - { +     returnPq->items = =maxqueue; a } at BOOLQueueisempty (ConstQueue *Pq) - { -     returnPq->items = =0; - } - intQueueitemcount (ConstQueue *Pq) - { in     returnPq->items; - } to BOOLEnQueue (item item, Queue * pq)//add an item at the end of the queue + { -Node * pnew;//to create a new node the      *     if(queueisfull (pq)) $         return false;Panax NotoginsengPnew = (Node *)malloc(sizeof(Node));//request space for a new node -     if(pnew = =NULL) the     { +fprintf (stderr,"Unable to allocate memory!\n"); a Exit (exit_failure); the     } +Copytonode (item, pnew);//Copy the item content to the new node -Pnew->next = NULL;//place the next member of the new node NULL to indicate that this is the last item in the current queue $     if(queueisempty (pq))//if the queue is empty, the new node is used as the header of the queue $Pq->front =pnew; -     Else //otherwise, Place the address of the new node in the next member of the tail item of the queue -Pq->rear->next =pnew; thePq->rear = pnew;//to use a new node as the tail item of a queue -pq->items++;Wuyi      the     return true; - } wu  - BOOLDeQueue (Item * pitem, Queue *Pq) about { $Node *pt; -     //the Pitem and PT are used to store the contents of the deleted Item. -     if(queueisempty (pq)) -         return false; a     //Copy the contents of the deleted item to the temporary pointer +Copytoitem (pq->front, pitem); thePT = pq->front; -      $Pq->front = pq->front->next; the      free(pt); thepq->items--; the     if(pq->items = =0)//When you delete the last item, you set the tail pointer and the head pointer to null at the same time.  thePq->rear =NULL; -     return true; in } the voidEmptythequeue (Queue *Pq) the { about Item dummy; the      while(!Queueisempty (pq)) theDeQueue (&dummy, pq); the}

main.c/* user Interface */

1#include <stdio.h>2#include"queue.h"3 4 intMainvoid)5 {6 Queue line;7 Item temp;8     Charch;9 TenInitqueue (&line ); onePuts"Testing the Queue interface. Type A To add a value,"); aPuts"Type D To delete a Value,and type q to Quit."); -      while(ch = GetChar ())! ='Q') -     { the         if(ch! ='a'&&ch! ='D') -             Continue; -         if(ch = ='a') -         { +printf"Interger to Add:"); -scanf"%d", &temp); +             if(! Queueisfull (&line )) a             { atprintf"putting%d into queue\n", temp); -EnQueue (temp, &line ); -             } -             Else -Puts"Queue is full!"); -         } in         Else -         { to             if(queueisempty (&line )) +Puts"Nothing to delete!"); -             Else the             { *DeQueue (&temp, &line ); $printf"%removing%d from queue\n", temp);Panax Notoginseng             } -         } theprintf"%d items in queue\n", Queueitemcount (&line )); +Puts"Type A to Add,d to Delete,q to Quit:"); a     } theEmptythequeue (&line ); +Puts"bye!"); -  $     return 0; $}

Basic C language implementation of the "ADT" queue

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.