0x00数据结构——C语言实现(双向循环链表)

0x00数据结构——C语言实现(双向循环链表)

/*
 循环双链表
 
 Functions:
 (在链表中增加附加头结点的版本)
	创建一个空线性表
	将链表置为空表
	计算表长度
	返回附加头结点的地址
	搜索函数:找x在表中的位置,返回表项位置
	定位函数:返回第i个表项在表中的位置
	取第i个表项的值
	用x修改第i个表项的内容
	插入x在表中第i个表项之后,函数返回成功标志
	删除表中第i个表项,通过x返回删除表项的值,函数返回成功标志
	判断表空,空返回真,否则返回假
	判断表满:满返回真,否则返回假
	输出
	对当前的表排序
	
*/

#ifndef D_C_LIST
#define D_C_LIST

//假定每个表项的类型为T
typedef int T;
#define MAXLEN 100
typedef enum {
	false = 0,
	true
} BOOL;

//双链表结点的数据结构。
struct node;
typedef struct node dcnode;
typedef struct node *to_node;
typedef to_node d_c_list;
typedef to_node pos;

//创建一个空链表
dc_list create_list(void);

//将链表置为空表
BOOL set_empty(dc_list l);

//计算表长度
int calc_length(dc_list l);

//返回附加头结点的地址
dc_list head_addr(dc_list l);

//搜索函数:找x在表中的位置,返回表项位置
pos search(dc_list l, T x);

//定位函数:返回第i个表项在表中的位置
pos locate(dc_list l, int i);

//取第i个表项的值
T get_val(dc_list l, int i);

//用x修改第i个表项的内容
BOOL change_val(dc_list l, int i, const T x);

//插入x在表中第i个表项之后,函数返回成功标志
BOOL insert_val(dc_list l, int i, const T x);

//删除表中第i个表项,通过x返回删除表项的值,函数返回成功标志
BOOL delete_val(dc_list l, int i, T *x);

//判断表空,空返回真,否则返回假
BOOL isempty(dc_list l);

//输出
void output(dc_list l);

//对当前的表排序
BOOL sort(dc_list l);

#endif
#include <stdio.h>
#include <stdlib.h>
#include "double_linkedc_list.h"


/*
循环双链表结点的数据结构
*/
struct node{
	T val;
	struct node *prev;
	struct node *next;
};
/*
struct node;
typedef struct node dcnode;
typedef struct node *to_node;
typedef to_node d_c_list;
typedef to_node pos;
*/

//创建一个空循环双链表
dc_list create_list(void)
{
	dc_list l = (dc_list)malloc(sizeof(dcnode));
	l->next = l;
	l->prev = l;
	l->val = 0;
	return l;
}
//该循环双链表带头节点,头节点的val域存储链表的长度

//将链表置为空表
BOOL set_empty(dc_list l)
{
	pos tmp = l->next, ttemp;
	while(tmp!=l){
		ttemp = tmp->next;
		free(tmp);
		tmp = ttemp;
		}
	l->next = l;
	l->prev = l;
	l->val = 0;
	return true;
}

//计算表长度
int calc_length(dc_list l)
{
	return l->val;
}

//返回附加头结点的地址
pos head_addr(dc_list l)
{
	return l;
}

//搜索函数:找x在表中的位置,返回表项位置
pos search(dc_list l, T x)
{
	int i = 1;
	pos tmp;
	tmp = l->next;
	while(tmp != l && tmp->val != x && i<=l->val){
		tmp = tmp->next;
		i++;
	}
	if(i>l->val) return NULL;
	else return tmp;
}

//定位函数:返回第i个表项在表中的位置
pos locate(dc_list l, int i)
{
	if(i<=l->val && i>0){
		pos tmp;
		tmp = l;
		while(--i>=0){
			tmp = tmp->next;
		}
		return tmp;
	} else {
		printf("can not access the %d'th element\n", i);
		return NULL;
	}
}

//取第i个表项的值
T get_val(dc_list l, int i)
{
	if(i<=l->val && i>0){
		pos tmp;
		tmp = l;
		while(--i>=0){
			tmp = tmp->next;
		}
		return tmp->val;
	} else {
		printf("can not access the %d'th element\n", i);
		return -1;
	}
}

//用x修改第i个表项的内容
BOOL change_val(dc_list l, int i, const T x)
{
	if(i<=l->val && i>0){
		pos tmp;
		tmp = l;
		while(--i>=0){
			tmp = tmp->next;
		}
		tmp->val = x;
		return true;
	} else {
		return false;
	}
}

//插入x在表中第i个表项之后,函数返回成功标志
BOOL insert_val(dc_list l, int i, const T x)
{
	if(i<=l->val && i>=0){
		pos tmp;
		tmp = l;
		while(--i>=0){
			tmp = tmp->next;
		}
		pos t = (pos)malloc(sizeof(dcnode));
		t->next = tmp->next;
		(tmp->next)->prev = t;
		t->prev = tmp;
		tmp->next = t;
		t->val = x;
		l->val++;
		return true;
	} else {
		return false;
	}
}

//删除表中第i个表项,通过x返回删除表项的值,函数返回成功标志
BOOL delete_val(dc_list l, int i, T *x)
{
	if(i<=l->val && i>0){
		i--;
		pos tmp;
		tmp = l;
		while(--i>=0 && tmp!=NULL){
			tmp = tmp->next;
		}
		*x = (tmp->next)->val;
		
		tmp->next = (tmp->next)->next;
		(tmp->next)->prev = tmp;
		l->val--;
		return true;
	} else {
		return false;
	}
}

//判断表空,空返回真,否则返回假
BOOL isempty(const dc_list l)
{
	return (l->val == 0 ? true:false);
}

//输出
void output(const dc_list l)
{
	pos tmp = NULL;
	int i = 1;
	tmp = l->next;
	while(tmp!=NULL){
		printf("the %dth element is %d\n", i++, tmp->val);
		tmp = tmp->next;
	}
}

//对当前的表排序
BOOL sort(const dc_list l)
{
	if(l->val < 2)
		return true;
	//冒泡排序
	pos tmp, tmp1, tmp2;
	int i;
	tmp = l->prev;
	while(tmp->prev != l) {
		
		tmp1 = l->next;
		tmp2 = tmp1->next;
		while(tmp1 != tmp) {
			if(tmp1->val > tmp2->val) {
				i = tmp1->val;
				tmp1->val = tmp2->val;
				tmp2->val = i;
			}
			tmp1 = tmp1->next;
			tmp2 = tmp2->next;
		}
		
		tmp = tmp->prev;
	}
	return true;
	
	//快速排序
	
	//归并排序
	
}
posted @ 2018-04-23 21:19  main_c  阅读(167)  评论(0)    收藏  举报