C语言双向环形链表

#include<stdio.h>
#include<stdlib.h>

struct list 
{
	struct list *pr;
	int data;
	struct list *next;
};

struct list* Create()
{
	struct list *head,*pnew,*pre;
	int i;
	int number = 5;
	head = (struct list*)malloc(sizeof(struct list));
	head->data = 0;
	head->next = NULL;
	pre = head;
	for(i=1;i<number;i++)
	{
		pnew = (struct list*)malloc(sizeof(struct list));
		pnew->pr = pre;
		pnew->data = i;
		pnew->next = NULL;
		pre->next = pnew;
		pre = pre->next;
	}
	pnew->next = head;
	head->pr = pnew;
	return head;
}

struct list* Insert(struct list *head,int pos)
{
	struct list *pnew,*end;
	int i;
	pnew = (struct list*)malloc(sizeof(struct list));
	pnew->data = 100;
	if(pos==0)
	{
		head->pr->next = pnew;
		pnew->next = head;
		head->pr = pnew;
		pnew->pr = head->pr;
		head = pnew;
	}
	else
	{
		end = head;
		if(pos>=2)
		{
			for(i=0;i<(pos-1);i++)
			{
				end = end->next;
				if(end->next==NULL)
					break;
			}
		}
		pnew->pr = end;
		pnew->next = end->next;
		if(end->next==NULL)
			pnew->next = NULL;
		else
			end->next->pr = pnew;	
		end->next = pnew;
	}
	return head;
}

struct list* Delete(struct list *head,int pos)
{
	struct list *obj;
	int i;
	if(pos==0)
	{
		obj = head;
		head->pr->next = head->next;
		head->next->pr = head->pr;
		head = head->next;
	}
	else
	{
		obj = head->next;
		if(pos>=2)
		{
			for(i=0;i<(pos-1);i++)
				obj = obj->next;
		}
		obj->pr->next = obj->next;
		obj->next->pr = obj->pr;
		if(obj==head)
			head = head->next;//如果被删除的是head,那么新head为原head下一个链节
		
	}
	free(obj);
	return head;
}

int Check(struct list *head,int pos)
{
	int i;
	if(pos>=1)
	{
		for(i=0;i<pos;i++)
			head = head->next;
	}
	return head->data;
}

void Modify(struct list *head,int pos,int dat)
{
	int i;
	if(pos>=1)
	{
		for(i=0;i<pos;i++)
			head = head->next;
	}
	head->data = dat;
}	

void Print(struct list *head)//只打印有效数据
{
	struct list *temp;
	temp = head;
	printf("%d\n",temp->data);
	do
	{
		temp = temp->next;
		printf("%d\n",temp->data);
	}while(temp->next!=head);//如果再次循环到head处,则停止输出
}

void Print_all(struct list *head)//这个是打印全部数据,也就是一个无限循环
{
	printf("%d\n",head->data);
	head = head->next;
}

void main()
{
	struct list *head;
	int data;
	head = Create();
	data = Check(head,0);
	printf("%d\n",data);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值