双向链表

本文详细介绍了链表的基本操作,包括插入、删除、搜索等,并通过实例展示了如何使用链表进行数据结构的构建与应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>

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


void Push_Front(Node** pHead,Node** pEnd,int nValue)
{
 //  申请一个节点
 Node* temp = (Node*)malloc(sizeof(Node));
 temp->nValue = nValue;
 temp->pNext = NULL;
 temp->pPre = NULL;

 //  把这个节点放到链表 头
 if ((*pHead) == NULL)
 {
  (*pHead) = temp;
  (*pEnd) = temp;
 }
 else
 {
  temp->pNext = (*pHead);
  (*pHead)->pPre = temp;
  //   新来的变成头
  (*pHead) = temp;
 }
 //  头尾连接
 (*pHead)->pPre = (*pEnd);
 (*pEnd)->pNext = (*pHead);
}


void Push_Back(Node** pHead,Node** pEnd,int nValue)
{
 //  申请一个节点
 Node* temp = (Node*)malloc(sizeof(Node));
 temp->nValue = nValue;
 temp->pNext = NULL;
 temp->pPre = NULL;

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


void Pop_Front(Node** pHead,Node** pEnd)
{
 if (*pHead == NULL)    //  没有节点就结束
 {
  return;
 }

 if ((*pHead) == (*pEnd))                 //  有一个节点
 {
  free(*pHead);
  (*pHead) = NULL;
  (*pEnd) = NULL;
  return;
 }

 //   有多个节点
 //   记住要删除的这个头
 Node* pDel = (*pHead);
 (*pHead) = (*pHead)->pNext;
 free(pDel);
 pDel = NULL;
 //   头尾连接
 (*pHead)->pPre = (*pEnd);
 (*pEnd)->pNext = (*pHead);
}


void Pop_Back(Node** pHead,Node** pEnd)
{
 if (*pHead == NULL)    //  没有节点就结束
 {
  return;
 }

 if ((*pHead) == (*pEnd))                 //  有一个节点
 {
  free(*pHead);
  (*pHead) = NULL;
  (*pEnd) = NULL;
  return;
 }

 //   有多个节点
 //   记住要删除的这个头
 Node* pDel = (*pEnd);
 (*pEnd) = (*pEnd)->pPre;
 free(pDel);
 pDel = NULL;
 //   头尾连接
 (*pHead)->pPre = (*pEnd);
 (*pEnd)->pNext = (*pHead);
}

 

void Insert(Node** pHead,Node** pEnd,int nFindValue,int nInsertValue)
{
 if (pHead == NULL)
 {
  return;
 }

 Node* temp = *pHead;
 do
 {
  //  看这个节点  是不是要查找值
  if (temp->nValue == nFindValue)
  {
   // 看这个节点是不是  头
   if (temp == (*pHead))
   {
    Push_Front(pHead,pEnd,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));
}

void Delete(Node** pHead,Node** pEnd,int nDeleteValue)
{
 if (pHead == NULL)
 {
  return;
 }

 Node* temp = (*pHead);
 do
 {
  if (temp->nValue == nDeleteValue)
  {
   // 看这个节点 是头还是中间 还是尾
   if (temp == (*pHead))
   {
    Pop_Front(pHead,pEnd);
   }
   else if(temp == (*pEnd))
   {
    Pop_Back(pHead,pEnd);
   }
   else
   {
    temp->pNext->pPre = temp->pPre;
    temp->pPre->pNext = temp->pNext;
    free(temp);
    temp = NULL;
   }
   return;
  }
  temp = temp->pNext;
 } while (temp != (*pHead));
}


void Show_Head(Node* pHead)

 if (pHead == NULL)
 {
  return;
 }

 Node* temp = pHead;
 do
 {
  printf("%d ",temp->nValue);
  temp = temp->pNext;
 } while (temp != pHead);
}
void Show_End(Node* pEnd)
{
 if (pEnd == NULL)
 {
  return;
 }

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


int main()
{

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

 Push_Back(&pHead,&pEnd,1);


 Show_Head(pHead);
 printf("\n-------------------------------------------\n");
 Show_End(pEnd);
 printf("\n-------------------------------------------\n");
 
 Pop_Back(&pHead,&pEnd);
 Pop_Front(&pHead,&pEnd);
 Show_Head(pHead);
 printf("\n-------------------------------------------\n");
 Show_End(pEnd);
 printf("\n-------------------------------------------\n");

 Push_Back(&pHead,&pEnd,2);
 Push_Front(&pHead,&pEnd,3);

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

  
 Insert(&pHead,&pEnd,3,100);
 Show_Head(pHead);
 printf("\n-------------------------------------------\n");
 Show_End(pEnd);
 printf("\n-------------------------------------------\n");


 Insert(&pHead,&pEnd,3,200);

 

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


 Delete(&pHead,&pEnd,200);

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

 

 system("pause");
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值