更多C++知识:c++目录索引
顺序表:
Vector.h
#pragma once
typedef int DataType;
class Vector
{
public:
Vector();//构造
Vector( const Vector& v);//拷贝构造
Vector& operator=(const Vector& v);//赋值运算符重载
~Vector();//析构
size_t Size();//大小
size_t Capacity();//容量
void Expand(size_t n);//扩容
void PushBack(DataType x);//尾插
void Reserve(size_t n);//保留
void PopBack();//尾删
void Insert(size_t pos, DataType x);//插入
void Erase(size_t pos);//删除
size_t Find(DataType x);//查找
void Show();
void Swap(Vector& tmp);
private:
DataType* _first;
DataType* _finish;
DataType* _endofstorage;
};
Vector.cpp
#include"Vector.h"
#include<iostream>
using namespace std;
Vector::Vector()//构造
:_first(NULL)
, _finish(NULL)
, _endofstorage(NULL)
{}
Vector::~Vector()//析构
{
if (_first)
{
delete[] _first;
}
_first = _finish = _endofstorage = NULL;
}
void Vector::Show()
{
DataType* cur = _first;
while (cur < _finish)
{
cout << *cur<<' ';
++cur;
}
cout << endl;
}
//v1(v)
Vector::Vector(const Vector& v)
{
//深拷贝
if (this != &v)
{
DataType* cur = v._first;
while (cur < v._finish)
{
this->PushBack(*cur);
cur++;
}
}
}
void Vector::Swap(Vector& tmp)
{
swap(_first, tmp._first);
swap(_finish, tmp._finish);
swap(_endofstorage, tmp._endofstorage);
}
Vector& Vector::operator=(const Vector& v)
{
if (this != &v)
{
//拷贝构造一样的对象,进行交换
Vector tmp(v);
Swap(tmp);
}
return *this;
}
size_t Vector::Size()//大小
{
return _finish - _first;
}
size_t Vector::Capacity()//容量
{
return _endofstorage - _first;
}
void Vector::Expand(size_t n)//扩容
{
if (n > Capacity())
{
//开空间
DataType* array = new DataType[n];
DataType* newend = array + n;
DataType* newfinish = array;
//开空间,拷数据
DataType* cur = _first;
while (cur < _finish)
{
*newfinish = *cur;
cur++;
newfinish++;
}
//释放旧空间
delete[] _first;
_finish = _endofstorage = NULL;
_first = array;
_finish = newfinish;
_endofstorage = newend;
}
}
void Vector::PushBack(DataType x)//尾插
{
if (_finish == _endofstorage)
{
size_t capacity = Capacity() > 0 ? Capacity() * 2 : 3;
Expand(capacity);
}
*_finish = x;
_finish++;
}
void Vector::Reserve(size_t n)//n大小,保留空间,欲开空间
{
Expand(n);
}
void Vector::PopBack()//尾删
{
if (_finish != NULL && _finish != _first)
{
--_finish;
}
}
void Vector::Insert(size_t pos, DataType x)//插入
{
//位置是否合法
if (_first + pos > _endofstorage)
return;
if (_finish == _endofstorage)
{
size_t capacity = Capacity() > 0 ? Capacity() * 2 : 3;
Expand(capacity);
}
DataType* cur = _finish;
while (cur >= _first + pos)
{
*(cur + 1) = *cur;
--cur;
}
*(_first + pos) = x;
_finish++;
}
void Vector::Erase(size_t pos)//删除
{
if (_first + pos > _endofstorage)
return;
//交换pos和最后一个位置
DataType tmp = *(_first + pos);
tmp = *(_finish - 1);
*(_finish - 1) = tmp;
--_finish;
}
size_t Vector::Find(DataType x)//查找
{
if (_first != NULL)
{
DataType* cur = _first;
while (cur < _finish)
{
if (*cur == x)
{
return cur-_first;
}
++cur;
}
return -1;
}
}
链表
#pragma once
#include<iostream>
using namespace std;
typedef int DataType;
struct ListNode
{
DataType _data;
ListNode* _next;
ListNode* _prev;
ListNode(DataType data )
:_data(data)
, _next(NULL)
, _prev(NULL)
{}
};
class List
{
typedef ListNode Node;
public:
void Show();//打印
List();//构造
List(const List& l);//拷贝构造
void Swap(List& l);//交换
List& operator=(const List& l);//赋值运算符重载
~List();//析构
void PushBack(DataType x);//尾插
void PopBack();//尾删
void PushFront(DataType x);//头插
void PopFront();//头删
void Insert(Node* pos, DataType x);//任意位置插入
void Erase(Node* pos);//任意位置删除
Node* Find(DataType x);//查找
private:
Node* head;
};
List.cpp
List::List()
:head(new Node(DataType()))
{
head->_next = head;
head->_prev = head;
}
List::List(const List& l)
{
if (l.head == NULL)
return;
Node* cur = l.head->_next;//将对象1进行尾插
while (cur != l.head)
{
this->PushBack(cur->_data);
cur = cur->_next;
}
}
void List::Swap(List& l)
{
swap(head, l.head);
}
List& List::operator=(const List& l)//交换
{
if (this != &l)
{
List tmp(l);//构造出和l一样的对象,进行交换
Swap(tmp);
}
return *this;
}
List::~List()
{
Node* cur = head;
if (cur == NULL)
return;
head->_prev->_next = NULL;
while (cur)
{
Node* tmp = cur;
cur = cur->_next;
delete tmp;
}
head = NULL;
}
void List::PushBack(DataType x)
{
Node* newnode = new Node(x);
if (head == NULL)//如果头为空
{
head = new Node(DataType());
head->_next = head;
head->_prev = head;
head->_next = newnode;
head->_prev = newnode;
newnode->_next = head;
newnode->_prev = head;
}
else
{
Node* tail = head->_prev;
tail->_next = newnode;
newnode->_next = head;
newnode->_prev = tail;
head->_prev = newnode;
}
}
void List::PopBack()
{
//空链表
if (head->_next == head)
return;
Node* tail = head->_prev;
Node* tmp = tail;
tail = tail->_prev;
head->_prev = tail;
tail->_next = head;
delete tmp;
tmp = NULL;
}
void List::PushFront(DataType x)//头插
{
Node* newnode = new Node(x);
Node* next = head->_next;
newnode->_next = next;//新节点与头的下一个节点相连
next->_prev = newnode;//新节点和头相连
newnode->_prev = head;
head->_next = newnode;
}
void List::PopFront()//头删
{
Node* tmp = head->_next;
head->_next = tmp->_next;
tmp->_next->_prev = head;
}
void List::Insert(Node* pos, DataType x)//任意位置插入
{
Node* newnode = new Node(x);
Node* prev = pos->_prev;
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = pos;
pos->_prev = newnode;
}
void List::Erase(Node* pos)//任意位置删除
{
Node* next = pos->_next;
pos->_prev->_next = next;
next->_prev = pos->_prev;
delete pos;
}
List::Node* List::Find(DataType x)
{
if (head == NULL)
return NULL;
Node* cur = head->_next;
while (cur != head)
{
if (cur->_data == x)
{
return cur;
}
cur = cur->_next;
}
return NULL;
}
void List::Show()//打印
{
Node* cur = head->_next;
while (cur != head)
{
printf("%d->", cur->_data);
cur = cur->_next;
}
printf("\n");
}