数据结构队列操作在保证数据稳定、有序的传输方面是很有用的,通过有序的出队和入队操作来完成数据先后的过程。
记录一下。。。。。。。。。
链式队列实现例子:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
typedef struct _node{
char data[1024];
struct _node *next;
}node;
typedef struct{
struct _node *front;
struct _node *rear;
int size;
}queue;
queue *init_queue()
{
queue *q=(queue *)malloc(sizeof(queue));
if(q==NULL)
{
perror("init queue");
exit(1);
}
q->size = 0;
q->front=q->rear=NULL;
return q;
}
node *new_node(char *data)
{
node *new=(node*)malloc(sizeof(node));
if(new==NULL)
{
perror("new node");
exit(1);
}
strcpy(new->data,data);
new->next=NULL;
return new;
}
bool is_empty(queue *q)
{
return q->size==0;
}
int en_queue(queue *q,node *new)
{
if(new==NULL)
{
return -1;
}
if(is_empty(q))
{
q->front=q->rear=new;
}
else
{
q->rear->next=new;
q->rear=new;
}
q->size++;
return 0;
}
int de_queue(queue *q,node **data)
{
if(is_empty(q))
{
return -1;
}
node *tmp=q->front;
if(q->front==q->rear)
{
q->front=q->rear=NULL;
}
else
{
q->front=q->front->next;
}
if(data==NULL)
{
free(tmp);
}
else
{
tmp->next=NULL;
*data=tmp;
}
q->size--;
return 0;
}
void show_queue(queue *q)
{
if(is_empty(q)) return;
node *tmp=q->front;
while(tmp!=NULL)
{
printf("data:%s\n",tmp->data);
tmp=tmp->next;
}
}
int main(int argc,char *argv[])
{
queue *q=init_queue();
char data[1024];
printf("input 'quit' quit this section\n");
while(1)
{
memset(data,0,sizeof(data));
printf("please input data:");
fgets(data,sizeof(data),stdin);
if(!strcmp(data,"quit\n")) break;
node *new = new_node(data);
en_queue(q,new);
}
show_queue(q);
return 0;
}
安全队列实现
#ifndef _QUEUE_H
#define _QUEUE_H
#include <stdlib.h>
#include <pthread.h>
#define QUEUE_SIZE 20
typedef struct {
void **data;
int _head;
int _tail;
int max_size;
pthread_mutex_t lock; /* 互斥体lock 用于对缓冲区的互斥操作 */
pthread_cond_t notempty; /* 缓冲区非空的条件变量 */
pthread_cond_t notfull; /* 缓冲区未满的条件变量 */
}queue;
queue *init_queue(int max_size);
void enqueue(queue* q,void *data);
void dequeue(queue* q,void **data);
#endif
~
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "queue.h"
#define QUEUE_SIZE 20
typedef struct {
void **data;
int _head;
int _tail;
int max_size;
pthread_mutex_t lock; /* 互斥体lock 用于对缓冲区的互斥操作 */
pthread_cond_t notempty; /* 缓冲区非空的条件变量 */
pthread_cond_t notfull; /* 缓冲区未满的条件变量 */
}queue;
queue *init_queue(int max_size)
{
queue *q = (queue *)malloc(sizeof(queue));
if(q == NULL)
{
perror("malloc failed \n");
return NULL;
}
q->data = (void **)malloc(max_size*sizeof(void *));
if(q->data == NULL)
{
perror("malloc failed \n");
return NULL;
}
q->_head = 0;
q->_tail = 0;
q->max_size = max_size;
pthread_mutex_init(&q->lock, NULL);
pthread_cond_init(&q->notempty, NULL);
pthread_cond_init(&q->notfull, NULL);
return q;
}
void destroy_queue(queue *q)
{
if(q == NULL)
return;
pthread_cond_destroy(&q->notempty);
pthread_cond_destroy(&q->notfull);
pthread_mutex_destroy(&q->lock);
free(q->data);
free(q);
}
void enqueue(queue* q,void *data)
{
pthread_mutex_lock(&q->lock);
/* 等待缓冲区未满*/
if ((q->_tail + 1) % QUEUE_SIZE == q->_head)
{
pthread_cond_wait(&q->notfull, &q->lock);
}
/* 写数据,并移动指针 */
q->data[q->_tail] = data;
q->_tail++;
if (q->_tail >= QUEUE_SIZE)
q->_tail = 0;
/* 设置缓冲区非空的条件变量*/
pthread_cond_signal(&q->notempty);
pthread_mutex_unlock(&q->lock);
}
void dequeue(queue* q,void **data)
{
pthread_mutex_lock(&q->lock);
/* 等待缓冲区未满*/
if (q->_tail == q->_head)
{
pthread_cond_wait(&q->notempty, &q->lock);
}
/* 写数据,并移动指针 */
*data = q->data[q->_head];
q->_head++;
if (q->_head >= QUEUE_SIZE)
q->_head = 0;
/* 设置缓冲区非空的条件变量*/
pthread_cond_signal(&q->notfull);
pthread_mutex_unlock(&q->lock);
}