2010年9月8日星期三

用链表模拟队列(FIFO:first input first output。先进先出)

     1  /*********************************************************************************
     2   *
     3   *头文件
     4   *
     5   *********************************************************************************/
     6  #include    <stdio.h>
     7  #include    <string.h>
     8  #include    <stdlib.h>
     9  
    10  /*********************************************************************************
    11   *
    12   *功能:条件编译,用于显示调试信息
    13   *
    14   *********************************************************************************/
    15  #ifdef d
    16  #define DEBUG(format,...) printf(format,##__VA_ARGS__)  /*输出调试信息*/
    17  #else
    18  #define DEBUG(format,...) do{}while(0)                  /*什么都不做*/
    19  #endif
    20  
    21  /*********************************************************************************
    22   *
    23   *功能:定义全局变量
    24   *
    25   *********************************************************************************/
    26  typedef struct node
    27  {
    28      int x;
    29      struct node *next;
    30  }NODE;
    31  
    32  /*********************************************************************************
    33   *
    34   *功能:链表初始化
    35   *传入参数:结构体指针的地址,使用了二重指针
    36   *
    37   *********************************************************************************/
    38  void init(NODE** phead)
    39  {
    40      if (NULL!=*phead) {
    41          DEBUG("In init ,the phead is NULL.\n");
    42          exit(-1);
    43      }
    44      *phead=(NODE*)malloc(sizeof(NODE));
    45      if(NULL==*phead)
    46      {
    47          DEBUG("In init ,malloc is NULL!\n");
    48          exit(-1);
    49      }
    50      memset(*phead,0,sizeof(NODE));
    51      DEBUG("init success!\n");
    52  }
    53  
    54  /*********************************************************************************
    55   *
    56   *功能:入队列(再链表末尾插入新节点)
    57   *传入参数:链表首节点,要插入的元素地址
    58   *
    59   *********************************************************************************/
    60  void inQue(NODE* phead,NODE* data)
    61  {
    62      if (NULL==phead) 
    63      {
    64          DEBUG("In init ,the phead is NULL!\n");
    65          exit(-1);
    66      }
    67      while(NULL!=phead->next) {
    68          phead=phead->next;
    69      }
    70      phead->next=(NODE*)malloc(sizeof(NODE));
    71      phead=phead->next;
    72      if (NULL==phead)
    73      {
    74          DEBUG("In inQue,malloc is NULL!\n");
    75          exit(-1);
    76      }
    77      phead->x=data->x;
    78      DEBUG("inQue success!\n");
    79      phead->next=NULL;
    80  
    81  }
    82  
    83  /*********************************************************************************
    84   *
    85   *功能:出队列,(从前面依次出列)
    86   *传入参数:链表首节点
    87   *
    88   *********************************************************************************/
    89  void outQue(NODE* phead)
    90  {
    91      NODE* pheadNext=phead->next;
    92  
    93      if (NULL==phead) 
    94      {
    95          DEBUG("In outQue,phead is NULL!\n");
    96          exit(-1);
    97      }
    98      if (NULL==phead->next) 
    99      {
   100          DEBUG("the queue is empty!\n");
   101          exit(-1);
   102      }
   103      phead->next=pheadNext->next;
   104      printf("%d  ",pheadNext->x);
   105      free(pheadNext);
   106  }
   107  
   108  /*********************************************************************************
   109   *
   110   *主函数:用链表模拟队列(入列和出列)
   111   *入列:在链表末尾插入新节点
   112   *出列:从头部依次取出节点数据
   113   *
   114   *********************************************************************************/
   115  int main(int argc, char *argv[])
   116  {
   117      NODE* point=NULL;
   118      NODE  p;
   119      int a[5]={1,2,3,4,5};
   120      int i=0;
   121      int len=sizeof(a)/sizeof(a[0]);
   122  
   123  
   124      init(&point);
   125      for(i=0;i<len;i++)      /*依次入列*/
   126      {
   127          p.x=a[i];
   128          inQue(point,&p);
   129      }
   130      for(i=0; i<len; i++)    /*依次出列*/ 
   131      {
   132          outQue(point);
   133          printf("outQue success!\n");
   134      } 
   135  
   136      return 0;
   137  }

没有评论:

发表评论