双向链表的相关操作

typedef char DataType;

typedef struct node
{
	DataType data;
	struct node *next, *prior;
}DUNODE;


/************************************************************************
函数名:  CreateDoublyLinkedList
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-4-17
作  用:  创建双向链表
形参数:  
返回值:                                              
************************************************************************/
DUNODE* CreateDoublyLinkedList()
{
	DUNODE *head = (DUNODE*)malloc(sizeof(DUNODE));
	head->next = head->prior = head;

	return head;
}

/************************************************************************
函数名:  Insert
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-4-17
作  用:  往双向链表中插入数据元素
形参数:  
返回值:                                              
************************************************************************/
int Insert(DUNODE *head, DataType data, int index)
{
	DUNODE *curDUNODE = (DUNODE*)malloc(sizeof(DUNODE));
	curDUNODE->data = data;

	if (head == NULL)
	{
		return -1;
 	}

	if (index < 0 || index > GetLength(head))
	{
		return -2;
	}

	DUNODE *p = head;
	for(int i = 0; i < index; i++)
	{
		p = p->next;
	}

	curDUNODE->next = p->next;
	curDUNODE->prior = p;
	p->next->prior = curDUNODE;
	p->next = curDUNODE;

	return 0;
}

/************************************************************************
函数名:  GetLength
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-4-17
作  用:  获得双向链表的长度
形参数:  
返回值:                                              
************************************************************************/
int GetLength(DUNODE *head)
{
	int len = 0;
	DUNODE *p = head;

	if (head == NULL)
	{
		return -1;
 	}
	
	while(p->next != head)
	{
		len++;
		p = p->next;
	}

	return len;
}

/************************************************************************
函数名:  Delete
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-4-17
作  用:  删除双向链表中指定位置的元素
形参数:  
返回值:                                              
************************************************************************/
int Delete(DUNODE *head, int index)
{
	DUNODE *pre = head;
	DUNODE *p;

	if (head == NULL)
	{
		return -1;
	}

	if (index < 0 || index >= GetLength(head))
	{
		return -2;
	}

	for(int i = 0; i < index; i++)
	{
		pre = pre->next;
	}
	
	p = pre->next;
	pre->next->next->prior = pre;
	pre->next = pre->next->next;
	free(p);

	return 0;
}

/************************************************************************
函数名:  Delete
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-4-17
作  用:  删除双向链表第一个与指定元素相等的元素
形参数:  
返回值:                                              
************************************************************************/
int Delete(DUNODE *head, DataType data)
{
	DUNODE *pre = head;
	DUNODE *p;

	if (head == NULL)
	{
		return -1;
	}

	while(pre->next != head && pre->next->data != data)
	{
		pre = pre->next;
	}

	if (pre->next == head)
	{
		return -3;
	}

	p = pre->next;
	pre->next->next->prior = pre;
	pre->next = pre->next->next;
	free(p);
	
	return 0;
}

/************************************************************************
函数名:  ClearList
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-4-17
作  用:  清除双向链表
形参数:  
返回值:                                              
************************************************************************/
int ClearList(DUNODE *head)
{
	DUNODE *q;

	if (head == NULL)
	{
		return -1;
	}

	DUNODE *p = head->next;
	while(p != head)
	{
		q = p->next;
		q->prior = head;
		head->next = q;
		free(p);
		p = q;
	}

	return 0;
}

/************************************************************************
函数名:  IsEmpty
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-4-17
作  用:  判断双向链表是否为空
形参数:  
返回值:                                              
************************************************************************/
BOOL IsEmpty(DUNODE *head)
{
	if (head == NULL || head->next == head)
	{
		return true;
	}

	return false;
}

/************************************************************************
函数名:  GetAt
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-4-17
作  用:  获取双向链表中指定位置的元素
形参数:  
返回值:                                              
************************************************************************/
DataType GetAt(DUNODE *head, int index)
{
	if (head == NULL)
	{
		return -1;
	}

	DUNODE *p = head->next;
	if (index < 0 || index >= GetLength(head))
	{
		return -2;
	}

	for(int i = 0; i < index; i++)
	{
		p = p->next;
	}

	return p->data;
}

/************************************************************************
函数名:  FindAddr
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-4-17
作  用:  获取双向链表中指定元素的地址
形参数:  
返回值:                                              
************************************************************************/
DUNODE* FindAddr(DUNODE *head, DataType data)
{
	if (head == NULL)
	{
		return NULL;
	}

	DUNODE *p = head->next;
	while(p != head && p->data != data)
	{
		p = p->next;
	}

	if (p == head)
	{
		return NULL;
	}

	return p;
}

/************************************************************************
函数名:  FindIndex
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-4-17
作  用:  获取双向链表中指定元素的索引
形参数:  
返回值:                                              
************************************************************************/
int FindIndex(DUNODE *head, DataType data)
{
	int index = 0;

	if (head == NULL)
	{
		return -1;
	}

	DUNODE *p = head->next;
	while(p != head && p->data != data)
	{
		index++;
		p = p->next;
	}

	if (p == head)
	{
		return -3;
	}

	return index;
}

/************************************************************************
函数名:  SetAt
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-4-17
作  用:  将指定位置的元素赋予指定值
形参数:  
返回值:                                              
************************************************************************/
int SetAt(DUNODE *head, int index, DataType data)
{
	if (head == NULL)
	{
		return -1;
	}

	DUNODE *p = head->next;
	if (index < 0 || index >= GetLength(head))
	{
		return -2;
	}

	for(int i = 0; i < index; i++)
	{
		p = p->next;
	}

	p->data = data;

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值