C++实现双向链表

本文介绍了一种双向链表的数据结构实现,并演示了如何进行基本操作如插入、删除等。通过具体的代码示例,展示了双向链表的构造、拷贝构造、赋值运算符重载、插入节点、删除节点等功能。

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

#pragma once
typedef int DataType;
struct ListNode
{
 ListNode* _next;
 ListNode* _prev;
 DataType _data;
 ListNode(const DataType& x)
  :_data(x)
  ,_next(NULL)
  ,_prev(NULL)
 {}
};
class List
{
 typedef ListNode Node;
public:
 List()
  :_head(NULL)
  ,_tail(NULL)
 {}
 List(const List& l)
  :_head(NULL)
  ,_tail(NULL)
 {
  Node* cur = l._head;
  while (cur)
  {
   PushBack(cur->_data);
   cur = cur->_next;
  }
 }
 List& operator=(List l)
 {
  swap(_head, l._head);
  swap(_tail, l._tail);
 }
 void Clear()
 {
  Node* cur = _head;
  while (cur)
  {
   Node* next = cur->_next;
   delete cur;
   cur = next;
  }
  _head = _tail = NULL;
 }
 ~List()
 {
  Clear();
 }
 void PushBack(const DataType& x)
 {
  if (_tail == NULL)
  {
   _head = _tail = new Node(x);
  }
  else
  {
   Node* tmp = new Node(x);
   _tail->_next = tmp;
   tmp->_prev = _tail;
   _tail = tmp;
  }
 }
 void PopBack()
 {
  Erase(_tail);
 }
 void PushFront(const DataType& x)
 {
  Insert(_head, x);
 }
 void PopFront()
 {
  Erase(_head);
 }
 void Insert(Node* pos, const DataType& x)
 {
  Node* tmp = new Node(x);
  if (_head == pos)
  {
   if(_head == NULL)
   {
    _head = _tail = tmp;
   }
   else
   {
    tmp->_next = _head;
    _head->_prev = tmp;
    _head = tmp;
   }
  }
  else
  {
   Node* prev = pos->_prev;
   prev->_next = tmp;
   tmp->_prev = prev;
   tmp->_next = pos;
   pos->_prev = tmp;
  }
 }
 void Erase(Node* pos)
 {
  if (_head == _tail)
  {
   assert(_head == pos);
   _head = _tail = NULL;
  }
  else if (pos == _head)
  {
   _head = _head->_next;
   _head->_prev = NULL;
  }
  else if (pos == _tail)
  {
   _tail = _tail->_prev;
   _tail->_next = NULL;
  }
  else
  {
   Node* prev = pos->_prev;
   Node* next = pos->_next;
   prev->_next = next;
   next->_prev = prev;
  }
  delete pos;
 }
 Node* Find(const DataType& x)
 {
  Node* cur = _head;
  while (cur)
  {
   if (cur->_data == x)
   {
    return cur;
   }
   cur = cur->_next;
  }
  return NULL;
 }
 void Print()
 {
  Node* cur = _head;
  while (cur)
  {
   cout<<cur->_data<<" ";
   cur = cur->_next;
  }
  cout<<endl;
 }
 void Reverse()
 {
  Node* cur = _head;
  while(cur)
  {
   swap(cur->_next, cur->_prev);
   cur = cur->_prev;
  }
  swap(_head, _tail);
 }
private:
 Node* _head;
 Node* _tail;
};
void TestList()
{
 List l1;
 l1.PushBack(1);
 l1.PushBack(2);
 l1.PushBack(3);
 l1.PushBack(4);
 l1.Print();
 ListNode* pos = l1.Find(3);
 l1.Insert(pos, 30);
 pos = l1.Find(3);
 l1.Erase(pos);
 l1.Print();
 l1.PopBack();
 l1.PopBack();
 l1.PopBack();
 l1.PopBack();
 l1.Print();
}

void TestList1()
{
 List l1;
 l1.PushBack(1);
 l1.PushBack(2);
 l1.PushBack(3);
 l1.PushBack(4);
 l1.Print();
 l1.Reverse();
 l1.Print();
 List l2(l1);
 l2.Print();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值