c++实现单链表

#define  _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <assert.h>
using namespace std;
typedef int dataType;
struct ListNode
{
    dataType _data;
	ListNode* _next;
    
	ListNode(const dataType x)
		:_data(x)
		,_next(NULL)
	{ }
};	
class List
{
     typedef ListNode Node;
public:
	List()
		:_head(NULL)
		,_tail(NULL)
	{}
	List(const List& s)
		:_head(NULL),_tail(NULL)
	{
		if (s._head == NULL)
		{
			return;
		}
		else
		{
			Node* cur = s._head;
			while (cur)
			{
				PushBack(cur->_data);
				cur = cur->_next;
			}
		}
	}     
void PushBack(dataType x)
{
   if(_head==NULL)
   {
       _head=_tail=new Node(x);
   }
   else
   {
       Node* tmp=new Node(x);
	   _tail->_next=tmp;
	  
	   _tail=_tail->_next;
   }
}
void PushFront(dataType x)
{
	if(_head==NULL)
	{
		_head=_tail=new Node(x);
	}
	else
	{
		Node* tmp=_head;
		_head=new Node(x);
		 _head->_next=tmp;
	}
}
void PopBack()
{
    if (_tail==NULL)
    {
     return;
    }
	else if (_head->_next==NULL)
	{
        delete _tail;
		_tail=NULL;
		_head=NULL;
	}
	else
	{
        Node* cur=_head;
		while(cur->_next!=_tail)
		{
			cur=cur->_next;
    		}
	   delete _tail;
	   _tail=cur;
	   _tail->_next=NULL;
	  
	} 
}
void PopFront()
{
	if (_head==NULL)
	{
		return;
	}
	else if (_head->_next==NULL)
	{
		delete _tail;
		_tail=NULL;
		_head=NULL;		
	}
	else
	{
		Node* del=_head;
           _head=_head->_next;
		delete del;
	} 
}
Node* Find(dataType x)
{     
	Node* cur=_head;
	while(cur)
	{
      if (cur->_data==x)
      {
		   return cur;
      }
	  	 cur=cur->_next;	
	}
}
void Insert(Node* pos,dataType x)
{
	assert(pos);
	if (pos == _tail)
	{
		PushBack(x);
	}
	else if (pos==_head)
	{
       PushFront(x);
	}
	else
	{
		Node* cur = _head;
		while (cur->_next != pos)
		{
			cur = cur->_next;
		}
		cur->_next = new Node(x);
		cur->_next ->_next = pos;
	}
} 
void Erase(Node* pos)
{
    assert(pos);
	if (pos==_head)
	{
		PopFront();
	}
	else if(pos==_tail)
	{
	   PopBack();
	}
	else
	{
		Node* cur = _head;
					while (cur->_next != pos)
					{
						cur = cur->_next;
					}
					cur->_next = pos->_next;
					delete pos;
	}
}
//赋值现代写法 
void Swap(List& s)
 	{
 	    swap(_head, s._head);
 		swap(_tail, s._tail);
 }
List& operator=(const List& L1)
{
	if (this!=&L1)
	{
		 List tmp(L1);
		 Swap(tmp);
	}
	return *this;
} 
//链表的逆置
void Reverse()
{
	if (_head==NULL||_head->_next==NULL)
	{
	    return;
	}
	else  
	{
		Node* cur = _head;
				Node* pre = _head;
				Node* del = _head->_next;
				while (cur->_next)
				{
					PushFront(cur->_next->_data);
					cur = cur->_next;
				}
				while (del)
				{
					Node* pdel = del;
					del = del->_next;
					delete pdel;
				}
				_tail = pre;
				_tail->_next = NULL;
			}
	}
void Print()
{
	Node* cur=_head;
	while (cur)
	{
		cout<<cur->_data<<"   ";
		cur=cur->_next;
	}
  cout<<endl;
}
private:
	Node* _head;
	Node* _tail;
};


#include"List.h"
void TestList()
{
	List  list1;
	List  list2;
	//PushBack测试
	/*list1.PushBack(1);
     list1.PushBack(2);
	 list1.PushBack(3);
	 list1.PushBack(4);
	 list1.PushBack(5);
	list1.Print();*/
     //PopBack测试
	//list1.PushBack(1);
	//list1.PushBack(2);
	//list1.PushBack(3);
	//list1.PushBack(4);
	//list1.PushBack(5);
	//list1.PopBack();
 //  list1.PopBack();
	//list1.PopBack();
	//list1.PopBack();
	//list1.PopBack();
	//list1.PopBack();
 //   list1.Print();

   //PushFront测试

	//list1.PushFront(1);
 //    list1.PushFront(2);
	// list1.PushFront(3);
	// list1.PushFront(4);
	// list1.PushFront(5);
	//list1.Print();

	//PopFront()测试
//	list1.PushBack(1);
//     list1.PushBack(2);
//	 list1.PushBack(3);
//	 list1.PushBack(4);
//	 list1.PushBack(5);
//	//list1.PopFront();
//	//list1.PopFront();
//	//list1.PopFront();
//	list1.PopFront();
//	list1.PopFront();
//	list1.PopFront();
//list1.Print();	 

//Insert测试
	list1.PushBack(1);
 
list1.PushBack(2);
list1.PushBack(3);
//list1.PushBack(4);
//	 
	list1.Insert(list1.Find(2),2);
	list1.Insert(list1.Find(3),3);
	list1.Insert(list1.Find(3),4);
	list1.Print();
	list2=list1;
   list1.Print();

//Erase测试

	list1.Erase(list1.Find(1));
	list1.Erase(list1.Find(4));
//	/*list1.Erase(list1.Find(2));*/
// //   list1.Erase(list1.Find(3));
//	//list1.Erase(list1.Find(4));
//		list1.Erase(list1.Find(4));
  list1.Print();
   //Reverse测试
  list1.Reverse();
  list1.Print();
}
int main()
{
	TestList();
	return 0;
}
 	 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值