双向循环链表的插入删除

#include
#include
#include
#define insert(p){\
p->next=head;\
p->prev=head->prev;\
head->prev->next=p;\
head->prev=p;\
}
#define for_each_task(p)\
for (p = head ; (p = p->next) != head;)\
printf("%d\n",p->pid);
struct task
{
int pid;
struct task *prev;
struct task *next;
};
struct task *head;
void insert_task(int pid)
{
struct task *p=(struct task*)malloc(sizeof(struct task));
p->pid=pid;
insert(p);

}
void init()
{
head=(struct task *)malloc(sizeof(struct task));
head->pid=0;
head->prev=head;
head->next=head;
}
int main()
{
int i;
 init();

for(i=1;i<4;i++)
insert_task(i);
struct task *p;
for_each_task(p);
return 0;
}

你可能感兴趣的:(数据结构基础)