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;
}
双向链表的相关操作
最新推荐文章于 2020-06-03 15:45:49 发布