11.27整理

有道云笔记

1双向循环链表

head.h

#ifndef __HEAD_H__
#define __HEAD_H__

typedef int datatype;

typedef struct node{
	datatype data;
	struct node *next;
	struct node *priv;	
}*doublelist;

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

doublelist creat_space();
doublelist toucha(doublelist head,datatype element);
doublelist weicha(doublelist head,datatype element);
int output(doublelist head);
doublelist toushan(doublelist head);
doublelist weishan(doublelist head);


#endif

test.c

#include "head.h"
doublelist creat_space()
{
	doublelist p=(doublelist)malloc(sizeof(struct node));
	if(p==NULL)
		return NULL;
	p->data=0;
	p->next=p;
	p->priv=p;

return p;
}
doublelist toucha(doublelist head,datatype element)
{
	doublelist s=creat_space();
	if(s==NULL)
		return head;
	s->data=element;
	if(head==NULL)
		head=s;
	else 
	{
		doublelist p=head->priv;
		s->next=head;
		head->priv=s;
		head=s;
		head->priv=p;
		
		p->next=head;
		
	}
	

return head;


}

doublelist weicha(doublelist head,datatype element)
{
	doublelist s=creat_space();
	if(s==NULL)
		return NULL;
	s->data=element;
	if(head==NULL)
		head=s;
	else
	{
		doublelist p=head->priv;
		p->next=s;
		s->next=head;
		s->priv=p;
		head->priv=s;
	
	}
return head;
}
int output(doublelist head)
{
	doublelist p=head;
	if(head==NULL)
		return -1;
	else
	{
#if 1
	while(p->next!=head)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	printf("%d\n",p->data);
#endif
#if 0
	while(p!=head)
	{
		printf("%d ",p->data);
		p=p->priv;
	}
	printf("%d\n",p->data);
#endif
	}

return 0;

}

doublelist toushan(doublelist head)
{
	if(head==NULL)
		return head;
	if(head->next==head)
	{
		free(head);
		head=NULL;
	}
	else
	{
		doublelist q=head->priv;
		doublelist p=head;
		 q->next=head->next;
		 head->next->priv=q;
		head=head->next;
		free(p);
		p=NULL;
	}
	return head;
}

doublelist weishan(doublelist head)
{
	if(head==NULL)
		return head;
	if(head->next==head)
	{
		free(head);
		head=NULL;
	}
	else
	{
		doublelist p=head->priv;
		p->priv->next=head;
		head->priv=p->priv;
		free(p);
		p=NULL;
		
	}
return head;

}

main.c

#include "head.h"

int main()
{
	doublelist head=NULL;	
	int i=0,n=0;
	datatype element;
	printf("input n:");
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		printf("input %d element:",i+1);
		scanf("%d",&element);
		head=weicha(head,element);
	}
	
//	head=toushan(head);
	head=weishan(head);
	int flag=output(head);
//	printf("%d\n",flag);

return 0;
}

2双向链表(略去main.c head.h)

test.c

#include "head.h"

doublelink creat_space( )
{
	doublelink p=(doublelink)malloc(sizeof(struct node));
	if(p==NULL)
		return NULL;
	p->data=0;
	p->next=NULL;
	p->priv=NULL;

}

doublelink toucha(doublelink head,datatype element)
{
	doublelink s=creat_space();
	if(s==NULL)
		return head;
	s->data=element;
	if(head==NULL)
		head=s;
	else
	{
		s->next=head;
		head->priv=s;
		head=s;

	}
	return head;
}

int output(doublelink head)
{
	if(head==NULL)
		return -1;
	else
	{
		doublelink p=head;
		while(p->next!=NULL)
		{
			printf("%d ",p->data);
			p=p->next;

		}
		printf("%d\n",p->data);
	}	
	/*	while(p!=NULL)
		{
		printf("%d ",p->data);
		p=p->priv;

		}
		printf("\n");
		*/
	return 0;

}
doublelink weicha(doublelink head,datatype element)
{
	doublelink s=creat_space();
	s->data=element;
	if(head==NULL)
		head=s;
	else
	{	doublelink p=head;
		while(p->next!=NULL)
			p=p->next;
		p->next=s;
		s->priv=p;
	}
	return head; 

}

doublelink toushan(doublelink head)
{
	if(head==NULL)
		return head;

	doublelink p=head;
	head=head->next;
	if(head!=NULL)
		head->priv=NULL;
	free(p);
	p=NULL;

	return head;

}
doublelink weishan(doublelink head)
{
	if(head==NULL)
		return head;
	doublelink p=head;
	if(head->next==NULL)
	{
		free(head);
		head==NULL;
	}
	else
	{
		while(p->next!=NULL)
			p=p->next;
		//	if(p->priv!=NULL)
		p->priv->next=NULL;
		free(p);
		p=NULL;
	}
	return head;


}

     图

3单向循环链表(略去main.c head.htest.c)

test.c

#include "head.h"
linklist creat_node()
{
	linklist p=(linklist)malloc(sizeof(struct node));
	if(p==NULL)
		return NULL;
	else {
		p->data=0;
		p->next=p;
	
	}

return p;

}
linklist toucha(linklist head,datatype element)
{
	linklist s=creat_node();
	if(s==NULL)
		return head;

	s->data=element;

	if(head==NULL)
		head=s;
	else
	{
		linklist p=head;
		while(p->next!=head)
			p=p->next;

		s->next=head;
		head=s;
		p->next=head;
	}
return head;
}
void output(linklist head)
{
	if(head==NULL)
		printf("empty\n");
	else{
		linklist p=head;
		do
		{
			printf("%d ",p->data);
			p=p->next;
		}while(p!=head);
		printf("\n");
	
	}
}
linklist weicha(linklist head,datatype element)
{
	linklist s=creat_node();
	if(s==NULL)
		return head;

	s->data=element;

	if(head==NULL)
		head=s;
	else
	{
		linklist p=head;
		while(p->next!=head)
			p=p->next;
		p->next=s;
		s->next=head;
	}
	
return head;
}
linklist toushan(linklist head)
{
	if(head==NULL)
		return head;
	if(head->next==head)
	{
		free(head);
		head=NULL;
	}
	else
	{
		linklist s=head;
		linklist p=head;
		while(p->next!=head)
			p=p->next;
		head=head->next;
		free(s);
		s=NULL;
		p->next=head;
	}
	return head;
}
linklist weishan(linklist head)
{
	if(head==NULL)
		return head;
	
	
	else if(head->next==head)
	{
		free(head);
		head=NULL;
	}

	else
	{
		linklist p=head;	
		while(p->next->next!=head)
			p=p->next;
		free(p->next);
		p->next=head;
	}
return head;
}

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值