A circular buffer is a linked list with a slight twist.
The 'first' element in the list points to the last as the previous element, and the last element points to the first element as the next. This code creates the buffer
Code:typedef struct x{ struct x *prev; char data[100]; struct x *next; }; struct x m[100]; for (i=1;i<100;i++){ m[i].prev = &m[i-1]; m[i].next = &m[i+1]; memset(m[i].data,0x00,sizeof(m[i].data)); } m[0].next=&m[1]; m[0].prev=&m[100]; m[100].next=&m[0]; m[100].prev=&m[99]; memset(m[0].data,0x00,sizeof(m[0].data)); memset(m[100].data,0x00,sizeof(m[100].data));




Reply With Quote