您的位置:首页 > 其它

C实现循环双链表

2012-07-13 09:09 190 查看
/**************************************
C实现循环双链表
**************************************/
#include<stdio.h>
#include<stdlib.h>
struct DuLNode
{
int num;
struct DuLNode *prior,*next;
};
struct DuLNode *head=NULL;
cre_list() //初始化结点
{
head = (struct DuLNode *)malloc(sizeof(struct DuLNode));
head->prior = head;
head->next = head;
}
add_DuLNode(int num) //插入结点
{
struct DuLNode *ptr = (struct DuLNode *)malloc(sizeof(struct DuLNode));
ptr->num = num;
ptr->next=head->next;
head->next=ptr;
ptr->prior=head;
ptr->next->prior=ptr;
}
display_DuLNode() //遍历链表
{
struct DuLNode *p = head;
do
{
printf("%d\t",p->num);
p = p->next;
}while(p!=head);
printf("\n");
}
delete_DuLNode(int num)  //删除
{
struct DuLNode *p,*q;
p = head;
do
{
if(p->num == num)
{
q=p->next;
p->prior->next=q;
q->prior=p->prior;
free(p);
p=q;
}
else
{
p=p->next;
}
}while(p != head);
}
int main()
{
int i=0;
cre_list();
for(i;i<10;i++)
{
add_DuLNode(i);
}
display_DuLNode();
delete_DuLNode(5);
display_DuLNode();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: