a round-robin queue implement in c under linux

a simple implement round-robin queue

1. roque.h
2. roque.c
3. roque_test.c
4. makefile
5. output


/*
 *
 * Copyright (c) 2006
 * chenzhixin, [email protected]  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 __ROQUE_H
#define __ROQUE_H

/*
 * this round-robin queue is implement with a static array
 *
 
*/


#ifdef _cplusplus
extern "C" {
#endif

typedef  
int node_t;

typedef 
struct __roque_t{
    node_t
* base;
    
int max_size;/*max length of queue*/
    
int head;
    
int tail;
}
roque_t;

/*init a roque with the max length of queue,per node's size*/
int roque_init(roque_t* roque,int length);
void roque_destory(roque_t* roque);
void roque_clear(roque_t* roque);

int roque_size(roque_t* roque);
int roque_max_size(roque_t* roque);
int roque_is_empty(roque_t* roque);

/*get the head of roque*/
node_t
* roque_peek_head(roque_t* roque);
node_t
* roque_peek_tail(roque_t* roque);

node_t
* roque_find(roque_t* roque,node_t* node);

int roque_push(roque_t* roque,node_t* node);
int roque_pop(roque_t* roque,node_t* node);

void roque_traverse(roque_t* roque,void (*visit)(node_t* node));

#ifdef _cplusplus
}

#endif

#endif//__ROQUE_H


/**//*
 *
 * Copyright (c) 2006
 * chenzhixin, [email protected]  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 
"roque.h"

int roque_init(roque_t* roque,int length){
    roque
->max_size=length+1;
    roque
->base=(node_t*)malloc((roque->max_size)*sizeof(node_t));
    
if(!roque->base)return -1;
    roque
->head=roque->tail=0;
    
return 0;    
}


void roque_distroy(roque_t* roque){
    free(roque);    
}


void roque_clear(roque_t* roque){
    roque
->head=roque->tail=0;
}


int roque_size(roque_t* roque){
    
return (roque->tail-roque->head+roque->max_size)%roque->max_size;
}


int roque_max_size(roque_t* roque){
    
return roque->max_size-1;
}


int roque_is_empty(roque_t* roque){
    
return roque->head==roque->tail;
}


node_t
* roque_peek_head(roque_t* roque){
    
return &(roque->base[roque->head]);
}

node_t
* roque_peek_tail(roque_t* roque){
    
return &(roque->base[roque->tail]);
}


node_t
* roque_find(roque_t* roque,node_t* node){
    
int index=roque->head;
    
while(index<roque->tail){
        
if(*node==roque->base[index])return &(roque->base[index]);
        index
++;    
    }

    
return NULL;    
}


int roque_push(roque_t* roque,node_t* node){
    
if((roque->tail+1)%roque->max_size==roque->head)return -1;/*the queue is full*/
    roque
->base[roque->tail]=*node;
    roque
->tail++;
    
return 0;
}


int roque_pop(roque_t* roque,node_t* node){
    
if(roque_is_empty(roque))return -1;
    
if(node)*node=roque->base[roque->head++];
        
else roque->head++;
    
return 0;
}


void roque_traverse(roque_t* roque,void (*visit)(node_t* node)){
    
int index=roque->head;
    
while(index<roque->tail){
        visit(
&(roque->base[index]));
        index
++;
    }

}


/**//*
 *
 * Copyright (c) 2006
 * chenzhixin, [email protected]  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 
<string.h>
#include 
"roque.h"

/*
 * roque_test.c
 * make a mode for :
 *    i have 10 ubuntu dapper disks ,i can borrow one to anyone need it, 
 *    first come first borrow and return to me after install the ubuntu
 
*/


void roque_print(node_t* node);

static roque_t roque;
static int disk_count=10;

