C++实现顺序表、单链表、双链表

1.顺序表:

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
typedef int DataType;
class Seqlist
{
public:
	Seqlist()
		:_array(NULL)
		,_size(0)
		,_capatify(0)
	{
	}
	Seqlist(const Seqlist& s)
	{
		_array=(DataType*)malloc(s._size*sizeof(DataType));
		memcpy(_array,s._array,s._size*sizeof(DataType));
		_size=_capatify=s._size;
	}
	Seqlist operator=(const Seqlist& s)
	{
		free(_array);
		Swap(s);
		return *this;
	}
	~Seqlist()
	{
		if(_array)
		{
			free(_array);
			_array=NULL;
			_size=_capatify=0;
		}
	}
	void Swap(const Seqlist& s)
	{
		_array=s._array;
		_size=s._size;
		_capatify=s._capatify;
	}
	void PushBack(DataType x)//尾插
	{
		//1.没有节点时
		//2.有一个节点和多个节点
		//1、2是一样的方法
		CheckCapatify();
		_array[_size]=x;
		_size++;
	}
	void PushFront(DataType x)//头插
	{
		//1.没有节点时
		//2.有一个或者多个节点
		if(_size==0)
		{
			CheckCapatify();
			_array[_size]=x;
		}
		else
		{
			CheckCapatify();
			int end=(int)_size-1;
			int i = 0;
			for(i=end;i>=0;i--)
			{
				_array[i+1]=_array[i];
			}
			_array[0]=x;
		}
		_size++;
	}
	void CheckCapatify()//检查是否需要增容
	{
		if(_size==_capatify)
		{
			_capatify=2*_capatify+3;
			_array=(DataType*)realloc(_array,_capatify*(sizeof(DataType)));
			assert(_array);//检查增容是否失败
		}
	}
	void Insert(size_t pos,DataType x)//定点插入
	{
		assert(pos<=_size);
		CheckCapatify();
		if(pos==0)
			PushFront(x);
		else if(pos==_size)
			PushBack(x);
		else
		{
			int end=(int)_size-1;
			CheckCapatify();
			while(end!=pos-1)
			{
				_array[end+1]=_array[end];
				end--;
			}
			_array[pos]=x;
		}
		_size++;
	}
	void PopBack()//尾删
	{
		//1.没有节点
		//2.有节点
		if(_size==0)
		{
			return;
		}
		else
		{
			_array[_size]=NULL;
			_size--;
		}
	}
	void PopFront()//头删
	{
		//1.没有节点
		//2.有节点
		if(_size==0)
		{
			return;
		}
		else
		{
			int end=(int)_size-1;
			int i = 0;
			for(;i<end;i++)
			{
				_array[i]=_array[i+1];
			}
			_size--;
		}
	}	
	void Earse(size_t pos)//定点删除
	{
		//1.pos==0;
		//2.pos==size_t;
		//3.pos<size_t;
		assert(pos<_size&&(pos>=0));
		if(pos==0)
			PopFront();
		else if(pos==_size)
			PopBack();
		else
		{
			int end=(int)_size-1;
			int i = 0;
			for(i=pos;i<end;i++)
			{
				_array[i]=_array[i+1];
			}
			_size--;
		}
	}

	void print()
	{
		size_t i= 0;
		for(i=0;i < _size;i++)
		{
			cout<<_array[i]<<" ";
		}
		cout<<endl;
	}
private:
	DataType* _array;
	size_t _size;
	size_t _capatify;
};
void test1()//测试
{
	Seqlist s;
	s.PushBack(1);
	s.PushBack(2);
	s.PushBack(3);
	s.PushBack(5);
	s.print();
	s.PushFront(0);
	s.print();
	s.Insert(4,4);
	s.print();
	s.PopBack();
	s.print();
	s.PopFront();
	s.print();
	s.Earse(3);
	s.print();
}
int main()
{
	test1();
	return 0;
}

2.单链表:

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
typedef int DataType;

