C++方式封装顺序表、单链表和双向链表

1.顺序表

Seqlist.h


#pragma once

typedef int DataType;

class Seqlist
{
public:
	Seqlist()//构造
		:_array(NULL)
		, _size(0)
		, _capacity(0)
	{}
	Seqlist(const Seqlist& s)//拷贝构造
	{
		_array = (DataType*)malloc(sizeof(DataType)*s._size);
		memcpy(_array, s._array, sizeof(DataType)*s._size);
		_size = _capacity = s._size;
	}
	Seqlist& operator=(const Seqlist& s)//赋值运算符的重载
	{
		if (this != &s)
		{
			free(_array);
			_array = (DataType*)malloc(sizeof(DataType)*s._size);
			memcpy(_array, s._array, sizeof(DataType)*s._size);
		}
		return *this;
	}
	~Seqlist()//析构
	{
		if (_array)
		{
			free(_array);
			_size = _capacity;
			_array = NULL;
		}
	}
	void PushBack(DataType x)//尾插
	{
		CheckCapacity();
		_array[_size++] = x;
	}
	void PopBack()//尾删
	{
		assert(_size > 0);
		--_size;
	}
	void PushFront(DataType x)//头插
	{
		CheckCapacity();
		int end = _size - 1;
		while (end >= 0)
		{
			_array[end + 1] = _array[end];
			--end;
		}
		_array[0] = x;
		++_size;
	}
	void PopFront()//头删
	{
		assert(_size > 0);
		for (size_t i = 1; i < _size; ++i)
		{
			_array[i - 1] = _array[i];
		}
		--_size;
	}
	inline void Insert(size_t pos, DataType x)//指定一个位置插入
	{
		assert(pos <= _size);
		CheckCapacity();
		for (int end = _size - 1; end >= (int)pos; --end)
		{
			_array[end + 1] = _array[end];
		}
		_array[pos] = x;
		++_size;
	}
	inline void Erase(size_t pos)//指定一个位置删除
	{
		assert(pos <= _size);
		for (size_t i = pos + 1; i < _size; ++i)
		{
			_array[i - 1] = _array[i];
		}
		--_size;
	}
	inline DataType& operator[](size_t pos)//[]重载   修改任意位置的数据
	{
		assert(pos < _size);
		return _array[pos];
	}
	void CheckCapacity()//检查容量
	{
		if (_size == _capacity)
		{
			_capacity = _capacity * 2 + 3;
			_array = (DataType*)realloc(_array, _capacity*sizeof(DataType));
		}
	}
	void Print()//打印函数
	{
		for (size_t i = 0; i < _size; ++i)
		{
			cout << _array[i] << " ";
		}
		cout << endl;
	}
private:
	DataType* _array;
	size_t _size;
	size_t _capacity;
};
void TestSeqlist()
{
	Seqlist s;
	s.PushBack(1);
	s.PushBack(2);
	s.PushBack(3);
	s.PushBack(4);
	s.Print();
	s.PopBack();
	s.Print();
	s.PushFront(0);
	s.Print();
	s.PopFront();
	s.Print();
	s.Insert(0, 30);
	s.Print();
	s.Erase(0);
	s.Print();
}

test.cpp


#include 
#include 
using namespace std;

#include "Seqlist.h"

int main()
{
	TestSeqlist();
	system("pause");
	return 0;
}

2.单链表

SList.h


#pragma once

typedef int DataType;

struct SlistNode
{
	SlistNode* _next;
	DataType _data;
	SlistNode(DataType x)
		:_data(x)
		,_next(NULL)
	{}
};
class Slist
{
	typedef SlistNode Node;
public:
	Slist()
	: _head(NULL)
	, _tail(NULL)
	{}
	Slist(const Slist& s);
	Slist& operator=(const Slist& s);
	~Slist()
	{
			Node* cur = _head;
			while (cur)
			{
				Node* next = cur->_next;
				delete cur;
				cur = next;
			}
			_head = _tail = NULL;
		}
	Node* BuyNode(DataType x)
	{
		Node* node = (Node*)malloc(sizeof(Node));
		node->_data = x;
		node->_next = NULL;
		return node;
	}
	void PushBack(DataType x)
	{
		if (_head == NULL)
		{
			_head = _tail = new Node(x);
		}
		else
		{
			_tail->_next = new Node(x);
			_tail = _tail->_next;
		}
	}
	void PopBack()
	{
		if (_head == NULL)
		{
			return;
		}
		else if (_head == _tail)
		{
			delete _head;
			_head = _tail = NULL;
		}
		else
		{
			Node* prev = _head;
			while (prev->_next != _tail)
			{
				prev = prev->_next;
			}
			delete _tail;
			_tail = prev;
			_tail->_next = NULL;
		}
	}
	void PushFront(DataType x)
	{
		if (_head == NULL)
		{
			_head = _tail = new Node(x);
		}
		else
		{
			Node* tmp = new Node(x);
			tmp->_next = _head;
			_head = tmp;
		}
	}
	void PopFront()
	{
		if (_head == NULL)
		{
			return;
		}
		else if (_head == _tail)
		{
			delete _head;
			_head = _tail = NULL;
		}
		else
		{
			Node* del = _head;
			_head = _head->_next;
			delete del;
		}
	}
	void Print()
	{
		Node* cur = _head;
		while(cur)
		{
			cout << cur->_data << " ";
			cur = cur->_next;
		}
		cout << endl;
	}
private:
	Node* _head;
	Node* _tail;
};
void TestSlist()
{
	Slist s;
	s.PushBack(1);
	s.PushBack(2);
	s.PushBack(3);
	s.PushBack(4);
	s.PushBack(5);
	s.Print();
}
test.cpp

