#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
感想:
物体的初始状态。
维持机制 (本身的规则)。
添加新东西,解决现有冲突。
贯彻执行