双向循环链表的基本操作

DLink.h

#pragma once 
#include <stdio.h>
typedef int DLinkType;
typedef struct DLinkNode {
 DLinkType data;
 struct DLinkNode* _next;
 struct DLinkNode* _prev;
} DLinkNode;

DLinkNode* BuyNode(DLinkType value);
void DestroyNode(DLinkNode** PosNode);//删除节点
void PrintDlink(DLinkNode*head);//打印链表
void InitDLinkList(DLinkNode** head);//初始化
DLinkNode* PushBackDLinkList(DLinkNode*phead, DLinkType value);//尾插
void* PopBackDLinList(DLinkNode** head);//尾删
void PushFrontDLinkList(DLinkNode** head, DLinkType value);//头插
void PopFrontDLinkList(DLinkNode* head);//头删
DLinkNode* FindDLinkList(DLinkNode* head, DLinkType to_find);//查找
void InsertDLinkList(DLinkNode** head, DLinkNode* pos, DLinkType value);//在某个节点前插入元素
void InsertAfterDLinkList(DLinkNode** head, DLinkNode* pos, DLinkType value);//在某个节点后插入元素
void DLinkListErase(DLinkNode** head);//链表的删除
void DLinkListRemove(DLinkNode** head, DLinkType value);//删除值为value的一个元素
void DLinkListRemoveAll(DLinkNode** head, DLinkType value); //删除整个链表中值为value的元素
size_t DLinkListSize(DLinkNode* head);//求链表大小
int DLinkListEmpty(DLinkNode* head);//判空


DLink.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<stdlib.h>
#include<assert.h>
#include"DLink.h"

