双向循环链表的创建,插入,删除操作

下面请看源代码:

#include <stdio.h>
#include <Windows.h>

typedef struct _Node
{
 int nValue;
 struct _Node* pPre;
 struct _Node* pNext;
}Node;

Node* pHead = NULL;
Node* pEnd = NULL;

 

void Push_Front(int value)   //  在头添加节点
{
 //  需要 一个节点
 Node* temp = (Node*)malloc(sizeof(Node));
 temp->nValue = value;
 temp->pNext = NULL;
 temp->pPre = NULL;

 //  把这个节点放到链表的头
 if (pHead == NULL)
 {
  pHead = temp;
  pEnd = temp;
 }
 else
 {
  //  在头添加
  pHead->pPre = temp;
  temp->pNext = pHead;
  pHead = temp;
 }
 //  头尾连接
 pHead->pPre = pEnd;
 pEnd->pNext = pHead;
}


void Push_Back(int value)   //  在尾添加
{
 //  需要一个节点
 Node* temp = (Node*)malloc(sizeof(Node));
 temp->nValue = value;
 temp->pNext = NULL;
 temp->pPre = NULL;

 //   放到链表的尾部
 if (pHead == NULL)
 {
  pHead = temp;
  pEnd = temp;
 }
 else
 {
  pEnd->pNext = temp;
  temp->pPre = pEnd;
  pEnd = temp;
 }
 //  头尾连接
 pEnd->pNext = pHead;
 pHead->pPre = pEnd;
}

void Pop_Front()   //  在头删除
{
 if (pHead == NULL)   //  看  要删除的这个链表有没有东西
 {
  return;
 }

 if (pHead == pEnd)    //  看这个链表  是不是 就一个节点
 {
  free(pHead);
  pHead = NULL;
  pEnd = NULL;
  return;
 }
 else
 {
  //  两个以上
  Node* temp = pHead;
  //  头向后移动
  pHead = pHead->pNext;
  free(temp);
  // 头尾连接
  pHead->pPre = pEnd;
  pEnd->pNext = pHead;
 }
}

void Pop_Back()
{
 if (pHead == NULL)
 {
  return;
 }

 if (pHead == pEnd)
 {
  free(pHead);
  pHead = NULL;
  pEnd = NULL;
 }
 else
 {
  Node* temp = pEnd;
  pEnd = pEnd->pPre;
  free(temp);
  // 头尾连接
  pHead->pPre = pEnd;
  pEnd->pNext = pHead;
 }
}


void Show_Head(Node* temp)
{
 do
 {
  if (temp == NULL)   // 看  这个链表是不是空链表
  {
   return;    //  结束函数
  }

  printf("%d ",temp->nValue);
  temp = temp->pNext;
 } while (temp != pHead);
}

void Show_End(Node* temp)
{
 do
 {
  if (temp == NULL)   // 看  这个链表是不是空链表
  {
   return;    //  结束函数
  }

  printf("%d ",temp->nValue);
  temp = temp->pPre;
 } while (temp != pEnd);
}

void Insert_Value(int nFindValue,int nInsertValue)   //   在中间插入
{
 Node* temp = pHead;
 //  找nFindValue
 do
 {
  if (temp == NULL)
  {
   break;
  }
  //  找到了 看这个节点是头还是中间节点
  if (temp->nValue == nFindValue)
  {
   //  头就Push_Front
   if (temp == pHead)
   {
    Push_Front(nInsertValue);
   }
   else
   {
    //   中间节点
    Node* node = (Node*)malloc(sizeof(Node));
    node->nValue = nInsertValue;
    //  先连接
    node->pNext = temp;
    node->pPre = temp->pPre;
    //  断开
    temp->pPre = node;
    node->pPre->pNext = node;
   }
   return;
  }
  //   继续查找
  temp = temp->pNext;
 }while(temp != pHead);

 //   没找到就放到最后
 Push_Back(nInsertValue);
}

void Delete_Value(int nDeleteValue)
{
 Node* temp = pHead;
 do
 {
  if (temp == NULL)
  {
   return;
  }
  
  //  看这个节点是不是要删除的
  if (temp->nValue == nDeleteValue)
  {
   //  看是不是头
   if(temp == pHead)
   {
    Pop_Front();
   }
   else if(temp == pEnd)  //  看是不是尾
   {
    Pop_Back();  
   }
   else   //  中间删除的
   {
    temp->pPre->pNext = temp->pNext;
    temp->pNext->pPre = temp->pPre;
    free(temp);
   }
   return;
  }
  //  继续查找
  temp = temp->pNext;
 } while (temp!=pHead);
}
int main()
{
  
  Push_Front(1);
  Push_Front(2);
  Push_Front(3);
  Push_Front(4);
  Push_Front(5);
  Push_Front(6);
 
  Show_Head(pHead);
  printf("\n---------------------\n");
  Show_End(pEnd);
  printf("\n---------------------\n");

  //Push_Back(7);
  //Show_Head(pHead);
  //printf("\n---------------------\n");
  //Show_End(pEnd);
  //printf("\n---------------------\n");

 //Pop_Front();
 //Show_Head(pHead);
 //printf("\n---------------------\n");
 //Show_End(pEnd);
 //printf("\n---------------------\n");

 //Pop_Back();
 //Show_Head(pHead);
 //printf("\n---------------------\n");
 //Show_End(pEnd);
 //printf("\n---------------------\n");
 
 Insert_Value(6,100);
 Show_Head(pHead);
 printf("\n---------------------\n");


 Insert_Value(1,200);
 Show_Head(pHead);
 printf("\n---------------------\n");


 Insert_Value(11,300);
 Show_Head(pHead);
 printf("\n---------------------\n");

 
 Delete_Value(100);
 Show_Head(pHead);
 printf("\n---------------------\n");

 Delete_Value(200);
 Show_Head(pHead);
 printf("\n---------------------\n");

 Delete_Value(300);
 Show_Head(pHead);
 printf("\n---------------------\n");


 system("pause");
 return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值