第九课: C数据结构—双向循环链表实现

**双向循环链表实现 **
**example **

typedef struct node{
    int data;
    struct node *prior;
    struct node *next;
}distnode;

dlistnode *dlist_create();
void dlist_show(distnode *H);
dlistnode *dlist_get(distnode *H,int pos);
int dlist_insert(distnode *H,int value, int pos);
int dlist_delete(distnode *H, int pos);

dlistnode *dlist_create()
{
    int n;
    distnode *H, *r, *p;
    if((H = (distnode *)malloc(sizeof(distnode))) == NULL)
    {
		printf("malloc failed\n");
		return NULL;
    }
    H->prior = H;
    H->next = H;
    while(1)
    {
    	printf("exit when input: -1\n");
    	scanf("%d",&n);
    	if(n == -1)
        	break;
    	p->data = n;
    	p->prior = r->next;
    	r->next = p;
    	H->prior = P;
    	r = p;
    }
    retuin H;
}
void dlist_show(distnode *H)
{
	dlistnode *p;
	p = H->next;
	while(p != H)
	{
    	printf("%d", p->data);
    	p = p->next; 
    }
}

dlistnode *dlist_get(distnode *H,int pos)
{
    int i =0;
	if(pos < 0)
	{
		printf("POS < 0,invalid!\n");
		return NULL;
	}
	while(i< pos)
	{
		p = p->next;
		i++;
		if(p == H)
		{
			printf("pos is invaild\n");
			return NULL;
		}
	}
	return p;
}

int dlist_insert(distnode *H,int value, int pos)
{
	distnode *p, *q;
	p = dlist_get(H, pos);
	if(p == NULL)
	{
		return -1;
	}
	if((q = (distnode *)malloc(sizeof(distnode))) == NULL){
		printf("malloc error");
	    return -1;
	}
	q->data = value;
	q->prior = p-prior;
	q->next = p;
	p->prior->next = q;
	p->prior = q;
	return 0;

}

int dlist_delete(distnode *H, int pos)
{
	dlistnode *p;
	p = dlist_get(H, pos);
	if(p == NULL)
	{
    	return -1;
	}
	p->prior->next = p->next;
	p->next->prior = p->prior;
	return 0;
}

int main (int argc, const char *argv)
{
/*遍历显示双向链表  */
    dlistnode *H;
    H = dlist_create();
    dlist_show(H);
    return 0;
 #if 0
/*查找某位置的值  */
	dlistnode *p;
	while(1) 
	{
		printf("input pos:");
		scanf("%d",&pos);
		p = dlist_get(H,&pos);
		if(p)
		{
			printf("%d",p->data);
		}
	}
#endif
/*在某个位置前面插入固定值100,并在插入后打印链表所有值  */
    while(1) 
	{
		printf("input pos:");
		scanf("%d",&pos);
		dlist_insert(H,100, &pos);
		dlist_show(H);
	}
	return 0;
}

**1、双向循环链表的创建 **

1、malloc内存;
2、prior next
3、return

dlistnode *dlist_create()
{
    int n;
    distnode *H, *r, *p;
    if((H = (distnode *)malloc(sizeof(distnode))) == NULL)
    {
		printf("malloc failed\n");
		return NULL;
    }
    H->prior = H;
    H->next = H;
    while(1)
    {
    	printf("exit when input: -1\n");
    	scanf("%d",&n);
    	if(n == -1)
        	break;
    	p->data = n;
    	p->prior = r->next;
    	r->next = p;
    	H->prior = P;
    	r = p;
    }
    retuin H;
}

**2、双向循环链表遍历 **

void dlist_show(distnode *H)
{
	dlistnode *p;
	p = H->next;
	while(p != H)
	{
    	printf("%d", p->data);
    	p = p->next; 
    }
	

}

**3、双向循环链表的查找 **

dlistnode *dlist_get(distnode *H,int pos)
{
    int i =0;
    distnode *p = H;
	if(pos < 0)
	{
		printf("pos < 0, invalid!\n");
		return NULL;
	}
	while(i < pos)
	{
		p = p->next;
		i++;
		if(p == H)
		{
			printf("pos is invaild\n");
			return NULL;
		}
	}
	return p;
}

**4、双向循环链表的插入 **

int dlist_insert(distnode *H,int value, int pos)
{
	distnode *p, *q;
	p = dlist_get(H, pos);
	if(p == NULL)
	{
		return -1;
	}
	if((q = (distnode *)malloc(sizeof(distnode))) == NULL){
		printf("malloc error");
	    return -1;
	}
	q->data = value;
	q->prior = p-prior;
	q->next = p;
	p->prior->next = q;
	p->prior = q;
	return 0;

}

**5、双向循环链表的删除 **

int dlist_delete(distnode *H, int pos)
{
	dlistnode *p;
	p = dlist_get(H, pos);
	if(p == NULL)
	{
    	return -1;
	}
	p->prior->next = p->next;
	p->next->prior = p->prior;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值