C 双向循环链表


//循环链表


#ifndef _DLIST_H_
#define _DLIST_H_

#include <stdio.h>
#include<stdlib.h>
typedef struct node{

		int data;
		struct node *prior, *next;
}dlist_node;

extern dlist_node*  dlist_create();
extern void show_dlist(dlist_node *H);

extern int   empty_dlist(dlist_node *H);
extern dlist_node * dlist_get(dlist_node *H,int pos);//通过位置查找

extern dlist_node *dlist_insert(dlist_node *H,int data,int pos);
extern dlist_node *dlist_delete(dlist_node *H,int pos);



#include "dlist.h"

dlist_node*  dlist_create()
{
	//头节点 的创建
	dlist_node*H = (dlist_node*)malloc(sizeof(dlist_node));
	if(H == NULL)
	{
		printf("creat H error\n");
		exit(-1);
	}
	H->prior = H;
	H->next = H;
//添加节点  在H后面	
	dlist_node *r = H;//r 指的是表尾
	int n;
	while(1)
	{
		printf("input>>");
		scanf("%d",&n);
		getchar();
		if(n == -1)
		{
			break;
		}
		dlist_node *new;
	if((new = (dlist_node*)malloc(sizeof(dlist_node)))  == NULL)
	{
		printf("create node error\n");
		exit(-1);
	}
		new->data = n;
		
		new->prior = r;
		new->next = r->next;
		
		H->prior = new;
		r->next = new;
		r =  new;

	}
	return H;


}
void show_dlist(dlist_node *H)
{
	//当尾端指向H的时候 表示链表 的结束
	dlist_node *p = H->next;
	if(empty_dlist(H))
	{
		printf("empty");
		return ;
	}

	while(p != H)
	{
		printf("%5d",p->data);
		p = p->next;
	}	
	putchar(10);
}
int  empty_dlist(dlist_node *H)
{	
	return (H->next == NULL ? 1:0);
}

dlist_node *dlist_get(dlist_node *H,int pos)
{
	if(pos < 0)
	{
		printf("pos < 0 ,pos < invailed\n");
		return NULL;
	}
	dlist_node *p = H;
	int  i = -1;
	while(i<pos)//寻找位置
	{
			i++;
			p = p->next;
			if(p == H)
			{
				printf("pos is invailed\n");
				return NULL;
			}
	}
	return p;
}


dlist_node *dlist_insert(dlist_node *H,int data,int pos)
{
	if(pos < 0)
	{
		printf("pos < 0 ,pos < invailed\n");
		return NULL;
	}
	dlist_node *p = H;
	int  i = -1;
	while(i < pos)//寻找位置
	{
			i++;
			p = p->next;
			if(p == H)
			{
				printf("pos is invailed\n");
				return NULL;
			}
	}
	dlist_node *new;
	if((new = (dlist_node*)malloc(sizeof(dlist_node)))  == NULL)
	{
		printf("no menory");
		return NULL;
	}	
	new->data = data;

	new->next =  p;
	new->prior = p->prior;
	p->prior->next = new;
	p->prior = new;

	return H;
}

dlist_node *dlist_delete(dlist_node *H,int pos)
{
	if(pos < 0)
	{
		printf("pos < 0 ,pos < invailed\n");
		return NULL;
	}
	dlist_node *p = H;
	int  i = -1;
	while(i < pos)//寻找位置
	{
			i++;
			p = p->next;
			if(p == H)
			{
				printf("pos is invailed\n");
				return NULL;
			}
	}
//找到删除点 跳出循环 后
	p->prior->next = p->next;
	p->next->prior = p->prior;
	free(p);

	return H;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值