#include 
#include 
using namespace std;

#include "SList.h"

int main()
{
	TestSlist();
	system("pause");
	return 0;
}

3.双向链表

List.h


#pragma once

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(const List& s)//拷贝构造
		:_head(NULL)
		,_tail(NULL)
	{
		Node* cur = s._head;
		while (cur)
		{
			PushBack(cur->_data);
			cur = cur->_next;
		}
	}

	List& operator=(const List& s)//赋值运算符的重载
	{
		if (this != &s)
		{
			Node* cur = _head;
			while (cur)
			{
				Node* next = cur->_next;
				delete cur;
				cur = next;
			}
			_head = _tail = NULL;
			Node* tmp = s._head;
			while (tmp)
			{
				PushBack(tmp->_data);
				tmp = tmp->_next;
			}
		}
		return *this;
	}

	~List()//析构
	{
		if (_head)
		{
			Node* cur = _head;
			while (_head)
			{
				cur = _head;
				_head = _head->_next;
				delete cur;
			}
			_head = _tail = NULL;
		}
	}

	void PushBack(DataType x)//尾插
	{
		if (_head == NULL)
		{
			Node* cur = new Node(x);
			cur->_next = NULL;
			cur->_prev = NULL;
			_head = _tail = cur;
		}
		else
		{
			Node* cur = new Node(x);
			_tail->_next = cur;
			cur->_prev = _tail;
			_tail = cur;
		}
	}

	void PopBack()//尾删
	{
		if (_head == NULL)
		{
			return;
		}
		else if (_head->_next == NULL)
		{
			delete _head;
			_head = _tail = NULL;
		}
		else
		{
			Node* cur = _tail;
			_tail = _tail->_prev;
			_tail->_next = NULL;
			delete cur;
		}
	}

	void PushFront(DataType x)//头插
	{
		if (_head == NULL)
		{
			_head = _tail = new Node(x);
		}
		else
		{
			Node* cur = new Node(x);
			cur->_next = _head;
			_head->_prev = cur;
			_head = _head->_prev;
		}
	}

	void PopFront()//头删
	{
		if (_head == NULL)
		{
			return;
		}
		else if (_head->_next == NULL)
		{
			delete _head;
			_head = _tail = NULL;
		}
		else
		{
			Node* cur = _head;
			_head = _head->_next;
			delete cur;
			_head->_prev = NULL;
		}
	}

	Node* Find(DataType x)//找一个数据
	{
		Node* cur = _head;
		while (cur)
		{
			if (cur->_data == x)
				return cur;
			cur = cur->_next;
		}
		return NULL;
	}

	void Insert(Node* pos, DataType x)//在指定位置前插入一个数据
	{
		assert(pos);
		if(pos->_prev == NULL)
		{
			PushFront(x);
		}
		else
		{
			Node* front = pos->_prev;
			Node* last = new Node(x);
			last->_prev = front;
			last->_next = pos;
			front->_next = last;
			pos->_prev = last;
		}
	}

	void Erase(Node* pos)//删除指定位置
	{
		assert(pos);
		if(pos->_prev == NULL)
		{
			PopFront();
		}
		else if (pos->_next == NULL)
		{
			PopBack();
		}
		else
		{
			Node* front = pos->_prev;
			Node* last = pos->_next;
			front->_next = last;
			last->_prev = front;
			delete pos;
		}
	}

	void Print()//打印函数
	{
		Node* cur = _head;
		while (cur)
		{
			cout << cur->_data << " ";
			cur = cur->_next;
		}
		cout << endl;
	}

private:
	Node* _head;
	Node* _tail;
};

void TestList()
{
	List s;
	s.PushBack(1);
	s.PushBack(2);
	s.PushBack(3);
	s.PushBack(4);
	s.Print();
	s.PopBack();
	s.Print();
	s.PushFront(0);
	s.Print();
	s.PopFront();
	s.Print();
	s.Insert(s.Find(1), 0);
	s.Print();
	s.Erase(s.Find(0));
	s.Print();
	List s1(s);
	s1.Print();
	List s2;
	s2 = s1;
	s2.Print();
	s2 = s2;
	s2.Print();
}
test.cpp

#include 
#include 
using namespace std;

#include "List.h"

int main()
{
	TestList();
	system("pause");
	return 0;
}

你可能感兴趣的:(C++方式封装顺序表、单链表和双向链表)