C-implemented cyclic queue

Source: Internet
Author: User
Tags define definition

/// Fs_loop_queue.h // fsnet // Created by Vincent on 14-5-22. // Copyright (c) 2014 Vincent. all rights reserved. // # ifndef summary # define definition # include "fs_define.h" struct fs_loop_queue; struct validation * evaluate (size_t len); fs_bool evaluate (struct fs_loop_queue *, void * item ); void * evaluate (struct fs_loop_queue *); fs_bool evaluate (struct fs_loop_queue *); void Merge (struct merge *); # endif

/// Fs_loop_queue.c // fsnet // Created by Vincent on 14-5-22. // Copyright (c) Aug Vincent. All rights reserved. // # include
 
  
# Include "example" # include "fs_malloc.h" struct detail {void ** que; size_t head; size_t tail; size_t max_len; unsigned long script_id ;};# define _ pos_add (x, n, max) x = (x + n) % maxstruct fs_loop_queue * queue (size_t len) {struct queue * queue = (struct fs_loop_queue *) fs_malloc (sizeof (* queue )); fs_zero (queue, sizeof (* queue); queue-> max_len = len; queue-> que = fs_malloc (sizeof (void *) * len ); fs_zero (queue-> que, sizeof (void *) * len); return queue;} evaluate (struct fs_loop_queue * que, void * item) {if (fs_loop_queue_full (que )) {return fs_false;} que-> que [que-> head] = item; _ pos_add (que-> head, 1, que-> max_len); return fs_true ;} void * fs_loop_queue_pop (struct fs_loop_queue * que) {if (fs_loop_queue_empty (que) {return NULL;} void * item = que-> que [que-> tail]; que-> que [que-> tail] = NULL; _ pos_add (que-> tail, 1, que-> max_len); return item;} fs_boolfs_loop_queue_full (struct fs_loop_queue * que) {return (que-> head + 1) % que-> max_len) = que-> tail;} fs_boolfs_loop_queue_empty (struct fs_loop_queue * que) {return que-> head = que-> tail;} voidfs_loop_queue_free (struct fs_loop_queue * que) {fs_free (que-> que); fs_free (que );}
 

This type of cyclic queue is unlocked in A push B pop.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.