struct SlistNode
{
	SlistNode* _next;
	DataType _data;
	SlistNode(DataType x)
		:_next(NULL)
		,_data(x)
	{
	}
};
class Slist
{
	typedef SlistNode Node;
public:
	Slist()
		:_head(NULL)
		,_tail(NULL)
	{
	}
	Slist(const Slist& s)
		:_head(NULL)
		,_tail(NULL)
	{
		Node* cur=(Node*)s._head;
		while(cur!=NULL)
		{
			PushBack(cur->_data);
			cur=cur->_next;
		}
	}
	~Slist()
	{
		Node* cur=_head;
		while(cur!=NULL)
		{
			Node* temp=cur;
			cur=cur->_next;
			delete  temp;
		}
		_head=_tail=NULL;
	}
	Slist& operator=(const Slist& s)
	{
		Node* cur=(Node*)s._head;
		while(cur!=NULL)
		{
			PushBack(cur->_data);
			cur=cur->_next;
		}
		return *this;
	}
	void PushBack(DataType x)//尾插
	{
		//1.没有节点
		//2.有节点
		if((_head==NULL)&&(_tail==NULL))
		{
			_tail=_head=new Node(x);
		}
		else
		{
			_tail->_next=new Node(x);
			_tail=_tail->_next;
		}
	}
	void PushFront(DataType x)//头插
	{
		//1.没有节点
		//2.有节点
		if(_head==NULL)
		{
			_head=_tail=new Node(x);
		}
		else
		{
			Node* temp= new Node(x);
			temp->_next=_head;
			_head=temp;
		}
	}
	Node* Find(DataType x,const Slist& s)//查找节点
	{
		Node* cur=s._head;
		while(cur)
		{
			if(cur->_data==x)
				return cur;
			cur=cur->_next;
		}
		return NULL;
	}
	void Insert(Node* pos,DataType x)//在pos前插一个节点
	{
		assert(pos);
		if(pos==_head)
			PushFront(x);
		else
		{
			Node* cur = _head;
			while(cur->_next!=pos)
			{
				cur=cur->_next;
			}
			Node* temp=new Node(x);
			temp->_next=cur->_next;
			cur->_next=temp;
		}
	}
	void PopBack()//尾删
	{
		//1.没有节点
		//2.有1个节点
		//3.有多个节点
		if(_head==NULL)
		{
			return;
		}
		else if(_head->_next==NULL)
		{
			delete _head;
			_head=NULL;
			_tail=_head;
		}
		else
		{
			Node* cur=_head;
			while(cur->_next->_next!=NULL)
			{
				cur=cur->_next;
			}
			delete cur->_next;
			cur->_next=NULL;
			_tail=cur;
		}
	}
	void PopFront()//头删
	{
		//1.没有节点
		//2.有1个节点
		//3.有多个节点
		if(_head==NULL)
		{
			return;
		}
		else
		{
			Node* cur=_head;
			_head=_head->_next;
			delete cur;
		}
	}
	void Earse(Node* pos)//删除pos位置的节点
	{
		//1.pos==_head
		//2.pos==_tail;
		//3.pos在_head和_tail之间
		assert(pos);
		if(pos==_head)
			PopFront();
		if(pos==_tail)
			PopBack();
		else
		{
			Node* cur=_head;
			while(cur->_next!=pos)
			{
				cur=cur->_next;
			}
			cur->_next=cur->_next->_next;
			delete pos;
		}
	}
	void print()
	{
		if(_head==0)
		{
			cout<<"NULL"<<endl;
		}
		else
		{
			Node* cur=_head;
			while(cur)
			{
				cout<<cur->_data<<" ";
				cur=cur->_next;
			}
			cout<<endl;
		}
	}
private:
	Node* _head;
	Node* _tail;
};
void test1()//测试
{
	Slist s,s1;
	SlistNode* temp=NULL;
	s.PushBack(1);
	s.PushBack(2);
	s.PushBack(3);
	s.print();
	s.PushFront(0);
	s.print();
	temp=s.Find(0,s);
	s.Insert(temp,-1);
	s.print();
	s.PopBack();
	s.print();
	s1.print();
	s.PopFront();
	s.print();
	temp=s.Find(1,s);
	s.Earse(temp);
	s.print();
	Slist s2(s);
	s2.print();
	Slist s3;
	s3=s2;
	s3.print();
}
int main()
{
	test1();
	return 0;
}

