双链表程序理解?
#include<stdio.h>#include<stdlib.h>
#define N 20
typedef struct buf_t buf;
struct buf_t
{
int buftId;
buf *prev;
buf *next;
};
typedef struct Data_t Data;
struct Data_t
{
buf *busyList;
};
Data gData;
buf bufs[N];
void AddtoHead(buf **headerPPtr,
buf *newPtr)
{
if (*headerPPtr != NULL)
{
newPtr->prev = (*headerPPtr)->prev;
newPtr->next = *headerPPtr;
(*headerPPtr)->prev->next = newPtr;
(*headerPPtr)->prev = newPtr;
}
else
{
*headerPPtr = newPtr;
newPtr->next = newPtr;
newPtr->prev = newPtr;
}
return;
}
void Data_initialize()
{
int i;
memset((int*)bufs, 0, sizeof(buf_t) *N);
for (i = 0; i < N; i++)
{
bufs[i].bufId = i;
}
memset(&gData, 0, sizeof(gData));
return;
}
int main()
{
Data_initialize();
buf *tmpPtr;
int simIndex = 0;
int i;
/*add the bufId to the busylist*/
for( i = 0;i< 20;i++)
{
AddtoHead(&(gData.busyList),
&buf[i]);
}
tmpPtr = Data.busyList;
while(tmpPtr != NULL )
{
printf("%d\n",tmpPtr->bufId);
tmpPtr = tmpPtr ->next;
}
}
这段程序怎么理解啊?
先初始化,然后把buf的内容加到gData去?从打印来看,好像是从0,1,2******19循环打印的。
这是双项链表的插入吗?若是的话,那数组一开始就分配好的啊?这样做有什么好处?
而且插入的话应该都是插在表头才对啊,应该是19,18,17,****0?