i have written a simple normal queue implement with c,these are the code
1. queue.h
2. queue.c
3. queue_test.c
4. Makefile
5. execute output
![]()
/**//*
*
* Copyright (c) 2006
* chenzhixin, [email protected] 2006.12.6
*
* Permission to use,copy,modify,distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. chenzhixin makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
![]()
#ifndef __QUEUE_H
#define __QUEUE_H
![]()
![]()
/**//*
* function summary:
* all function in this file,return 0 when it's ok,negative is error
*/
#ifdef __cplusplus
![]()
extern "C"...{
#endif
![]()
![]()
/**//* node_t,struct of node in queue*/
![]()
typedef struct __node_t...{
void* data;
struct __node_t* next;
}node_t;
![]()
![]()
/**//*queue_t,struct of queue*/
![]()
typedef struct __queue_t...{
struct __node_t* head;
struct __node_t* tail;
}queue_t;
![]()
![]()
void queue_init(queue_t* queue);/**//*init the queue*/
![]()
void queue_distroy(queue_t* queue);/**//*distroy the queue*/
![]()
void queue_clear(queue_t* queue);/**//*clear all the nodes in queue*/
![]()
void* queue_gethead(queue_t* queue);/**//*get the head of queue*/
![]()
void* queue_gettail(queue_t* queue);/**//*get the tail of queue*/
![]()
/**//*find the node which data's content same as the data's*/
node_t* queue_find(queue_t* queue,void* data,size_t size);
![]()
int queue_is_empty(queue_t* queue);/**//*the queue is empty or not*/
![]()
int queue_push(queue_t* queue,void* node);/**//*entry a node to queue*/
![]()
int queue_pop(queue_t* queue);/**//*only pop data,not save it*/
![]()
/**//* remove a node from queue*/
int queue_pop_copy(queue_t* queue,void* data,size_t size);
![]()
int queue_size(queue_t* queue);/**//*size of queue*/
![]()
/**//*traverse the queue with function visit*/
void queue_traverse(queue_t* queue,void (*visit)(void* data));
#ifdef __cplusplus
}
#endif
![]()
#endif/*__QUEUE_H*/
![]()
![]()
![]()
![]()
/**//*
* Copyright (c) 2006
* chenzhixin , [email protected] 2006.12.6
*
* Permission to use,copy,modify,distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. chenzhixin makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
![]()
#include <stdlib.h>
#include "queue.h"
![]()
![]()
void queue_init(queue_t* queue)...{
queue->head=queue->tail=NULL;
}
![]()
![]()
void queue_distroy(queue_t* queue)...{
![]()
while(queue->head)...{
queue->tail=queue->head->next;
free(queue->head);
queue->head=queue->tail;
}
}
![]()
![]()
void queue_clear(queue_t* queue)...{
queue_distroy(queue);
}
![]()
![]()
int queue_size(queue_t* queue)...{
![]()
int size=0;
node_t* node=queue->head;
![]()
while(node)...{
size++;
node=node->next;
}
return size;
}
![]()
![]()
int queue_is_empty(queue_t* queue)...{
return queue->head==NULL;
}
![]()
![]()
void* queue_gethead(queue_t* queue)...{
![]()
/**//*return (queue->head!=NULL)&&(data=queue->head->data); */
if(!queue->head)return NULL;
return queue->head->data;
}
![]()
![]()
void* queue_gettail(queue_t* queue)...{
if(!queue->head || !queue->tail)return NULL;
return queue->tail->data;
}
![]()
![]()
node_t* queue_find(queue_t* queue,void* data,size_t size)...{
node_t* node=queue->head;
![]()
while(node)...{
![]()
if(data && size && memcmp(data,node->data,size)==0)...{
return node;
}
node=node->next;
}
return NULL;
}
![]()
![]()
int queue_push(queue_t* queue,void* data)...{
![]()
/**//*
* push the data to the tail of queue,and return 0;
* if the malloc fail,then return -1;
*/
node_t* node=(node_t*)malloc(sizeof(node_t));
if(!node)return -1;
node->data=data;
node->next=NULL;
![]()
if(queue->head)...{
queue->tail->next=node;
queue->tail=node;
return 0;
}
![]()
/**//*the queue is empty*/
queue->head=queue->tail=node;
return 0;
}
![]()
![]()
int queue_pop(queue_t* queue)...{
node_t* node;
if(!queue->head)return -1;
![]()
node=queue->head;
queue->head=queue->head->next;
![]()
free(node);
return 0;
}
![]()
![]()
int queue_pop_copy(queue_t* queue,void* data,size_t size)...{
![]()
/**//*
* if the queue is not empty,then delete the head of queue,and return 0;
* else return 0;
*/
node_t* node;
if(!queue->head)return -1;
![]()
node=queue->head;
![]()
![]()
if( data && size )...{
memcpy(data,queue->head->data,size);
![]()
}/**//*copy data*/
queue->head=queue->head->next;
free(node);
return 0;
}
![]()
![]()
void queue_traverse(queue_t* queue,void (*visit)(void* data))...{
node_t* node=queue->head;
![]()
while(node)...{
visit(node->data);
node=node->next;
}
}
![]()
![]()
/**//*
*
* Copyright (c) 2006
* chenzhixin, [email protected] 2006.12.6
*
* Permission to use,copy,modify,distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. chenzhixin makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
![]()
#include <stdlib.h>
#include <stdio.h>
#include "queue.h"
![]()
![]()
void queue_print(void* data)...{printf("the data is : %d ",*(int*)data);}
![]()
![]()
int main()...{
int index,*temp,test;
node_t* node;
![]()
queue_t* queue=(queue_t*)malloc(sizeof(queue_t));
![]()
![]()
queue_init(queue);/**//*init the queue*/
![]()
for(index=1;index<=6;index++)...{
temp=(int*)malloc(sizeof(int));
*temp=index;
queue_push(queue,(void*)temp);
}
queue_traverse(queue,queue_print);
![]()
printf("the queue's size is : %d ",queue_size(queue));
printf("the queue's head is : %d ",*(int*)queue_gethead(queue));
![]()
/**//*find the node who's data is 6*/
printf("find the node who's data is 6........... ");
temp=6;
node=queue_find(queue,&temp,sizeof(temp));
if(!node)printf("can not find the node!!!");
else printf("find the node,who's addr is: 0x%x, data is: %d ",
node,*(int*)node->data);
![]()
queue_pop_copy(queue,&test,sizeof(int));
queue_traverse(queue,queue_print);
printf("after pop a node,the pop data is : %d ",test);
![]()
![]()
if(!queue_is_empty(queue))...{printf("the queue is not empty ");}
queue_clear(queue);
![]()
if(queue_is_empty(queue))...{printf("after clean,the queue is now empty ");}
queue_traverse(queue,queue_print);
![]()
return 0;
}
![]()
this is the makefile for compiler:
#makefile
#!/bin/bash
CC=gcc
![]()
all: queue_test
queue_test: queue.o queue_test.o
$(CC) -g $^ -o $@ -O -Wall
![]()
%.o:%.c
$(CC) -g -c $< -Wall
clean:
rm -r *.o
![]()
this is the output when execute the test program:
newlad@ubuntu:~/workspace/algorithms/learn/queue$ ./queue_test
the data is : 1
the data is : 2
the data is : 3
the data is : 4
the data is : 5
the data is : 6
the queue's size is : 6
the queue's head is : 1
find the node who's data is 6...........
find the node,who's addr is: 0x804a0c8, data is: 6
the data is : 2
the data is : 3
the data is : 4
the data is : 5
the data is : 6
after pop a node,the pop data is : 1
the queue is not empty
after clean,the queue is now empty
![]()