Libevent分解之数据结构Circlequeue

来源:互联网 发布:3dmax专业优化 编辑:程序博客网 时间:2024/05/16 23:35
#ifndef _TEST_STRUCT_C
#define _TEST_STRUCT_C


//结构体定义
struct Student
{
 //业务数据
 const char name[32];
 int id;
 int classID;
 //下面的字段纯粹是为了建立链表用
 struct TstField
 {
  struct Student *sle_next;
 } MyField;
 LIST_ENTRY(Student) ListField;
 SIMPLEQ_ENTRY(Student) SQueueField;
 TAILQ_ENTRY(Student) TQueueField;
 CIRCLEQ_ENTRY(Student) CQueueField;
};
 void* NewStudent();

#endif




#ifndef _TEST_CIRCLE_C
#define _TEST_CIRCLE_C
#include "innerStruct.h"
CIRCLEQ_HEAD(StudentCircleQueue,Student);
struct StudentCircleQueue sutCircleHead;
void TestCircleQueue();
static void OutputAll();
static void OutputAllR();
#endif


#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
#include "innerStruct.h"
#include "CircleQueue.h"
void TestCircleQueue()
{
   struct Student *stu;
   struct Student *stu1;
   struct Student *stu2;
   char tem[32];
   int i;
   CIRCLEQ_INIT(&sutCircleHead);
   //进入队列
   for(i=0;i<10;i++)
   {
  stu=NewStudent();
  if(NULL==stu){return;}
  stu->classID=i;
  stu->id=100;  
  sprintf(tem,"jiayp%d",i+1);
  strcpy(stu->name,tem);
  CIRCLEQ_INSERT_TAIL(&sutCircleHead,stu,CQueueField);  
   }
   OutputAll();   
   OutputAllR();
}
static void OutputAllR()
{
   struct Student *stu3;
    CIRCLEQ_FOREACH_REVERSE(stu3,&sutCircleHead,CQueueField)
   {
  printf(stu3->name);
  printf("\r\n");
   }
printf("\r\n");
}
static void OutputAll()
{
   struct Student *stu3;
     CIRCLEQ_FOREACH(stu3,&sutCircleHead,CQueueField)
   {
  printf(stu3->name);
  printf("\r\n");
   }
printf("\r\n");
}


原创粉丝点击