顺序表:顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构。
确定了起始位置,就可通过公式计算出表中任一元素的地址:LOC(ai)=LOC(a1)+(i-1)*L 1≤i≤n (L是元素占用存储单元的长度)
顺序表的实现一般是实现连续开辟一段空间,然后在进行数据的增删查改(静态顺序表)
其代码实现如下:
#include<assert.h>
#include<iostream>
using namespace std;
typedef int DataType;
class SeqList
{
public:
SeqList()
:_array(NULL)
,_size(0)
,_capacity(0)
{}
SeqList(const SeqList&s)
{
_array = (DataType*)malloc(s._size*sizeof(DataType));
memcpy(_array,s._array,s._size*sizeof(DataType));
_size = s._size;
_capacity = s._size;
}
/*SeqList&operator(SeqList s)
{*/
//传统写法
/*if(this != &s)
{
free(_array);
_array =(DataType*)malloc(sizeof(DataType)*s._size);
memcpy(_array,s._array,sizeof(DataType)*s._size);
_size = s._size;
_capacity = s._size;
}*/
SeqList&operator=(SeqList s)
{
//现代写法
free(_array);
Swap(s);
return *this;
}
~SeqList()
{
if(_array)
{
free(_array);
_array = NULL;
_size = _capacity = 0;
}
}
void Print()
{
for(size_t i = 0; i < _size; i++)
{
cout << _array[i] << "";
}
cout << endl;
}
void CheakCapacity()
{
if(_size == _capacity)
{
_capacity = 2* _capacity +3;
_array = (DataType*)realloc(_a,_capacity*sizeof(DataType));
assert(_array);
}
}
void Swap(SeqList& s)
{
swap(_array,s._array);
swap(_size,s._size);
swap(_capacity,s._capacity);
}
void PushBack(DataType x)
{
Insert(_size,x);
}
void PopBack()
{
Erase(_size);
}
void PushFront(DataType x)
{
Insert(0,x);
}
void PopFront()
{
Erase(0);
}
void Insert(size_t pos, DataType x)
{
assert(pos <= _size);
CheakCapacity();
int end = (int)_size - 1;
if(pos == 0)
{
while(end >= 0)
{
_array[end+1] = _array[end];
end--;
}
_array[0] = x;
}
else
{
while(end >= (int)pos)
{
_array[end+1] = _array[end];
end--;
}
_array[pos] = x;
}
_size++;
}
void Erase(size_t pos)
{
assert(pos < _size);
{
if(_size > 0)
{
//popfront的实现
if(pos == 0)
{
int end = 0;
while(end <(int)_size -1)
{
_array[end] = _array[end+1];
end++;
}
_size--;
}
//popBack的实现
else if(pos == _size)
{
_size--;
}
else
{
int end = pos;
while(end < (int)_size -1)
{
_array[end+1] = _array[end];
end++;
}
_size--;
}
}
return;
}
}
DataType& operator[](size_t pos)
{
assert(pos < _size);
return _array[pos];
}
private:
DataType* _array;
size_t* _size;
size_t* _capacity;
};
单链表:单链表是一种链式存取的数据结构
Tail指针是为了更方便的查找到末尾,在末尾插入元素。
单链表则是一次只开辟一个结点的空间,用来存储当前要保存的数据及指向下一个结点或NULL的指针
#include<iostream>
#include<windows.h>
#include<assert.h>
using namespace std;
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)
:_head(NULL)
, _tail(NULL)
{
Node*cur = s._head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
SList& operator=(const SList& s)
{
Clear();
Node* cur = s._head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
return *this;
}
~SList()
{
Clear();
}
void Clear()
{
Node* cur = _head;
while(_head != NULL)
{
cur = _head;
_head = cur->_next;
delete cur;
}
_head = _head = NULL;
}
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->_next==NULL)
{
delete _head;
_head = _tail =NULL;
}
else
{
Node* tmp = _head;
while (tmp->_next->_next != NULL)
{
tmp = tmp->_next;
}
_tail = tmp;
tmp->_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
{
Node* cur = _head;
_head = _head->_next;
delete cur;
}
}
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(pos);
if(pos == NULL)
{
PopFront();
}
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(pos);
if(pos->_next==NULL)
{
PopBack();
}
else
{
Node*cur = _head;
while (cur->_next != pos)
{
cur = cur->_next;
}
cur->_next = pos->_next;
delete pos;
}
}
void Print()
{
Node* tmp = _head;
while(tmp != NULL)
{
cout<<tmp->_data<<" ";
tmp = tmp->_next;
}
cout<<endl;
}
private:
Node* _head;
Node* _tail;
};