double-end queue (deque) has tree types:
1. normal double-end queue
2. restrict input queue (one end can only output,can't input data)
3. restrict output queue (one end can only input,can't output data)
the double-end queue is not normal used then queue or stack,but it's also usefull
the files i have write are:
1. deque.h
2. deque.c
3. deque_test.c
4. Makefile
5. output of execute the program
/*
*
* Copyright (c) 2006
* chenzhixin, china.newlad@gmail.com 2006.12.7
*
* 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 __DEQUE_H
#define __DEQUE_H
#ifdef __cplusplus
extern " C " {
#endif
typedef struct __node_t{
void* data;
struct __node_t* prev;
struct __node_t* next;
}node_t;
typedef struct __deque_t{
struct __node_t* head;
struct __node_t* tail;
}deque_t;
void deque_init(deque_t* deque);/*init the deque*/
void deque_destroy(deque_t* deque);/*destroy the deque*/
void deque_clear(deque_t* deque);/*clear all the nodes in deque*/
void* deque_peek_head(deque_t* deque);/*peek the head of deque*/
void* deque_peek_tail(deque_t* deque);/*peek the tail of deque*/
/*find the data with same content*/
node_t* deque_find_begin(deque_t* deque,void* data,size_t size);
/*find from deque's end to begin*/
node_t* deque_find_reverse(deque_t* deque,void* data,size_t size);
int deque_is_empty(deque_t* deque);/*the deque is empty or not*/
int deque_size(deque_t* deque);/*get the size of deque*/
int deque_push_head(deque_t* deque,void* data);/*push data to head of deque*/
int deque_push_tail(deque_t* deque,void* data);/*push data to deque from tail*/
int deque_pop_head(deque_t* deque);/*pop data from head*/
int deque_pop_tail(deque_t* deque);/*pop data from tail*/
/*pop data from head and copy it*/
int deque_pop_head_copy(deque_t* deque,void* data,size_t size);
/*pop data from tail and copy it*/
int deque_pop_tail_copy(deque_t* deque,void* data,size_t size);
/*traverse deque with function visit*/
void deque_traverse(deque_t* deque, void (*visit)(void* data));
#ifdef __cplusplus
}
#endif
#endif // __DEQUE_H
*
* Copyright (c) 2006
* chenzhixin, china.newlad@gmail.com 2006.12.7
*
* 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 __DEQUE_H
#define __DEQUE_H
#ifdef __cplusplus
extern " C " {
#endif
typedef struct __node_t{
void* data;
struct __node_t* prev;
struct __node_t* next;
}node_t;
typedef struct __deque_t{
struct __node_t* head;
struct __node_t* tail;
}deque_t;
void deque_init(deque_t* deque);/*init the deque*/
void deque_destroy(deque_t* deque);/*destroy the deque*/
void deque_clear(deque_t* deque);/*clear all the nodes in deque*/
void* deque_peek_head(deque_t* deque);/*peek the head of deque*/
void* deque_peek_tail(deque_t* deque);/*peek the tail of deque*/
/*find the data with same content*/
node_t* deque_find_begin(deque_t* deque,void* data,size_t size);
/*find from deque's end to begin*/
node_t* deque_find_reverse(deque_t* deque,void* data,size_t size);
int deque_is_empty(deque_t* deque);/*the deque is empty or not*/
int deque_size(deque_t* deque);/*get the size of deque*/
int deque_push_head(deque_t* deque,void* data);/*push data to head of deque*/
int deque_push_tail(deque_t* deque,void* data);/*push data to deque from tail*/
int deque_pop_head(deque_t* deque);/*pop data from head*/
int deque_pop_tail(deque_t* deque);/*pop data from tail*/
/*pop data from head and copy it*/
int deque_pop_head_copy(deque_t* deque,void* data,size_t size);
/*pop data from tail and copy it*/
int deque_pop_tail_copy(deque_t* deque,void* data,size_t size);
/*traverse deque with function visit*/
void deque_traverse(deque_t* deque, void (*visit)(void* data));
#ifdef __cplusplus
}
#endif
#endif // __DEQUE_H
/*
*
* Copyright (c) 2006
* chenzhixin, china.newlad@gmail.com 2006.12.7
*
* 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 " deque.h "
/*init the deque*/
void deque_init(deque_t * deque) {
deque->head=NULL;
deque->tail=NULL;
}
/*destroy the deque through traverse the deque list*/
void deque_destroy(deque_t * deque) {
while(deque->head){
deque->tail=deque->head->next;
free(deque->head);
deque->head=deque->tail;
}
}
void deque_clear(deque_t * deque) {
deque_destroy(deque);
}
/*get the size of deque*/
int deque_size(deque_t * deque) {
int size=0;
node_t* node=deque->head;
while(node){
size++;
node=node->next;
}
return size;
}
int deque_is_empty(deque_t * deque) {
return deque->head==NULL;
}
void * deque_peek_head(deque_t * deque) {
if(!deque->head)return NULL;/*the deque is empty*/
return deque->head->data;
}
void * deque_peek_tail(deque_t * deque) {
if(!deque->head || !deque->tail)return NULL;/*the deque is empty*/
return deque->tail->data;
}
/*find the node that has the same content with data in normal order*/
node_t * deque_find_begin(deque_t * deque, void * data,size_t size) {
node_t* node=deque->head;
while(node){
if(data && size && memcmp(data,node->data,size)==0){
return node;
}
node=node->next;
}
return NULL;
}
/*find the node that has the same content with data from deque's end to begin*/
node_t * deque_find_reverse(deque_t * deque, void * data,size_t size) {
node_t* node=deque->tail;
while(node){
if(data && size && memcmp(data,node->data,size)==0){
return node;
}
node=node->prev;
}
return NULL;
}
int deque_push_head(deque_t * deque, void * data) {
node_t* node=(node_t*)malloc(sizeof(data));
if(!node)return -1;
node->data=data;
node->prev=NULL;
node->next=deque->head;
/*the deque is empty*/
if(deque->head==NULL){deque->head=deque->tail=node; return 0;}
deque->head->prev=node;
deque->head=node;
return 0;
}
int deque_push_tail(deque_t * deque, void * data)
{
node_t* node=(node_t*)malloc(sizeof(data));
if(!node)return -1;
node->data=data;
node->prev=deque->tail;
node->next=NULL;
/*the deque is empty*/
if(deque->tail==NULL){deque->head=deque->tail=node;return 0;}
deque->tail->next=node;
deque->tail=node;
return 0;
}
int deque_pop_head(deque_t * deque) {
if(!deque->head)return -1;
/*the deque has only one node*/
if(deque->head->next==NULL){
free(deque->head);
deque->head=deque->tail=NULL;
return 0;
}
deque->head=deque->head->next;
free(deque->head->prev);
deque->head->prev=NULL;
return 0;
}
int deque_pop_tail(deque_t * deque) {
if(!deque->tail)return -1;
/*the deque has only one node*/
if(deque->tail->prev==NULL){
free(deque->tail);
deque->head=deque->tail=NULL;
return 0;
}
deque->tail=deque->tail->prev;
free(deque->tail->next);
deque->tail->next=NULL;
return 0;
}
int deque_pop_head_copy(deque_t * deque, void * data,size_t size) {
if(!deque->head)return -1;
if(data && size){
memcpy(data,deque->head->data,size);
}
return deque_pop_head(deque);
}
int deque_pop_tail_copy(deque_t * deque, void * data,size_t size) {
if(!deque->head || !deque->tail)return -1;
if(data && size){
memcpy(data,deque->tail->data,size);
}
return deque_pop_tail(deque);
}
void deque_traverse(deque_t * deque, void ( * visit)( void * data)) {
node_t* node=deque->head;
while(node){
visit(node->data);
node=node->next;
}
}
*
* Copyright (c) 2006
* chenzhixin, china.newlad@gmail.com 2006.12.7
*
* 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 " deque.h "
/*init the deque*/
void deque_init(deque_t * deque) {
deque->head=NULL;
deque->tail=NULL;
}
/*destroy the deque through traverse the deque list*/
void deque_destroy(deque_t * deque) {
while(deque->head){
deque->tail=deque->head->next;
free(deque->head);
deque->head=deque->tail;
}
}
void deque_clear(deque_t * deque) {
deque_destroy(deque);
}
/*get the size of deque*/
int deque_size(deque_t * deque) {
int size=0;
node_t* node=deque->head;
while(node){
size++;
node=node->next;
}
return size;
}
int deque_is_empty(deque_t * deque) {
return deque->head==NULL;
}
void * deque_peek_head(deque_t * deque) {
if(!deque->head)return NULL;/*the deque is empty*/
return deque->head->data;
}
void * deque_peek_tail(deque_t * deque) {
if(!deque->head || !deque->tail)return NULL;/*the deque is empty*/
return deque->tail->data;
}
/*find the node that has the same content with data in normal order*/
node_t * deque_find_begin(deque_t * deque, void * data,size_t size) {
node_t* node=deque->head;
while(node){
if(data && size && memcmp(data,node->data,size)==0){
return node;
}
node=node->next;
}
return NULL;
}
/*find the node that has the same content with data from deque's end to begin*/
node_t * deque_find_reverse(deque_t * deque, void * data,size_t size) {
node_t* node=deque->tail;
while(node){
if(data && size && memcmp(data,node->data,size)==0){
return node;
}
node=node->prev;
}
return NULL;
}
int deque_push_head(deque_t * deque, void * data) {
node_t* node=(node_t*)malloc(sizeof(data));
if(!node)return -1;
node->data=data;
node->prev=NULL;
node->next=deque->head;
/*the deque is empty*/
if(deque->head==NULL){deque->head=deque->tail=node; return 0;}
deque->head->prev=node;
deque->head=node;
return 0;
}
int deque_push_tail(deque_t * deque, void * data)
{
node_t* node=(node_t*)malloc(sizeof(data));
if(!node)return -1;
node->data=data;
node->prev=deque->tail;
node->next=NULL;
/*the deque is empty*/
if(deque->tail==NULL){deque->head=deque->tail=node;return 0;}
deque->tail->next=node;
deque->tail=node;
return 0;
}
int deque_pop_head(deque_t * deque) {
if(!deque->head)return -1;
/*the deque has only one node*/
if(deque->head->next==NULL){
free(deque->head);
deque->head=deque->tail=NULL;
return 0;
}
deque->head=deque->head->next;
free(deque->head->prev);
deque->head->prev=NULL;
return 0;
}
int deque_pop_tail(deque_t * deque) {
if(!deque->tail)return -1;
/*the deque has only one node*/
if(deque->tail->prev==NULL){
free(deque->tail);
deque->head=deque->tail=NULL;
return 0;
}
deque->tail=deque->tail->prev;
free(deque->tail->next);
deque->tail->next=NULL;
return 0;
}
int deque_pop_head_copy(deque_t * deque, void * data,size_t size) {
if(!deque->head)return -1;
if(data && size){
memcpy(data,deque->head->data,size);
}
return deque_pop_head(deque);
}
int deque_pop_tail_copy(deque_t * deque, void * data,size_t size) {
if(!deque->head || !deque->tail)return -1;
if(data && size){
memcpy(data,deque->tail->data,size);
}
return deque_pop_tail(deque);
}
void deque_traverse(deque_t * deque, void ( * visit)( void * data)) {
node_t* node=deque->head;
while(node){
visit(node->data);
node=node->next;
}
}
/*
*
* Copyright (c) 2006
* chenzhixin, china.newlad@gmail.com 2006.12.7
*
* 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 " deque.h "
/*
* deque_test.c
* test for deque,the data type is a struct,this program is a model
* about line up when have lunch,somebody may jump to the queue,
* so use the deque to model it
*
*/
typedef struct __student_t {
char* name; /*student's name*/
char* number;/*student's number*/
} student_t;
/*show student's information*/
void deque_print( void * stu) {
printf("the student's name and number are: %s %s ",
((student_t*)stu)->name,((student_t*)stu)->number);
}
int main( int argc, char ** argv) {
student_t stu,stu1,stu2,stu3,leave_stu,find_stu;
deque_t deque;
node_t* node_find;
deque_init(&deque);
/*it is the lunch time now,students come to buy rice*/
stu1.name="chenzhixin";
stu1.number="03551503";
stu2.name="chendong";
stu2.number="03551501";
stu3.name="huanjianhua";
stu3.number="03551513";
deque_push_tail(&deque,&stu1);
deque_push_tail(&deque,&stu2);
deque_push_tail(&deque,&stu3);
deque_traverse(&deque,deque_print);
printf("the deque's size is : %d ",deque_size(&deque));
/*find the node in deque,who's data is same as find_stu*/
find_stu.name="chenzhixin";
find_stu.number="03551503";
printf("test the find function.... ,find the node who's name and number are: %s %s ",
find_stu.name,find_stu.number);
node_find=deque_find_begin(&deque,&find_stu,sizeof(student_t));
if(!node_find)printf("can not find the node ");
else printf("the find node's addr is: 0x%x,and next node's name is : %s ",
node_find,((student_t*)(node_find->next->data))->name);
/*stu jump to the wait line*/
printf("now stu will jump to the wait line ,and the deque now is : ");
stu.name="cha dui";
stu.number="jiu bu gao su ni ";
deque_push_head(&deque,&stu);
deque_traverse(&deque,deque_print);
/*stu was catched by a teacher,and then been send out*/
printf("the stu was catched by a teacher ");
deque_pop_head(&deque);
deque_traverse(&deque,deque_print);
/*the stu3 can't wait any more time ,so he leave the queue*/
deque_pop_tail_copy(&deque,&leave_stu,sizeof(student_t));
printf("the leave student's data are :%s %s ",leave_stu.name,leave_stu.number);
deque_traverse(&deque,deque_print);
return 0;
}
*
* Copyright (c) 2006
* chenzhixin, china.newlad@gmail.com 2006.12.7
*
* 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 " deque.h "
/*
* deque_test.c
* test for deque,the data type is a struct,this program is a model
* about line up when have lunch,somebody may jump to the queue,
* so use the deque to model it
*
*/
typedef struct __student_t {
char* name; /*student's name*/
char* number;/*student's number*/
} student_t;
/*show student's information*/
void deque_print( void * stu) {
printf("the student's name and number are: %s %s ",
((student_t*)stu)->name,((student_t*)stu)->number);
}
int main( int argc, char ** argv) {
student_t stu,stu1,stu2,stu3,leave_stu,find_stu;
deque_t deque;
node_t* node_find;
deque_init(&deque);
/*it is the lunch time now,students come to buy rice*/
stu1.name="chenzhixin";
stu1.number="03551503";
stu2.name="chendong";
stu2.number="03551501";
stu3.name="huanjianhua";
stu3.number="03551513";
deque_push_tail(&deque,&stu1);
deque_push_tail(&deque,&stu2);
deque_push_tail(&deque,&stu3);
deque_traverse(&deque,deque_print);
printf("the deque's size is : %d ",deque_size(&deque));
/*find the node in deque,who's data is same as find_stu*/
find_stu.name="chenzhixin";
find_stu.number="03551503";
printf("test the find function.... ,find the node who's name and number are: %s %s ",
find_stu.name,find_stu.number);
node_find=deque_find_begin(&deque,&find_stu,sizeof(student_t));
if(!node_find)printf("can not find the node ");
else printf("the find node's addr is: 0x%x,and next node's name is : %s ",
node_find,((student_t*)(node_find->next->data))->name);
/*stu jump to the wait line*/
printf("now stu will jump to the wait line ,and the deque now is : ");
stu.name="cha dui";
stu.number="jiu bu gao su ni ";
deque_push_head(&deque,&stu);
deque_traverse(&deque,deque_print);
/*stu was catched by a teacher,and then been send out*/
printf("the stu was catched by a teacher ");
deque_pop_head(&deque);
deque_traverse(&deque,deque_print);
/*the stu3 can't wait any more time ,so he leave the queue*/
deque_pop_tail_copy(&deque,&leave_stu,sizeof(student_t));
printf("the leave student's data are :%s %s ",leave_stu.name,leave_stu.number);
deque_traverse(&deque,deque_print);
return 0;
}
here is the make file:
#Makefile
# !/ bin / bash
CC = gcc
all: deque_test
deque_test: deque.o deque_test.o
$(CC) - g $ ^ - o $@ - O - Wall
% .o: % .c
$(CC) - g - c $ < - Wall
clean:
rm - r * .o
# !/ bin / bash
CC = gcc
all: deque_test
deque_test: deque.o deque_test.o
$(CC) - g $ ^ - o $@ - O - Wall
% .o: % .c
$(CC) - g - c $ < - Wall
clean:
rm - r * .o
this is the output of execute the program:
newlad@ubuntu:
~/
workspace
/
algorithms
/
learn
/
queue
/
deque$ .
/
deque_test
the student ' s name and number are: chenzhixin 03551503
the student ' s name and number are: chendong 03551501
the student ' s name and number are: huanjianhua 03551513
the deque ' s size is : 3
test the find function....
,find the node who ' s name and number are: chenzhixin 03551503
the find node ' s addr is: 0x804a008,and next node ' s name is : chendong
now stu will jump to the wait line ,and the deque now is :
the student ' s name and number are: cha dui jiu bu gao su ni
the student ' s name and number are: chenzhixin 03551503
the student ' s name and number are: chendong 03551501
the student ' s name and number are: huanjianhua 03551513
the stu was catched by a teacher
the student ' s name and number are: chenzhixin 03551503
the student ' s name and number are: chendong 03551501
the student ' s name and number are: huanjianhua 03551513
the leave student ' s data are :huanjianhua 03551513
the student ' s name and number are: chenzhixin 03551503
the student ' s name and number are: chendong 03551501
the student ' s name and number are: chenzhixin 03551503
the student ' s name and number are: chendong 03551501
the student ' s name and number are: huanjianhua 03551513
the deque ' s size is : 3
test the find function....
,find the node who ' s name and number are: chenzhixin 03551503
the find node ' s addr is: 0x804a008,and next node ' s name is : chendong
now stu will jump to the wait line ,and the deque now is :
the student ' s name and number are: cha dui jiu bu gao su ni
the student ' s name and number are: chenzhixin 03551503
the student ' s name and number are: chendong 03551501
the student ' s name and number are: huanjianhua 03551513
the stu was catched by a teacher
the student ' s name and number are: chenzhixin 03551503
the student ' s name and number are: chendong 03551501
the student ' s name and number are: huanjianhua 03551513
the leave student ' s data are :huanjianhua 03551513
the student ' s name and number are: chenzhixin 03551503
the student ' s name and number are: chendong 03551501