C++實現順序表
Seqlist.h
#pragma once
typedef int DataType;
#include<assert.h>
class Seqlist
{
public:
Seqlist()//構造函數
:_data(NULL)
, _size(0)
, _capa(0)
{
}
~Seqlist()//析構函數
{
if (_data)
{
free(_data);
_data = NULL;
_size = 0;
_capa = 0;
}
}
Seqlist(const Seqlist& s)//拷貝構造
{
_data = new DataType[s._capa];
memcpy(_data, s._data, s._size*sizeof(DataType));
_size = _capa = s._size;
}
Seqlist& operator=(const Seqlist& s)//賦值運算符“=”重載
{
if (this != &s)
{
free(_data);
_data = new DataType[s._capa];
memcpy(_data, s._data, s._size*sizeof(DataType));
_size = _capa = s._size;
}
return *this;
}
void PushBack(DataType x)//尾插
{
CheckCapcacity();
_data[_size++] = x;
}
void PopBack()//尾刪
{
assert(_size > 0);
--_size;
}
void PushFront(DataType x)//頭插
{
CheckCapcacity();
DataType end = _size-1;
for (size_t i = 0; i < _size; i++)
{
_data[end + 1] = _data[end];
end--;
}
_data[0] = x;
_size++;
}
void PopFront()//頭刪
{
for (size_t i = 0; i < _size; i++)
{
_data[i] = _data[i+1];
}
_size--;
}
void Insert(size_t pos, DataType x)//指定位置插入
{
assert(pos < _size);
CheckCapcacity();
DataType end = _size - 1;
for (size_t i = pos-1; i < _size; i++)
{
_data[end + 1] = _data[end];
end--;
}
_data[pos] = x;
_size++;
}
void Erase(size_t pos)//指定位置刪除
{
assert(pos < _size);
for (size_t i = pos; i < _size; i++)
{
_data[i] = _data[i+1];
}
_size--;
}
DataType& operator[](size_t pos)//[]重載,可讀可寫
{
assert(pos < _size);
return _data[pos];
}
DataType& operator[](size_t pos)const//只讀
{
assert(pos < _size);
return _data[pos];
}
void CheckCapcacity()//檢測容量
{
if (_size == _capa)
{
_capa = _capa * 2 + 3;
_data = (DataType*)realloc(_data, _capa*sizeof(DataType));
}
}
void Print()//輸出
{
for (size_t i = 0; i < _size; ++i)
{
cout << _data[i] << " ";
}
cout << endl;
}
private:
DataType *_data;
size_t _size;
size_t _capa;
};
void TestSeqList()
{
Seqlist s;
s.PushBack(1);
s.PushBack(2);
s.PushBack(3);
s.PushBack(4);
s.Print();
s.PopBack();
s.Print();
s.PushFront(5);
s.Print();
s.PopFront();
s.Print();
s.Insert(1, 8);
s.Print();
s.Erase(1);
s.Print();
Seqlist s1(s);
s1.Print();
Seqlist s2;
s2 = s1;
s2.Print();
}
C++實現單鏈表
Slist.h
#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
typedef int DataType;
struct SlistNode
{
SlistNode* _next;
DataType _data;
SlistNode(DataType x)
:_data(x)
, _next(NULL)
{}
};
typedef SlistNode Node;
class Slist
{
public:
Slist()
: _head(NULL)
, _tail(NULL)
{}
Slist(const Slist& s)
{
Node* cur = s._head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
Slist& operator=(const Slist& 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;
}
~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) && (_tail == 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* tmp = _head;
while (tmp->_next != _tail)
{
tmp = tmp->_next;
}
delete _tail;
_tail = tmp;
_tail->_next = NULL;
}
}
void PushFront(DataType x)
{
if ((_head == NULL) && (_tail == 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* tmp = _head;
_head = _head->_next;
delete tmp;
}
}
Node* Find(DataType x)
{
Node* tmp = _head;
while (tmp)
{
if (tmp->_data == x)
return tmp;
tmp = tmp->_next;
}
return NULL;
}
void Insert(Node* pos, DataType x)
{
assert(_head && pos);
if ((pos == _head) || (_head->_next == NULL))
{
PushFront(x);
}
else
{
Node* cur = _head;
while (cur->_next != pos)
{
cur = cur->_next;
}
Node* tmp = new Node(x);
tmp->_next = pos;
cur->_next = tmp;
}
}
void Erase(Node* pos)
{
assert(_head && pos);
if ((pos == _head) || (_head->_next == NULL))
{
PopFront();
}
else
{
Node* cur = _head;
while (cur->_next != pos)
{
cur = cur->_next;
}
cur->_next = pos->_next;
delete pos;
}
}
void Print()
{
Node* cur = _head;
while (cur)
{
cout << cur->_data << "->";
cur = cur->_next;
}
cout << "NULL" << 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();
s.PopBack();
s.Print();
s.PushFront(6);
s.Print();
s.PopFront();
s.Print();
s.Insert(s.Find(4), 8);
s.Print();
s.Erase(s.Find(8));
s.Print();
s.Insert(s.Find(1), 8);
s.Print();
s.Erase(s.Find(8));
s.Print();
Slist s1(s);
s1.Print();
Slist s2;
s2 = s1;
s2.Print();
s2 = s2;
s2.Print();
}
c++實現雙向鏈表
DoublyLinkedList.h
#pragma once
#include <iostream>
using namespace std;
#include <assert.h>
typedef int DataType;
struct Listnode
{
Listnode* _next;
Listnode* _prev;
DataType _data;
Listnode(DataType x)
:_next(NULL)
, _prev(NULL)
, _data(x)
{}
};
typedef Listnode node;
class List
{
public:
List()
:_head(NULL)
,_tail(NULL)
{}
List(const List& l)
:_head(NULL)
, _tail(NULL)
{
node* cur = l._head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
List& operator=(const List& l)
{
if (this != &l)
{
node* cur = _head;
while (cur)
{
node* next = cur->_next;
delete cur;
cur = next;
}
_head = _tail = NULL;
node* tmp = l._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* tmp = new node(x);
tmp->_next = NULL;
tmp->_prev = NULL;
_head = _tail = tmp;
}
else
{
node* tmp = new node(x);
_tail->_next = tmp;
tmp->_prev = _tail;
_tail = tmp;
}
}
void PopBack()
{
if (_head == NULL)
{
return;
}
else if (_head->_next == NULL)
{
delete _head;
_head = _tail = NULL;
}
else
{
node* tmp = _tail;
_tail = _tail->_prev;
_tail->_next = NULL;
delete tmp;
}
}
void PushFront(DataType x)
{
if (_head == NULL)
{
_head = _tail = new node(x);
}
else
{
node* tmp = new node(x);
tmp->_next = _head;
_head->_prev = tmp;
_head = _head->_prev;
}
}
void PopFront()
{
if (_head == NULL)
{
return;
}
else if (_head->_next == NULL)
{
delete _head;
_head = _tail = NULL;
}
else
{
node* tmp = _head;
_head = _head->_next;
delete tmp;
_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 == 0) || (pos->_prev == NULL))
{
PushFront(x);
}
else
{
node* font = pos->_prev;
node* tmp = new node(x);
tmp->_prev = font;
tmp->_next = pos;
font->_next = tmp;
pos->_prev = tmp;
}
}
void Erase(node* pos)
{
assert(pos);
if ((pos == 0) || (pos->_prev == NULL))
{
PopFront();
}
else if (pos->_next == NULL)
{
PopBack();
}
else
{
node* font = pos->_prev;
node* last = pos->_next;
font->_next = last;
last->_prev = font;
delete pos;
}
}
void Print()
{
node* cur = _head;
while (cur)
{
cout << cur->_data << "->";
cur = cur->_next;
}
cout << "NULL" << endl;
}
private:
node* _head;
node* _tail;
};
void TestDlist()
{
List s;
s.PushBack(1);
s.PushBack(2);
s.PushBack(3);
s.PushBack(4);
s.PushBack(5);
s.Print();
s.PopBack();
s.Print();
s.PushFront(6);
s.Print();
s.PopFront();
s.Print();
s.Insert(s.Find(4), 8);
s.Print();
s.Erase(s.Find(8));
s.Print();
s.Insert(s.Find(1), 8);
s.Print();
s.Erase(s.Find(8));
s.Print();
List s1(s);
s1.Print();
List s2;
s2 = s1;
s2.Print();
s2 = s2;
s2.Print();
}
測試部分test.cpp
#include<iostream>
using namespace std;
#include"Seqlist.h"
#include"SingLelinkedList.h"
#include"DoublyLinkedList.h"
int main()
{
cout << "C++實現順序表測試結果" << endl;
TestSeqList();
cout << "C++實現單鏈表測試結果" << endl;
TestSlist();
cout << "C++實現雙向鏈表測試結果" << endl;
TestDlist();
system("pause");
}
測試結果