数据结构-----用链表实现循环队列

实现

  1 #include                                                                                         
  2 #include                                   
  3 #include                                   
  4                                                      
  5 typedef struct Node                                  
  6 {                                                    
  7   int value ;                                        
  8   struct Node *next;                                 
  9 }Node;                                               
 10                                                      
 11 typedef struct Queue                                 
 12 {                                                    
 13   Node *head;          
 14   Node *last;
 15 }Queue;                                                     
 16 
 17 void QInit(Queue *q)
 18 {
 19   q->head = NULL;
 20   q->last = NULL;
 21 }
 22 
 23 void QPush(Queue *q , int v)
 24 {
 25   Node *node = (Node*)malloc(sizeof(Node));
 26   node->value = v;
 27   node->next = NULL;
  28   if(q->head == NULL)
 29   {
 30     q->head = node;
 31     q->last = node;
 32   }
 33   q->last->next = node;
 34   q->last = node;
 35 }
 36 
 37 void QPop(Queue *q)
 38 {
 39   assert(q != NULL);
 40   Node *head = q->head;
 41   q->head = q->head->next;
 42   free(head);
 43   if(q->head == NULL)
 44   {
 45     q->last = NULL;
 46   }
 47 }
 48                                                                                                           
 49 int QFront(Queue *q)
 50 {
 51   return q->head->value;
 52 }
 53 
  54 int QSzie(Queue *q)
 55 {
 56   int size = 0 ;
 57   for(Node *c = q->head ; c != NULL ; c = c->next)
 58   {
 59     size++;
 60   }
 61   return size;
 62 }
 63 
 64 bool QEmpty(Queue *q) {
 65     return q->head == NULL;
 66 }                       


你可能感兴趣的:(数据结构-----用链表实现循环队列)