void DestroyNode(DLinkNode*PosNode)
{
 assert(PosNode);
 free(PosNode);
 PosNode = NULL;
}
void PrintDlink(DLinkNode* head)
{
 assert(head);
 DLinkNode*CurNode = head;
 printf("<=  ");
 while ((CurNode->_next)!=head)
 {
  printf("%d  <=>  ", CurNode->data);
  CurNode = CurNode->_next;
 }
 printf("%d  =>\n",CurNode->data);
}
void InitDLinkList(DLinkNode** head)
{
 if (head == NULL) {
  return;
 }
 *head = NULL;
}
DLinkNode* BuyNode(DLinkType value) {
 DLinkNode* new_node = (DLinkNode*)malloc(sizeof(DLinkNode));
 if (new_node != NULL) {
  new_node->_prev = NULL;
  new_node->_next = NULL;
  new_node->data = value;
 }
 return new_node;
}
DLinkNode* PushBackDLinkList(DLinkNode**head, DLinkType value)
{
 assert(head);
 DLinkNode*InsertNode = BuyNode(value);
 if (*head == NULL)
 {
  *head = InsertNode;
  InsertNode->_next = InsertNode;
  InsertNode->_prev = InsertNode;
  return InsertNode;
 }
 DLinkNode*CurNode = (*head);
 while (CurNode->_next != (*head))
 {
  CurNode = CurNode->_next;
 }
 CurNode->_next = InsertNode;
 InsertNode->_next = (*head);
 InsertNode->_prev = CurNode;
 (*head)->_prev = InsertNode;
 return InsertNode;
}
void* PopBackDLinList(DLinkNode** head)
{
 assert(head);
 DLinkNode*CurNode = (*head);
 DLinkNode*PrevNode = NULL;
 while (CurNode->_next != (*head))
 {
  PrevNode = CurNode;
  CurNode = CurNode->_next;
 }
 DestroyNode(CurNode);
 PrevNode->_next = (*head);
 (*head)->_prev = PrevNode;
}
void PushFrontDLinkList(DLinkNode** head, DLinkType value)
{
 assert(head);
 DLinkNode*InsertNode=BuyNode(value);
 DLinkNode*TmpNode = (*head);
 DLinkNode*TailNode = (*head)->_prev;
 (*head) = InsertNode;
 InsertNode->_prev = TailNode;
 InsertNode->_next = TmpNode;
 TailNode->_next = InsertNode;
}
void PopFrontDLinkList(DLinkNode** head)
{
 assert(head);
 DLinkNode* TmpNode = (*head)->_next;
 DLinkNode*TailNode = (*head)->_prev;
 *head = TmpNode;
 TmpNode->_prev = TailNode;
 TailNode->_next = TmpNode;
}
DLinkNode* FindDLinkList(DLinkNode* head, DLinkType to_find)
{
 assert(head);
 DLinkNode*CurNode = (head);
 while (CurNode->_next!=head)
 {
  if (CurNode->data == to_find)
  {
   return CurNode;
  }
  CurNode = CurNode->_next;
 }
}
void InsertDLinkList(DLinkNode** head, DLinkNode* pos, DLinkType value)
{
 assert(pos);
 if (pos == (*head))
 {
  PushFrontDLinkList(head, value);
  return;
 }
 DLinkNode*CurNode = (*head);
 DLinkNode*PrevNode = NULL;
 DLinkNode*InsertNode = BuyNode(value);
 while (CurNode->_next != (*head))
 {
  while (CurNode != pos)
  {
   PrevNode = CurNode;
   CurNode = CurNode->_next;
  }
  PrevNode->_next = InsertNode;
  InsertNode->_prev = PrevNode;
  InsertNode->_next = CurNode;
  CurNode->_prev = InsertNode;
  return;
 }
}
void InsertAfterDLinkList(DLinkNode** head, DLinkNode* pos, DLinkType value)
{
 assert(head);
 DLinkNode*CurNode = (*head);
 if (CurNode==pos)
 {
  PushBackDLinkList(head,value);
  return;
 }
 DLinkNode*InsertNode = BuyNode(value);
 while (CurNode->_next != (*head))
 {
  while (CurNode != pos)
  {
   CurNode = CurNode->_next;
  }
  InsertNode->_next = CurNode->_next;
  CurNode->_next->_prev = InsertNode;
  CurNode->_next = InsertNode;
  InsertNode->_prev = CurNode;
  return;
 }
}
void DLinkListErase(DLinkNode** head)
{
 assert(head);
 free(*head);
 (*head) = NULL;
}
void DLinkListRemove(DLinkNode** head, DLinkType value)
{
 assert(head);
 DLinkNode*CurNode = (*head);
 DLinkNode*PrevNode = NULL;
 DLinkNode*TailNode = NULL;
 while (CurNode->_next != (*head))
 {
  while(CurNode->data != value)
  {
   PrevNode = CurNode;
   CurNode = CurNode->_next;
  }
  TailNode = CurNode->_next;
  PrevNode->_next = TailNode;
  TailNode->_prev = PrevNode;
  return;
 }
}
void DLinkListRemoveAll(DLinkNode** head, DLinkType value)
{
 assert(head);
 DLinkNode*CurNode = (*head);
 DLinkNode*PrevNode = NULL;
 DLinkNode*TailNode = NULL;
 while (CurNode->_next != (*head))
 {
  while (CurNode->data != value)
  {
   PrevNode = CurNode;
   CurNode = CurNode->_next;
  }
  TailNode = CurNode->_next;
  PrevNode->_next = TailNode;
  TailNode->_prev = PrevNode;
  CurNode = CurNode->_next;//不加的话会死循环
 }
}
size_t DLinkListSize(DLinkNode* head)
{
 assert(head);
 DLinkNode*CurNode = (head)->_next;
 size_t count=1;
 while (CurNode != (head))
 {
  ++count;
  CurNode = CurNode->_next;
 }
 printf("count=%d\n", count);
 return count;
}
int DLinkListEmpty(DLinkNode* head)
{
 assert(head);
 return (0==DLinkListSize(head));
}

test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<Windows.h>
#include<stdlib.h>
#include"DLink.h"
int main()
{
 DLinkNode* DLink;
 InitDLinkList(&DLink);
 PushBackDLinkList(&DLink, 1);
 PushBackDLinkList(&DLink, 2);
 PushBackDLinkList(&DLink, 3);
 DLinkNode* tmp = PushBackDLinkList(&DLink, 4);
 DLinkListEmpty(DLink);
 //DLinkListSize(&DLink);
 //DLinkListRemoveAll(&DLink, 2);
 //DLinkListErase(&DLink);
 //PushFrontDLinkList(&DLink, 0);
 //InsertAfterDLinkList(&DLink, tmp, 5);
 //InsertDLinkList(&DLink,tmp , 0);
 //PopBackDLinList(&DLink);
 //PopFrontDLinkList(&DLink);
 //FindDLinkList(DLink, 3);
 PrintDlink(DLink);
 system("pause");
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值