queue.c

来自「一个linux下的各种组播路由算法编程」· C语言 代码 · 共 150 行

C
150
字号
/*****************************************************************************
***                      Author: Hussein F. Salama                         ***
***                       Date: September 9, 1994                          ***
***                            File: queue.c                               ***
***  A C++ library of classes to represent FIFO queues and priority queues ***    
*****************************************************************************/
#include "queue.h"

int FIFOQueue::add_to_q(Data d) {

   //add a data packet to the queue
   DataList *temp;

   if (packets_inq < q_max) {
      temp = new DataList;
      temp->data(d);
      temp->next(NULL);
      if (hd != NULL) {
         tl->next(temp);
         tl = temp;
      }
      else { hd = tl = temp; };
      ++packets_inq;
      return(WasNotFull);
      
   }
   else return(WasFull);
};

Data FIFOQueue::remove_from_q() {

   //remove a data packet from the queue
   DataList *temp;
   Data d;            

   temp = hd;
   if (hd != NULL) {   q_max = QMAX;

      d = *(temp->data());
      if (hd == tl) { hd = tl = NULL; }
      else { hd = hd->next(); };
      delete temp;
      packets_inq -= 1;
   }
   else { d.type(Nonsense); }; //for the case that the queue is empty
   return(d);
};      

FIFOQueue::~FIFOQueue() {

   clearQ();
};

void FIFOQueue::clearQ() {
   //remove all packets from the queue
   while (hd != NULL) remove_from_q();
};

PriorityQueue::PriorityQueue(int l) {

   levels = l; 
   packets_inq = 0; 
   highestPrQ = NULL;
   int i;
   for (i = 0; i < l; i++) {
      QueueListEntry *tmp = new QueueListEntry(i);
      tmp->next(highestPrQ);
      highestPrQ = tmp;
   };
};

PriorityQueue::~PriorityQueue() {

   QueueListEntry *tmp = highestPrQ;
   QueueListEntry *tmp2;
   while (tmp != NULL) {
      tmp2 = tmp->next();
      delete tmp;
      tmp = tmp2;
   };
};

Data PriorityQueue::remove_from_q() {

   //remove a data packet from the queue
   Data d;
   QueueListEntry *tmp = highestPrQ;
   while (tmp->queue()->QisMT() == 1) tmp = tmp->next();

   if (tmp != NULL) {
      d = tmp->queue()->remove_from_q();
      packets_inq--;
   }
   else d.type(Nonsense); //for the case that the queue is empty
   return(d);
};      

int PriorityQueue::add_to_q(Data d) {

   //add a data packet to the queue
   if (packets_inq < q_max) {
      QueueListEntry *tmp = highestPrQ;
      while ((tmp != NULL) && (tmp->priority() != d.priority())) 
                                              tmp = tmp->next();
      if (tmp != NULL) tmp->queue()->add_to_q(d);
      ++packets_inq;
      return(WasNotFull);
   }
   else return(WasFull);
};

void PriorityQueue::clearQ() {

   //remove all packets from the queue
   QueueListEntry *tmp = highestPrQ;
   while (tmp != NULL) {
      tmp->queue()->clearQ();
      tmp = tmp->next();
   };
   packets_inq = 0;
};

DataList *PriorityQueue::head() {

   //get a pointer to the data packet at the head of the queue
   QueueListEntry *tmp = highestPrQ;
   while ((tmp != NULL) && (tmp->queue()->packetsInQ() == 0)) tmp = tmp->next();
   if (tmp != NULL) {
      Queue *q = tmp->queue();
      if (q != NULL) {
       DataList *d = q->head();
       return(d);
      };
   }
   else {
      return(NULL);
   };
};

void PriorityQueue::size(int s) {

   //change the size of the priority queue
   q_max = s;
   QueueListEntry *tmp = highestPrQ;
   while (tmp != NULL) {
      tmp->queue()->size(s);
      tmp = tmp->next();
   };
};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?