双向链表的创建和删除

 

#include"iostream.h"
#include"string.h"

struct Node
{
 int date;
 Node* pfront;
 Node* rear;
};

Node* create();
Node* del(Node* head, int num);
//Node* inser(Node* head, Node* ptu);
void print(Node*head);
void reverprint(Node*head);

void main()
{
 Node* p, *pel;
 p = create();
 cout<<"输出链表:"<<endl;
 print(p);
 cout<<endl<<"逆序输出:"<<endl;
 reverprint(p);

 pel = del(p,5);
 cout<<"删除结点后的链表输出:"<<endl;
 print(pel);
 cout<<endl;
}

Node* create()
{
 int data;
 Node* head, *p,*pstu;
 head = new Node;
 head->pfront = NULL;
 head->rear = NULL;
 p = head;
 cin>>data;
 while(data)
 {
  pstu = new Node;
  pstu->date  = data;
  p->pfront = pstu;
  pstu->rear = p;
  p = p->pfront ;
  cin>>data;
 }
 p->pfront = NULL;
 return head;
}

void print(Node*head)
{
 Node* p = head ->pfront;
 while(p != NULL)
 {
  cout<<p->date<<" ";
  p = p->pfront ;
 }
}

void reverprint(Node*head)
{
 Node* p = head;
 while(p->pfront != NULL)
 {
  p = p->pfront ;
 }
 while(p != NULL && p != head)
 {
  cout<<p->date<<" ";
  p = p ->rear ;
 }
 cout<<endl;
}

Node* del(Node* head, int num)
{
 Node* pt = head->pfront ;
 if(head == NULL || head->pfront == NULL)
 {
  return head;
 }
 while(pt->date != num && pt->pfront!= NULL)
 {
  pt = pt->pfront ;
 }
 if(pt->date == num)
 {
  if(pt == head)
  {
   head = pt->pfront ;
  }
  else if(pt->pfront != NULL)
  {

   pt->rear->pfront = pt->pfront ;
   pt->pfront ->rear = pt->rear ;
  }
  else
  {
   pt->rear->pfront = pt->pfront ;
  }

  delete pt;
  pt = NULL;

 }
 else
 {
  cout<<"not found"<<endl;
 }
 return head;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
双向链表的插入和删除操作相对于单链表来说,要稍微复杂一些。因为双向链表中的每个节点都同时具有前驱和后继指针,所以我们需要考虑这两个指针的变化。 下面分别介绍双向链表的插入和删除操作。 ### 双向链表的插入操作 双向链表的插入操作分为两种情况:在链表头部插入和在链表尾部插入。 #### 在链表头部插入 在链表头部插入一个新节点的步骤如下: 1. 创建一个新节点,将其 next 指向当前头节点; 2. 将当前头节点的 prev 指针指向新节点; 3. 将新节点设置为新的头节点。 代码实现如下: ``` void insert_front(Node* &head, int data) { Node* new_node = new Node(data); new_node->next = head; if (head != NULL) { head->prev = new_node; } head = new_node; } ``` #### 在链表尾部插入 在链表尾部插入一个新节点的步骤如下: 1. 创建新节点; 2. 将当前尾节点的 next 指针指向新节点; 3. 将新节点的 prev 指针指向当前尾节点; 4. 将新节点设置为新的尾节点。 代码实现如下: ``` void insert_back(Node* &head, int data) { Node* new_node = new Node(data); if (head == NULL) { head = new_node; } else { Node* tail = head; while (tail->next != NULL) { tail = tail->next; } tail->next = new_node; new_node->prev = tail; } } ``` ### 双向链表删除操作 双向链表删除操作同样分为两种情况:删除头节点和删除尾节点。 #### 删除头节点 删除头节点的步骤如下: 1. 将头节点的 next 指针指向新的头节点; 2. 如果新的头节点不为空,则将其 prev 指针设置为 NULL; 3. 释放原来的头节点。 代码实现如下: ``` void delete_front(Node* &head) { if (head == NULL) { return; } Node* old_head = head; head = head->next; if (head != NULL) { head->prev = NULL; } delete old_head; } ``` #### 删除尾节点 删除尾节点的步骤如下: 1. 找到当前尾节点的前一个节点; 2. 将该节点的 next 指针设置为 NULL; 3. 释放原来的尾节点。 代码实现如下: ``` void delete_back(Node* &head) { if (head == NULL) { return; } if (head->next == NULL) { delete head; head = NULL; return; } Node* tail = head; while (tail->next != NULL) { tail = tail->next; } tail->prev->next = NULL; delete tail; } ``` 以上就是双向链表的插入和删除操作的实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值