数据结构:环形链表

自己写了个环形链表,就当链表的复习了。


头文件

#ifndef __RINGLIST_H__
#define __RINGLIST_H__

#define FALSE 0
#define TRUE 1

typedef int RINGDATA;

typedef struct _ringlist
{
	RINGDATA data;
	struct _ringlist *next;
}RINGLIST;

//创建环形链表
RINGLIST* Creat_List();

//尾插
int Insert_Last(RINGLIST* h,RINGDATA data);

//头插
int Insert_Head(RINGLIST* h,RINGDATA data);

//再第pos位插入数据
int Insert_Pos(RINGLIST* h,int pos,RINGDATA data);

//删除在pos位的结点
int Delete_Pos(RINGLIST* h,int pos);

//输出环形链表(只循环一次)
void Display(RINGLIST* h);

// 逆序
int Reverse_Ring(RINGLIST* h);

//删除指定的数据(只能删除第一个)
int Delete_Data(RINGLIST* h,RINGDATA data);

//查找元素:通过值查找,返回位置(只能查找第一个)
int Find_Element(RINGLIST* h,RINGDATA data,int *x);

//查找元素:通过位置获取,返回值
int Get_Element(RINGLIST* h, int pos,RINGDATA *x);

// 求链表长度:通过函数返回值返回(循环一次的长度)
int Get_Len(RINGLIST* h);

//清空链表
int Clear_Ring(RINGLIST* h);

//删除链表
int Destroy(RINGLIST** h);

#endif


实现函数
#include
#include
#include"RingList.h"

//创建环形链表
RINGLIST* Creat_List()
{
	RINGLIST* node = (RINGLIST*)malloc(sizeof(RINGLIST)/sizeof(char));
	if(node == NULL)
		return NULL;
	
	node->next = node;
	return node;
}

//尾插
int Insert_Last(RINGLIST* h,RINGDATA data)
{
	if(h == NULL)
		return FALSE;
	
	RINGLIST* node = (RINGLIST*)malloc(sizeof(RINGLIST)/sizeof(char));
	if(node == NULL)
		return FALSE;
	node->data = data;
	node->next = h;
	
	RINGLIST* tmp = h;
	while(tmp->next != h)
	{
		tmp = tmp->next;
	}
	tmp->next = node;
	
	return TRUE;
}

//头插
int Insert_Head(RINGLIST* h,RINGDATA data)
{
	if(h == NULL)
		return FALSE;
	
	RINGLIST* node = (RINGLIST*)malloc(sizeof(RINGLIST)/sizeof(char));
	if(node == NULL)
		return FALSE;
	node->data = data;
	
	node->next = h->next;
	h->next = node;
	
	return TRUE;
}

//再第pos位插入数据
int Insert_Pos(RINGLIST* h,int pos,RINGDATA data)
{
	if(h == NULL || pos<1 )
		return FALSE;
	
	RINGLIST* node = (RINGLIST*)malloc(sizeof(RINGLIST)/sizeof(char));
	if(node == NULL)
		return FALSE;
	node->data = data;
	
	int i;
	RINGLIST* tmp = h;
	for(i=0;inext;
		if(tmp == h)
		{		
			printf("插入位置越界,插入失败。\n");
			return FALSE;
		}
	}
	node->next = tmp->next;
	tmp->next = node;
	
	return TRUE;
}

//删除在pos位的结点
int Delete_Pos(RINGLIST* h,int pos)
{
	if(h == NULL || h->next == h || pos<1)
		return FALSE;
	
	int i;
	RINGLIST* tmp = h;
	for(i=0;inext == h)
			break;
		tmp = tmp->next;
	}
	if(tmp->next == h)
	{
		printf("删除失败,删除位置越界。\n");
		return FALSE;
	}
	RINGLIST* p = tmp->next;
	tmp->next = p->next;
	free(p);
	
	return TRUE;
}

//输出环形链表(只循环一次)
void Display(RINGLIST* h)
{
	if(h == NULL)
		return ;
	
	printf("环形链表——————————————\n");
	RINGLIST* tmp = h->next;
	while(tmp != h)
	{
		printf("%4d",tmp->data);
		tmp = tmp->next;
	}
	printf("\n");
}

// 逆序
int Reverse_Ring(RINGLIST* h)
{
	if(h == NULL || h->next == h || h->next->next == h)
		return FALSE;
	
	RINGLIST* pre = h->next;
	RINGLIST* cur = h->next->next;
	RINGLIST* p;
	while(cur != h)
	{
		p = cur->next;
		cur->next = pre;
		pre = cur;
		cur = p;
	}
	h->next->next = h;
	h->next = pre;
	
	return TRUE;
}

//删除指定的数据
int Delete_Data(RINGLIST* h,RINGDATA data)
{
	if(h == NULL || h->next == h)
	{
		printf("删除失败,环形连表为空。\n");
		return FALSE;
	}
	
	RINGLIST* tmp = h;
	while(tmp->next != h)
	{
		if(tmp->next->data == data)
			break;
		tmp = tmp->next;
	}
	if(tmp->next == h)
	{
		printf("删除失败,没有该数据\n");
		return FALSE;
	}
	
	RINGLIST* p = tmp->next;
	tmp->next = p->next;
	free(p);
	
	return TRUE;
}

//查找元素:通过值查找,返回位置
int Find_Element(RINGLIST* h,RINGDATA data,int *x)
{
	if(h == NULL || h->next == h)
	{
		printf("查找失败,环形连表为空。\n");
		return FALSE;
	}
	
	int count = 0;
	RINGLIST* tmp = h->next;
	while(tmp != h)
	{
		count ++;
		if(tmp->data == data)
			break;
		tmp = tmp->next;
	}
	if(tmp == h)
	{
		printf("查找失败,没有该元素。\n");
	}	
	*x = count;
	
	return TRUE;
}


//查找元素:通过位置获取,返回值
int Get_Element(RINGLIST* h, int pos,RINGDATA *x)
{
	if(h == NULL || h->next == h || pos<1)
	{
		printf("查找失败,环形连表为空。\n");
		return FALSE;
	}
	
	RINGLIST* tmp = h;
	int i;
	for(i=0;inext;
		if(tmp == h)
			break;
	}
	if(tmp == h)
	{
		printf("查找失败,位置越界。\n");
		return FALSE;
	}
	
	*x = tmp->data;
	return TRUE;
}

// 求链表长度:通过函数返回值返回(循环一次的长度)
int Get_Len(RINGLIST* h)
{
	int count = 0;
	RINGLIST* tmp = h->next;
	while(tmp != h)
	{
		count ++;
		tmp = tmp->next;
	}
	return count;
}

//清空链表
int Clear_Ring(RINGLIST* h)
{
	if(h == NULL || h->next == h)
	{
		return FALSE;
	}
	
	int i;
	int len = Get_Len(h);
	for(i=0;i



你可能感兴趣的:(沙僧取金:第一站,Linus,c)