3.双链表:

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
typedef int DataType;
struct ListNode
{
	ListNode* _next;
	ListNode* _prev;
	DataType _data;
	ListNode(DataType x)
		:_next(NULL)
		,_prev(NULL)
		,_data(x)
	{
	}
};
class List
{
	typedef ListNode Node;
public:
	List()//构造函数
		:_head(NULL)
		,_tail(NULL)
	{
	}
	List(DataType x)//带参构造函数
		:_head(NULL)
		,_tail(NULL)
	{
		_head=new Node(x);
	}
	List(const List& l)//拷贝构造
		:_head(NULL)
		,_tail(NULL)
	{
		Node* cur=l._head;
		if(l._head!=NULL)
		{
			while(cur)
			{
				PushBack(cur->_data);
				cur=cur->_next;
			}
		}
	}
	List& operator=(const List& l)
	{
		if(this->_head!=l._head)
		{
			List temp(l);
			swap(_head,temp._head);
			swap(_tail,temp._tail);
		}
		return *this;
	}
			
	~List()//析构函数
	{
		if(_head!=NULL)
		{
			Node* cur=_head;
			delete _head;
			_head=cur;
		}
		_tail=_head=NULL;
	}
	void PushBack(DataType x)//尾插
	{
		//1.没有节点
		//2.有一个节点
		//3.有多个节点
		if(_head==NULL)
		{
			_head=_tail=new Node(x);
		}
		else if(_head->_next==NULL)
		{
			_head->_next=new Node(x);
			_tail=_head->_next;
			_tail->_prev=_head;
		}
		else
		{
			Node* cur=_tail;
			_tail->_next=new Node(x);
			_tail=_tail->_next;
			_tail->_prev=cur;
		}
	}	
	void PopBack()//尾删
	{
		//1.没有节点
		//2.有1个节点
		//3.有多个节点
		if(_head==NULL)
			return;
		else if(_head->_next==NULL)
		{
			delete _head;
			_tail=_head=NULL;
		}
		else
		{
			Node* temp=_tail;
			_tail=_tail->_prev;
			_tail->_next=NULL;
			delete temp;
		}	
	}
	void PushFront(DataType x)//头插
	{
		//1.没有节点
		//2.有1个节点和多个节点
		if(_head==NULL)
			_head=_tail=new Node(x);
		else
		{
			Node* cur=_head;
			_head=new Node(x);
			_head->_next=cur;
		}
	}		
	void PopFront()//头删
	{
		//1.没有节点
		//2.有一个节点
		//3.有多个节点
		if(_head==NULL)
			return;
		else if(_head->_next==NULL)
		{
			delete _head;
			_head=_tail=NULL;
		}
		else
		{
			Node* cur = _head;
			_head=_head->_next;
			delete cur;
		}
	}
	Node* Find(DataType x,List& l)//查找位置
	{
		Node* cur=l._head;
		while(cur)
		{
			if(cur->_data==x)
				return cur;
			else
				cur=cur->_next;
		}
		return NULL;
	}
	void Insert(Node* pos,DataType x)//在pos前插入一个节点
	{
		//1.头插
		//2.其余位置插
		if(pos==_head)
			PushFront(x);
		else
		{
			Node* temp=new Node(x);
			pos->_prev->_next=temp;
			temp->_prev=pos->_prev;
			temp->_next=pos;
			pos->_prev=temp;
		}
	}
	void Earse(Node* pos)
	{
		//1.头删
		//2.尾删
		//3.其余位置删
		if(pos==_head||pos->_prev==NULL)
			PopFront();
		else if(pos==_tail||pos->_next==NULL)
			PopBack();
		else
		{
			Node* front = pos->_prev;
			Node* back = pos->_next;
			front->_next=back;
			back->_prev=front;
		}
	}
	void Reverse()
	{
		//1.没有节点
		//2.有1个节点
		//3.有多个节点
		Node* cur = _head;
		if(_head==NULL||_head->_next==NULL)
			return;
		else
		{
			while(cur)
			{
				Node* temp=cur->_next;
				cur->_next=cur->_prev;
				cur->_prev=temp;
				cur=cur->_prev;
			}
			Node* temp=_head;
			_head=_tail;
			_tail=_head;
		}
	}
		
	void print()//打印
	{
		Node* cur=_head;
		while(cur)
		{
			cout<<cur->_data<<"->";
			cur=cur->_next;
		}
		cout<<"NULL"<<endl;
	}
private:
	Node* _head;
	Node* _tail;
};



猜你喜欢

转载自blog.csdn.net/fangxiaxin/article/details/75462244