您的位置:首页 > 其它

带头结点的双向链表

2018-02-02 16:25 447 查看
双向链表:每个数据节点中都有两个指针,分别指向该节点的前一个节点和后一个节点,所以在进行操作时就比较方便。在这里,我写的是带头节点的双向链表,所谓的头结点,它仅仅只是一个标志,并不保存任何数据。上面就说一个简单的双向链表。下面是我的代码:基本操作有:①双向链表的头插,尾插,头删,尾删,查找;②双向链表的任意位置之前插入元素,任意位置之后插入元素;依据元素的值进行元素的删除,依据元素的位置进行删除;③删除相同的元素;求链表中元素的个数;判断链表是否为空;RoundLinkNode.h文件#pragma once#define ElemType char#define TESTHEAD printf("\n==========================%s=============================\n",__FUNCTION__)//typedef char ElemType;typedef struct node{ElemType data;struct node* next;struct node* prev;}RoundLinkNode;RoundLinkNode* head;void RoundLinkNode_Init(RoundLinkNode** head,ElemType value);RoundLinkNode* CreateLinkNode(ElemType value);void RoundLinkNodePush(RoundLinkNode* head,ElemType value);void Display_RoundLinkNode(RoundLinkNode* head);void RoundLinkNodePop(RoundLinkNode* head);void DestroyRoundLinkNode(RoundLinkNode* value);RoundLinkNode* RoundLinkNode_FrontPush(RoundLinkNode* head,ElemType value);void RoundLinkNode_FrontPop(RoundLinkNode* head);RoundLinkNode* RoundLinkNodeFind(RoundLinkNode* head,ElemType find);void RoundLinkNode_Insert(RoundLinkNode* head,RoundLinkNode* pos,ElemType value);void RoundLinkNode_Insert_After(RoundLinkNode* head,RoundLinkNode* pos,ElemType value);void RoundLinkNodeErase(RoundLinkNode* head,ElemType value);void RoundLinkNodeRemove(RoundLinkNode* head,RoundLinkNode* pos);void RoundLinkNodeRemoveAll(RoundLinkNode* head,ElemType value);int RoundLinkNodeSize(RoundLinkNode* head);int RoundLinkNodeEmpty(RoundLinkNode* head);
RoundLinkNode.c文件
#include"RoundLinkNode.h"
#include<stdio.h>
#include<stdlib.h>
//创建一个节点
RoundLinkNode* CreateLinkNode(ElemType value)
{
Roun
4000
dLinkNode* new_node = (RoundLinkNode*)malloc(sizeof(RoundLinkNode));
new_node->data = value;
new_node->next = new_node;
new_node->prev = new_node;
return new_node;
}
//销毁一个节点
void DestroyRoundLinkNode(RoundLinkNode* value)
{
free(value);
}
//初始化链表
void RoundLinkNode_Init(RoundLinkNode** head,ElemType value)
{
if(head == NULL)
{
//非法输入
return;
}
*head = CreateLinkNode(value);
}
//打印链表
void Display_RoundLinkNode(RoundLinkNode* head)
{
RoundLinkNode* cur = head->next ;
printf("\n【head】");
if(head == NULL)
{
return;
}
for(cur = head->next;cur!=head;cur=cur->next)
{
printf(" -> 【%c:%p】  ",cur->data ,cur);
}
printf("\n【head】");
for(cur = head->prev;cur!=head;cur=cur->prev)
{
printf(" -> 【%c:%p】  ",cur->data ,cur);
}
printf("\n");
}
//尾插
void RoundLinkNodePush(RoundLinkNode* head,ElemType value)
{
RoundLinkNode* new_node = CreateLinkNode(value);
RoundLinkNode* new_node_next = head;
RoundLinkNode* new_node_prev = head->prev;
if(head == NULL)
{
//非法输入
return;
}
new_node->prev = new_node_prev;
new_node_prev->next  = new_node;
new_node->next = new_node_next;
new_node_next->prev  = new_node;
}
//尾删
void RoundLinkNodePop(RoundLinkNode* head)
{
RoundLinkNode* to_deleted = head->prev ;
if(head == NULL)
{
//非法输入
return;
}
if(head->next == head)
{
printf("空链表\n");
return;
}
head->prev = to_deleted->prev ;
to_deleted->prev ->next = head;
DestroyRoundLinkNode(to_deleted);
}
//头插
RoundLinkNode* RoundLinkNode_FrontPush(RoundLinkNode* head,ElemType value)
{
RoundLinkNode* new_node = CreateLinkNode(value);
RoundLinkNode* new_node_prev = head;
RoundLinkNode* new_node_next = head->next ;
if(head == NULL)
{
//非法输入
return NULL;
}
new_node->prev = new_node_prev;
new_node_prev->next = new_node;
new_node->next = new_node_next;
new_node_next->prev = new_node;
return new_node;
}
//头删
void RoundLinkNode_FrontPop(RoundLinkNode* head)
{
RoundLinkNode* to_deleted = head->next ;
if(head==NULL)
{
//非法输入
return;
}
if(head->next == head)
{
printf("空链表\n");
return;
}
head->next = to_deleted->next ;
to_deleted->next->prev = head;
DestroyRoundLinkNode(to_deleted);
}
//查找
RoundLinkNode* RoundLinkNodeFind(RoundLinkNode* head,ElemType find)
{
RoundLinkNode* cur = head->next ;
if(head == NULL)
{
//非法输入
return NULL;
}
if(head->next == head)
{
printf("空链表\n");
return NULL;
}
for(;cur!=head;cur=cur->next )
{
if(cur->data == find)
{
return cur;
}
}
return NULL;
}
//任意位置之前插入一个元素
void RoundLinkNode_Insert(RoundLinkNode* head,RoundLinkNode* pos,ElemType value)
{
RoundLinkNode* new_node = CreateLinkNode(value);
RoundLinkNode* new_node_prev = pos->prev ;
RoundLinkNode* new_node_next = pos;
if(head == NULL)
{
//非法输入
return;
}
new_node->next = pos;
pos->prev = new_node;
new_node->prev = new_node_prev;
new_node_prev->next  = new_node ;
return;
}
//任意位置之后插入一个元素
void RoundLinkNode_Insert_After(RoundLinkNode* head,RoundLinkNode* pos,ElemType value)
{
RoundLinkNode* new_node = CreateLinkNode(value);
RoundLinkNode* new_node_prev = pos ;
RoundLinkNode* new_node_next = pos->next ;
if(head == NULL)
{
//非法输入
return;
}
new_node->prev = pos;
pos->next = new_node;
new_node->next = new_node_next;
new_node_next->prev  = new_node ;
return;
}
void RoundLinkNodeErase(RoundLinkNode* head,ElemType value)
{
RoundLinkNode* p = NULL;
RoundLinkNode* p_pre = NULL ;
RoundLinkNode* p_nex = NULL ;
if(head == NULL)
{
//非法输入
return;
}
if(head->next == head)
{
printf("空链表\n");
return;
}
p = RoundLinkNodeFind(head,value);
if(p == NULL)
{
//非法输入
return;
}
p_pre = p->prev ;
p_nex = p->next ;
p_pre->next = p_nex;
p_nex->prev = p_pre;
DestroyRoundLinkNode(p);
return;
}
//按位置删除
void RoundLinkNodeRemove(RoundLinkNode* head,RoundLinkNode* pos)
{
RoundLinkNode* pos_pre = NULL;
RoundLinkNode* pos_nex = NULL;
if(head == NULL || pos == NULL)
{
//非法输入
return;
}
if(head->next == NULL)
{
printf("空链表。\n");
return;
}
pos_pre = pos->prev ;
pos_nex = pos->next ;
pos_pre->next = pos_nex;
pos_nex->prev = pos_pre;
DestroyRoundLinkNode(pos);
return;
}
//删除所有相同的元素
void RoundLinkNodeRemoveAll(RoundLinkNode* head,ElemType value)
{
RoundLinkNode* del = NULL;
RoundLinkNode* del_pre = NULL;
RoundLinkNode* del_nex = NULL;
RoundLinkNode* cur = head->next ;
int flag = 0;
if(head == NULL)
{
//非法输入
return;
}
if(head->next == NULL)
{
printf("空链表.\n");
return;
}
while(cur != head)
{
if(cur->data == value)
{
del = cur;
del_pre = cur->prev;
del_nex = cur->next ;
del_pre->next = del_nex;
del_nex->prev = del_pre;
flag = 1;
}
cur = cur->next ;
}
if(flag == 0)
{
printf("该链表中不存在该节点.\n");
return;
}
return;
}
//求节点个数
int RoundLinkNodeSize(RoundLinkNode* head)
{
RoundLinkNode* cur = head->next ;
int count = 1;
if(head == NULL)
{
//非法输入
return -1;
}
if(head->next == NULL)
{
printf("空链表.\n");
return 0;
}
for(cur = head->next ;cur != head;cur = cur->next )
{
++count;
}
return count;
}
//判空
int RoundLinkNodeEmpty(RoundLinkNode* head)
{
if(head == NULL)
{
//非法输入
return -1;
}
return (head->next->data == NULL) ? 1 : 0;
}
test.c文件
#include"RoundLinkNode.h"
#include<stdio.h>
#include<stdlib.h>
void TEST_RoundLinkNodePush()
{
TESTHEAD;
RoundLinkNode_Init(&head,0);
RoundLinkNodePush(head,'a');
RoundLinkNodePush(head,'b');
RoundLinkNodePush(head,'c');
RoundLinkNodePush(head,'d');
Display_RoundLinkNode(head);
}
void TEST_RoundLinkNodePop()
{
TESTHEAD;
RoundLinkNode_Init(&head,0);
RoundLinkNodePush(head,'a');
RoundLinkNodePush(head,'b');
RoundLinkNodePush(head,'c');
RoundLinkNodePush(head,'d');
Display_RoundLinkNode(head);
RoundLinkNodePop(head);
RoundLinkNodePop(head);
Display_RoundLinkNode(head);
RoundLinkNodePop(head);
RoundLinkNodePop(head);
Display_RoundLinkNode(head);
RoundLinkNodePop(head);
}
void TEST_RoundLinkNode_FrontPush()
{
TESTHEAD;
RoundLinkNode_Init(&head,0);
RoundLinkNode_FrontPush(head,'a');
RoundLinkNode_FrontPush(head,'b');
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'d');
Display_RoundLinkNode(head);
}
void TEST_RoundLinkNode_FrontPop()
{
TESTHEAD;
RoundLinkNode_Init(&head,0);
RoundLinkNode_FrontPush(head,'a');
RoundLinkNode_FrontPush(head,'b');
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'d');
Display_RoundLinkNode(head);
RoundLinkNode_FrontPop(head);
RoundLinkNode_FrontPop(head);
Display_RoundLinkNode(head);
RoundLinkNode_FrontPop(head);
RoundLinkNode_FrontPop(head);
Display_RoundLinkNode(head);
RoundLinkNode_FrontPop(head);
}
void TEST_RoundLinkNodeFind()
{
RoundLinkNode* d = NULL;
TESTHEAD;
RoundLinkNode_Init(&head,0);
RoundLinkNode_FrontPush(head,'a');
RoundLinkNode_FrontPush(head,'b');
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'d');
Display_RoundLinkNode(head);
d = RoundLinkNodeFind(head,'d');
printf("d = 【%p】\n",d);
}
void TEST_RoundLinkNodeInsert()
{
RoundLinkNode* p = NULL;
TESTHEAD;
RoundLinkNode_Init(&head,0);
RoundLinkNode_FrontPush(head,'a');
p = RoundLinkNode_FrontPush(head,'b');
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'d');
Display_RoundLinkNode(head);
RoundLinkNode_Insert(head,p,'m');
Display_RoundLinkNode(head);
}
void TEST_RoundLinkNodeInsert_After()
{
RoundLinkNode* p = NULL;
TESTHEAD;
RoundLinkNode_Init(&head,0);
RoundLinkNode_FrontPush(head,'a');
p = RoundLinkNode_FrontPush(head,'b');
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'d');
Display_RoundLinkNode(head);
RoundLinkNode_Insert_After(head,p,'m');
Display_RoundLinkNode(head);
}
void TEST_RoundLinkNodeErase()
{
TESTHEAD;
RoundLinkNode_Init(&head,0);
RoundLinkNode_FrontPush(head,'a');
RoundLinkNode_FrontPush(head,'b');
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'d');
Display_RoundLinkNode(head);
RoundLinkNodeErase(head,'a');
Display_RoundLinkNode(head);
RoundLinkNodeErase(head,'c');
Display_RoundLinkNode(head);
RoundLinkNodeErase(head,'d');
Display_RoundLinkNode(head);
RoundLinkNodeErase(head,NULL);
Display_RoundLinkNode(head);
}
void TEST_RoundLinkNodeRemove()
{
RoundLinkNode* p = NULL;
TESTHEAD;
RoundLinkNode_Init(&head,0);
RoundLinkNode_FrontPush(head,'a');
RoundLinkNode_FrontPush(head,'b');
RoundLinkNode_FrontPush(head,'c');
p = RoundLinkNode_FrontPush(head,'d');
Display_RoundLinkNode(head);
RoundLinkNodeRemove(head,p);
Display_RoundLinkNode(head);
}
void TEST_RoundLinkNodeRemoveAll()
{
TESTHEAD;
RoundLinkNode_Init(&head,0);
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'a');
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'b');
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'d');
RoundLinkNode_FrontPush(head,'c');
Display_RoundLinkNode(head);
RoundLinkNodeRemoveAll(head,'c');
Display_RoundLinkNode(head);
RoundLinkNodeRemoveAll(head,'w');
Display_RoundLinkNode(head);
}
void TEST_RoundLinkNodeSize()
{
int ret;
TESTHEAD;
RoundLinkNode_Init(&head,0);
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'a');
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'b');
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'d');
RoundLinkNode_FrontPush(head,'c');
Display_RoundLinkNode(head);
ret = RoundLinkNodeSize(head);
printf("该链表的节点个数为:%d\n",ret);
}
void TEST_RoundLinkNodeEmpty()
{
int ret = 0;
TESTHEAD;
RoundLinkNode_Init(&head,0);
//ret = RoundLinkNodeEmpty(head);
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'a');
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'b');
RoundLinkNode_FrontPush(head,'c');
RoundLinkNode_FrontPush(head,'c');
Display_RoundLinkNode(head);
ret = RoundLinkNodeEmpty(head);
printf("该链表是否为空:%d\n",ret);
}

////////////////////////////////////////////////////////////////////////////
int main()
{
TEST_RoundLinkNodePush();
TEST_RoundLinkNodePop();
TEST_RoundLinkNode_FrontPush();
TEST_RoundLinkNode_FrontPop();
TEST_RoundLinkNodeFind();
TEST_RoundLinkNodeInsert();
TEST_RoundLinkNodeInsert_After();
TEST_RoundLinkNodeErase();
TEST_RoundLinkNodeRemove();
TEST_RoundLinkNodeRemoveAll();
TEST_RoundLinkNodeSize();
TEST_RoundLinkNodeEmpty();
return 0;
}
以上都是在VS2008环境下实现的。。。。。。。。。。。。下面是运行的结果:

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: