circlelist

#include<stdlib.h>
#include<string.h>
#include<stdio.h>

#define LIST_SIZE 10

typedef struct clist{
    int head ;
    int tail ;
    int size;
    int capacity;
    int list[LIST_SIZE];
}clist_t;

clist_t * create()
{
    clist_t * cl = (clist_t*)(malloc(sizeof(clist_t)));
    cl->head = -1;
    cl->tail = -1;
    cl->size = 0;
    cl->capacity = LIST_SIZE;
    return cl; 
}

void release(clist_t *cl)
{
    free(cl);
    cl = NULL;
}

int push(clist_t * const cl,int v)
{
    if (cl->size == cl->capacity)
        return 0;
    else 
        cl->tail =(++cl->tail) %LIST_SIZE;
        cl->list[cl->tail] = v;
        cl->size ++;
        if (cl->size == 1)
            cl->head = cl->tail;
        return 1;
}

int pop(clist_t * const cl)
{
    if(cl->size == 0)
        return 0;
    else 
        cl->head = (++cl->head)%LIST_SIZE;
        cl->size --;
        if (cl->size == 0)
            cl->head = cl->tail = -1;
    return 1;
}
void remove_v(clist_t * const cl,int v)
{
    int cur = cl->head;
     while(cur%LIST_SIZE != cl->tail)
    {
        if(cl->list[cur]==v)
        {
            if (cur < cl->head)
            {
               int i;
              for (i= cur ;i<cl->tail;i++)
              {
                  cl->list[i]=cl->list[i+1];
              } 
              cl->tail =(cl->tail-1)%LIST_SIZE;
                
            }else
            {
               int i ;
               for (i = cur;i>cl->head;i--)
               {
                   cl->list[i] = cl->list[i-1];
               }
               cl->head = (cl->head+1)%LIST_SIZE;

            }
            cl->size --;
            return ;
        }
        else 
        {
            cur ++;
        }
    }
    
}

void print(clist_t cl)
{
    int cur = cl.head;
    do{
        printf("%d\t",cl.list[cl.head]);
        cl.head =  (++cl.head)%LIST_SIZE;
    } while(cur%LIST_SIZE != cl.head);
    printf("\r\n");
}

编写时遇到的问题:找准位置在差值

                              转圈接力跑

关于head,tail 的指向问题。是指向当前的值,还是指向 next-out,next-in 

感想:

  1. 物体的初始状态。

  2. 维持机制  (本身的规则)。

  3. 添加新东西,解决现有冲突。

  4. 贯彻执行


转载于:https://my.oschina.net/invictuslee/blog/267454

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值