c++:实现(list)带头结点的双向链表

Vector与list的区别:

Vector代码:https://blog.csdn.net/W_J_F_/article/details/82669412

1.vector数据结构
vector和数组类似,拥有一段连续的内存空间,并且起始地址不变。
因此能高效的进行随机存取,时间复杂度为o(1);
但因为内存空间是连续的,所以在进行插入和删除操作时,会造成内存块的拷贝,时间复杂度为o(n)。
另外,当数组中内存空间不够时,会重新申请一块内存空间并进行内存拷贝。

2.list数据结构
list是由双向链表实现的,因此内存空间是不连续的。
只能通过指针访问数据,所以list的随机存取非常没有效率,时间复杂度为o(n);
但由于链表的特点,能高效地进行插入和删除。

头文件:

#ifndef __LIST_H__
#define __LIST_H__

#include<iostream>

using namespace std;

typedef int DataType;

struct ListNode
{
	ListNode* _next;
	ListNode* _prev;
	DataType _data;

	ListNode(DataType x)
		:_data(x)
		, _next(NULL)
		, _prev(NULL)
	{}
};
class List
{
	typedef ListNode Node;
public:
	List();                         //构造函数
	List(const List& l);            //拷贝构造函数
	List& operator=(const List& l); //赋值运算符重载
	~List();                        //析构函数

	void Swap(List& l);             //交换
	void show();                    //打印
	void PushBack(DataType x);      //尾插
	void PushFront(DataType x);     //头插
	void PopBack();                 //尾删 
	void PopFront();                //头删
	Node* Find(DataType x);         //查找
	void Insert(Node* pos, DataType x);//任意位置插入
	void Erase(Node* pos);          // 任意位置删除
private:
	Node * _head;
};
#endif

list.h

#include"List.h"

List::List()
:_head(new Node(DataType()))
{
	_head->_next = _head;
	_head->_prev = _head;
}

List::List(const List& l)
{
	if(l._head == NULL)
	{
	return;
	}
	_head = new Node(DataType());
	_head->_next = _head;
	_head->_prev = _head;

	Node* cur = l._head->_next;
	while(cur != l._head)
	{
		PushBack(cur->_data);
		cur = cur->_next;
	}
}

List& List::operator=(const List& l)
{
  if(this != &l)
  {
  List tmp(l);
  Swap(tmp);
  }
  return *this;
}

List::~List()
{
	if(_head == NULL)
	{
	return;
	}
	Node* del = _head;
	_head->_prev->_next = NULL;

    Node* tmp = del;
	while(del != NULL)
	{
		tmp = del->_next;
		delete del;
		del = tmp;
	}
}

void List::PushBack(DataType x)
{
    if(NULL == _head)
	{
	return ;
	}

    Node* newNode = new Node(x);
	Node* tail = _head->_prev;//标记尾节点

	newNode->_prev = tail;
	newNode->_next = _head;
	_head->_prev = newNode;
	tail->_next = newNode;

}

void List::PopBack()
{
	if(_head == NULL || _head->_next == NULL)
	{
	return;
	}

	Node* del = _head->_prev;
	_head->_prev = del->_prev;
	del->_prev->_next = _head;

	delete del;
}

void List::PushFront(DataType x)
{
   Node* newNode = new Node(x);

   newNode->_prev = _head;
   newNode->_next = _head->_next;
   _head->_next->_prev = newNode;
   _head->_next = newNode;
}

void List::PopFront()
{
	if(_head == NULL || _head->_next == NULL)
	{
	return;
	}

	Node* del = _head->_next;
	_head->_next = del->_next;
	del->_next->_prev = _head;

	delete del;
}

void List::Insert(Node* pos ,DataType x)
{
	if(pos == _head->_next)
	{
	PushFront(x);
	}
	if(pos == _head->_prev)
	{
	PushBack(x);
	}
	else
	{
	Node* newNode = new Node(x);

	newNode->_prev = pos->_prev;
	newNode->_next = pos;
	pos->_prev->_next = newNode;
	pos->_prev = newNode;

	}
}

void List::Erase(Node* pos)
{
  if(pos == _head)
  {
  return ;
  }
  else if(pos == _head->_next)
  {
  PopFront();
  }
  else if(pos == _head->_prev)
  {
  PopBack();
  }
  else
  {
	  pos->_prev->_next = pos->_next;
	  pos->_next->_prev = pos->_prev;

	  delete pos;

	  pos->_next = NULL;
	  pos->_prev = NULL;

  }
}
List::Node* List::Find(DataType x)
{
	if(_head == NULL || _head->_next == NULL)
	{
	return NULL;
	}

	Node* cur = _head->_next;
	while(cur != _head)
	{
		if(cur->_data == x)
		{
		return cur;
		}
		cur = cur->_next;
	}
	return NULL;
}
void List::Swap(List& l)
{
   swap(_head,l._head);
}



void List::show()
{
	if(_head->_next == _head)
	{
	cout<<"the list is empty"<<endl;
	return;
	}
	else
	{
		Node* cur = _head->_next;
		while(cur != _head)
		{
			cout<<cur->_data<<"-";
			cur = cur->_next;
		}
        cout<<"NULL"<<endl;
	}
}

void test()
{ 
   List t;
   t.PushBack(1);
   t.PushBack(2);
   t.PushBack(4);
   t.PushBack(5);
   t.show();
   t.Insert(t.Find(4),3);
   t.show();
   t.Erase(t.Find(2));
   t.show();
   t.PushFront(1);
   t.PopBack();
   t.show();

}

int main()
{
   test();
   return 0;
}

猜你喜欢

转载自blog.csdn.net/W_J_F_/article/details/82695343