當前位置: 首頁>>數據結構>>正文


隊列的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/zh-tw/article/90.html,未經允許,請勿轉載。