int main(int argc, char** argv){

    
    
int i=0;
    
char name[20]="ubuntuer";
        
    roque_init(
&roque,disk_count);
    
/*borrow disks to someone need it*/
    
for(i=1;i<=12;i++){
        sprintf(name,
"ubuntuer%d",i);    
        printf(
"%s want to borrow one ubuntu disk :  ",name);    
        
if(roque_push(&roque,&i)==0)printf(" ok  ");
            
else printf(" no more disk!!!  ");
        
    }

    printf(
"borrowed disk number is : %d ",roque_size(&roque));
    printf(
"max count of disks can been borrowed: %d ",roque_max_size(&roque));
    printf(
"==================ubuntu disk borrow status============ ");
    roque_traverse(
&roque,roque_print);
    printf(
"============================================= ");

    printf(
"ubuntuer1 and ubuntuer2 has been return the disks... ");
    roque_pop(
&roque,NULL);
    roque_pop(
&roque,NULL);
    printf(
"now has %d disks can been borrowed "
            ,roque_max_size(
&roque)-roque_size(&roque));

    printf(
"==================ubuntu disk borrow status============ ");
    roque_traverse(
&roque,roque_print);
    printf(
"============================================= ");

    printf(
"ubuntuer 11 want to borrow a disk : ");    
    i
=11;
    
if(roque_push(&roque,&i)==0)printf(" ok  ");
    
else printf(" error !!!  ");

    printf(
"ubuntuer 12 want to borrow a disk : ");    
    i
=12;
    
if(roque_push(&roque,&i)==0)printf(" ok  ");
    
else printf(" error !!!  ");

    printf(
"ubuntuer 13 want to borrow a disk : ");    
    i
=13;
    
if(roque_push(&roque,&i)==0)printf(" ok  ");
    
else printf(" error !!!  ");

    printf(
"==================ubuntu disk borrow status============ ");
    roque_traverse(
&roque,roque_print);
    printf(
"============================================= ");


    
return 0;
}


void roque_print(node_t* node){
    printf(
"ubuntu disk %d had been borrowed  ",disk_number(node));
}


int disk_number(node_t* node){
    
int index=0;
    index
=*node%disk_count;
    
if(index==0)index=disk_count;
    
return index;
}


this is the make file :
#Makefile
#
!/bin/bash
CC
=gcc

all: roque_test

roque_test: roque.o roque_test.o
    $(CC) 
-g    $^ -o $@ --Wall
%.o:%.c
    $(CC) 
--c $< -Wall
clean:
    rm 
-*.o


this is the output :
newlad@ubuntu:~/workspace/algorithms/learn/queue/roque$ ./roque_test 
ubuntuer1 want to borrow one ubuntu disk :   ok 
ubuntuer2 want to borrow one ubuntu disk :   ok 
ubuntuer3 want to borrow one ubuntu disk :   ok 
ubuntuer4 want to borrow one ubuntu disk :   ok 
ubuntuer5 want to borrow one ubuntu disk :   ok 
ubuntuer6 want to borrow one ubuntu disk :   ok 
ubuntuer7 want to borrow one ubuntu disk :   ok 
ubuntuer8 want to borrow one ubuntu disk :   ok 
ubuntuer9 want to borrow one ubuntu disk :   ok 
ubuntuer10 want to borrow one ubuntu disk :   ok 
ubuntuer11 want to borrow one ubuntu disk :   no more disk
!!! 
ubuntuer12 want to borrow one ubuntu disk :   no more disk
!!! 
borrowed disk number 
is : 10
max count of disks can been borrowed: 
10
==================ubuntu disk borrow status============
ubuntu disk 
1 had been borrowed 
ubuntu disk 
2 had been borrowed 
ubuntu disk 
3 had been borrowed 
ubuntu disk 
4 had been borrowed 
ubuntu disk 
5 had been borrowed 
ubuntu disk 
6 had been borrowed 
ubuntu disk 
7 had been borrowed 
ubuntu disk 
8 had been borrowed 
ubuntu disk 
9 had been borrowed 
ubuntu disk 
10 had been borrowed 
=============================================
ubuntuer1 and ubuntuer2 has been 
return the disks...
now has 
2 disks can been borrowed
==================ubuntu disk borrow status============
ubuntu disk 
3 had been borrowed 
ubuntu disk 
4 had been borrowed 
ubuntu disk 
5 had been borrowed 
ubuntu disk 
6 had been borrowed 
ubuntu disk 
7 had been borrowed 
ubuntu disk 
8 had been borrowed 
ubuntu disk 
9 had been borrowed 
ubuntu disk 
10 had been borrowed 
=============================================
ubuntuer 
11 want to borrow a disk :  ok 
ubuntuer 
12 want to borrow a disk :  ok 
ubuntuer 
13 want to borrow a disk :  error !!! 
==================ubuntu disk borrow status============
ubuntu disk 
3 had been borrowed 
ubuntu disk 
4 had been borrowed 
ubuntu disk 
5 had been borrowed 
ubuntu disk 
6 had been borrowed 
ubuntu disk 
7 had been borrowed 
ubuntu disk 
8 had been borrowed 
ubuntu disk 
9 had been borrowed 
ubuntu disk 
10 had been borrowed 
ubuntu disk 
1 had been borrowed 
ubuntu disk 
2 had been borrowed 
=============================================
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章