zoukankan      html  css  js  c++  java
  • 手把手写数据结构之队列操作

     1#define ARGLEN        50
      2 
      3 #define Test(ARG)     if(ARG == NULL){
      4             printf("Invalid arg.......
    ");
      5             return -1;
      6 }
      7 
      8 //队列任务节点
      9 typedef struct node
     10 {
     11     char arg[ARGLEN+1];            //任务参数
     12     void (* pfun)(char *);        //任务处理函数
     13     struct node *next;            //next结点指针
     14 }Q_NODE;
     15 
     16 //队列信息
     17 typedef struct queue_info
     18 {
     19     int max;        //当前排队任务个数
     20     Q_NODE *head;    //队列头指针
     21     Q_NODE *tail;    //队列尾指针
     22 }QUEUE_INFO;
     23 
     24 
     25 
     26 //创建任务结点
     27 static Q_NODE *__Create_Node__(void (*pfun)(char *), char *arg)
     28 {
     29     Q_NODE *new_node = (Q_NODE *)malloc(sizeof(Q_NODE));
     30     if(NULL == new_node)
     31     {
     32         perror("Create_Node");
     33         return NULL;
     34     }
     35 
     36     new_node->pfun = pfun;
     37     strncpy(new_node->arg, arg, ARGLEN);
     38     
     39     return new_node;
     40 }
     41 
     42 
     43 /****************************************************
     44 *Des:    创建队列
     45 *Ret:    成功返回0,失败返回-1
     46 *******************************************************/
     47 int Create_Queue(QUEUE_INFO **plist)
     48 {
     49     Test(plist);//函数入口检测
     50 
     51     *plist = (QUEUE_INFO *)malloc(sizeof(QUEUE_INFO));
     52     if(NULL == *plist)
     53     {
     54         perror("Create_Queue");
     55         return -1;
     56     }
     57 
     58     (*plist)->head = NULL;
     59     (*plist)->tail = NULL;
     60     (*plist)->max = 0;
     61 
     62     return 0;
     63 }
     64 
     65 
     66 /****************************************************
     67 *Des:    判断队列是否为空
     68 *Ret:    成功返回1,失败返回0
     69 *******************************************************/
     70 int Is_empty_Queue(QUEUE_INFO *plist)
     71 {
     72     return (NULL == plist->head);
     73 }
     74 
     75 
     76 /****************************************************
     77 *Des:    清空队列
     78 *Ret:    成功返回0,失败返回-1
     79 *******************************************************/
     80 int Empty_Queue(QUEUE_INFO *plist)
     81 {
     82     Test(plist);//函数入口检测
     83 
     84     if(Is_empty_Queue(plist))
     85     {
     86         printf("the queue is emtpy!
    ");
     87         return -1;
     88     }
     89 
     90     Q_NODE *fnode = NULL, *pnode = plist->head;
     91     while(NULL != pnode)
     92     {
     93         fnode = pnode;
     94         pnode = pnode->next;
     95         free(fnode);
     96     }
     97 
     98     plist->max = 0;
     99     plist->head = plist->tail = NULL;
    100 
    101     return 0;
    102 }
    103 
    104 /****************************************************
    105 *Des:    入队队操作
    106 *Ret:    成功返回0,失败返回-1
    107 *******************************************************/
    108 int Push_Queue(QUEUE_INFO *plist, void (*pfun)(char *), char *arg)
    109 {
    110     //函数入口检测
    111     if(NULL == plist || NULL == pfun || NULL == arg)
    112     {
    113         printf("Invalid arg........
    ");
    114         return -1;
    115     }
    116 
    117     //创建结点
    118     Q_NODE *new_node = __Create_Node__(pfun, arg);
    119     if(NULL == new_node)
    120     {
    121         return -1;
    122     }
    123 
    124     //判断队列是否为空
    125     if(Is_empty_Queue(plist))
    126     {
    127         plist->head = plist->tail = new_node;
    128     }
    129     else
    130     {
    131         plist->tail->next = new_node;
    132         plist->tail = new_node;
    133     }
    134 
    135     plist->max++;
    136 
    137     return 0;
    138 }
    139 
    140 /****************************************************
    141 *Des:    出队操作(并执行任务)
    142 *Ret:    成功返回0,失败返回-1
    143 *******************************************************/
    144 int Pop_Queue(QUEUE_INFO *plist)
    145 {
    146     Test(plist);//函数入口检测
    147 
    148     //判断队列是否为空
    149     if(Is_empty_Queue(plist))
    150     {
    151         printf("The queue is empty!
    ");
    152         return -1;
    153     }
    154 
    155     Q_NODE *pnode = plist->head;
    156     if(plist->head->next == NULL)//只有一个节点
    157     {
    158         plist->head = plist->tail = NULL;
    159     }
    160     else        
    161     {
    162         plist->head = pnode->next;
    163     }
    164 
    165     //执行任务函数
    166     (pnode->pfun)(pnode->arg);
    167 
    168     free(pnode);
    169     plist->max--;
    170 
    171     return 0;
    172 }
    173 
    174 
    175 /****************************************************
    176 *Des:    获取当前任务节点个数
    177 *Ret:    成功返回0,失败返回-1
    178 *******************************************************/
    179 int Get_count_Queue(QUEUE_INFO *plist)
    180 {
    181     return plist->max;
    182 }
    183 
    184 /****************************************************
    185 *Des:    销毁队列
    186 *Ret:    成功返回0,失败返回-1
    187 *******************************************************/
    188 int Destory_Queue(QUEUE_INFO **plist)
    189 {
    190     Test(plist);    //函数入口检测
    191 
    192     //清空链表
    193     if(Empty_Queue(*plist) < 0)
    194         return -1;
    195 
    196     free(*plist);
    197 
    198     *plist = NULL;
    199 
    200     return 0;
    201 }
  • 相关阅读:
    异常作业
    多态作业
    封装和继承作业
    类和对象作业
    多重循环、方法作业
    选择语句+循环语句作业
    数据类型和运算符作业
    初识Java作业
    C 数据结构堆
    C基础 旋转数组查找题目
  • 原文地址:https://www.cnblogs.com/xuyh/p/3242854.html
Copyright © 2011-2022 走看看