当前位置: 首页>>数据结构>>正文


队列的C语言实现(原创)

使用链表实现的队列,源码用C语言编写,VC6.0编译。代码有三个文件,其中cqueue.h是队列数据结构和函数接口的声明,cqueue.c是队列函数的定义,main.c是队列测试文件。

/*
* File: cqueue.h
* Purpose: implementation of queue in c
* Author: puresky
*/

#ifndef _C_QUEUE_H
#define _C_QUEUE_H

typedef struct QueueNodeStruct QueueNode;
struct QueueNodeStruct
{
      void *_data;
      struct QueueNodeStruct *_next;
};

typedef struct QueueStruct Queue;
struct QueueStruct
{
      QueueNode *_head;
      QueueNode *_tail;
      int _count;
};

Queue *queue_new();
void queue_free(Queue *q);
void queue_push_back(Queue *q, void *data);
void *queue_pop_front(Queue *q);
int queue_size(Queue *q);
int queue_empty(Queue *q);
#endif

 

/*
* File: cqueue.c
* Purpose: implementation of queue in c
* Author: puresky
*/

#include <stdlib.h>
#include "cqueue.h"

QueueNode *queue_node_new(void *data)
{
      QueueNode *qn = (QueueNode *)malloc(sizeof(QueueNode));
      qn->_data = data;
      qn->_next = NULL;
      return qn;
}
void queue_node_free(QueueNode *qn)
{
      if(qn)
      {
            free(qn);
      }
}

Queue *queue_new()
{
      Queue *q = (Queue *)malloc(sizeof(Queue));
      q->_count = 0;
      q->_head = NULL;
      q->_tail = NULL;
      return q;
}
void queue_free(Queue *q)
{
      QueueNode *qn;
      QueueNode *iter;
      if(q)
      {
            iter = q->_head;
            while(iter)
            {
                  qn = iter;
                  iter = iter->_next;
                  queue_node_free(qn);
            }
      }
}
void queue_push_back(Queue *q, void *data)
{
      QueueNode *qn = queue_node_new(data);
      if(q->_head == NULL)
            q->_head = q->_tail = qn;
      else
      {
            q->_tail->_next = qn;
            q->_tail = qn;
      }
      q->_count++;
}
void *queue_pop_front(Queue *q)
{
      void *ret = NULL;
      QueueNode *qn;
      if(q->_head)
      {
            ret = q->_head->_data;
            qn = q->_head;
            q->_head = q->_head->_next;
            if(q->_head == NULL)
                  q->_tail = NULL;
            queue_node_free(qn);
            q->_count--;
      }
      return ret;
}
int queue_size(Queue *q)
{
      return q->_count;
}
int queue_empty(Queue *q)
{
      if(q->_head == NULL)
            return 1;
      return 0;
}

/*
* File: main.c
* Purpose: To test the queue in c
* Author: puresky
*/

#include <stdio.h>
#include <stdlib.h>
#include "cqueue.h"

int main(int argc, char **argv)
{
      int data;
      Queue *q = queue_new();
      queue_push_back(q, (void*)1);
      queue_push_back(q, (void*)3);
      queue_push_back(q, (void*)5);
      queue_pop_front(q);
      queue_push_back(q, (void*)7);
      queue_push_back(q, (void*)9);
      queue_push_back(q, (void*)11);
      printf("size:%d\n", queue_size(q));
      while(!queue_empty(q))
      {
            data = (int)queue_pop_front(q);
            printf("%d\n", data);
      }
      queue_free(q);
     
      system("pause");
      return 0;
}
本文由《纯净天空》出品。文章地址: https://vimsky.com/article/90.html,未经允许,请勿转载。