所谓顺序表指的是线性表中元素存储的方式是连续的,即在逻辑上相邻的元素在内存上也是相邻的。所有的元素都可以实现随机存取。 但缺点是,删除的个别操作会移动大量元素。
代码实现如下:
头文件:
#pragma once
#include <iostream>
#include <assert.h>
#define Swap(a,b,c) ((c)=(a),(a)=(b),(b)=(c))
using namespace std;
typedef enum {FALSE,TRUE}Status;
template<class T>
class SeqList
{
public:
SeqList(int sz = DefaultSize)
{
capacity = sz > DefaultSize ? sz : DefaultSize;
base = new T[sz];
size = 0;
}
~SeqList()
{
delete[]base;
base = NULL;
capacity = size = 0;
}
public:
Status IsFull() const
{
if (size >= capacity)
return TRUE;
else
return FALSE;
}
Status IsEmpty() const
{
if (size >= capacity)
return FALSE;
else
return TRUE;
}
Status push_back(const T& x)
{
return insert_pos(end(), x);
}
Status push_front(const T& x)
{
return insert_pos(begin(), x);
}
void Show_list()const
{
for (size_t i = 0; i < size; ++i)
{
cout << base[i] << " ";
}
cout << endl;
}
size_t length() const
{
return size;
}
size_t begin() const
{
return 0;
}
size_t end() const
{
return size;
}
Status pop_back()
{
return delete_pos(begin());
}
Status pop_front()
{
return delete_pos(end());
}
Status delete_pos(size_t p)
{
if (p < 0 || p > size)
{
cout << "位置非法 " << " 不能删除!" << endl;
return FALSE;
}
for (size_t i = p; i < size; i++)
{
base[i] = base[i + 1];
}
size--;
return TRUE;
}
Status insert_pos(size_t p, const T& x)
{
if (Inc() && IsFull())
{
cout << "空间满 " << " 不能插入!" << endl;
return FALSE;
}
if (p < 0 || p > size)
{
cout << "位置非法 " << " 不能插入!" << endl;
return FALSE;
}
for (size_t i = size; i > p; --i)
{
base[i] = base[i - 1];
}
base[p] = x;
size++;
return TRUE;
}
/*Status insert_value(T& x)
{
if (IsFull())
return FALSE;
sort();
int i = begin();
for (; i < size; ++i)
{
if (x <= base[i])
{
break;
}
}
insert_pos(i, x);
return TRUE;
}*/
Status insert_value(T& x)
{
if (Inc() && IsFull())
{
cout <<"满," <<x<<"不能插入!" << endl;
return FALSE;
}
sort();
size_t i = 0;
while (i < size && x > base[i])
i++;
insert_pos(i, x);
return TRUE;
}
void delete_value(T& x)
{
int pos;
while (pos = find(x), pos != -1)
delete_pos(pos);
}
size_t find(const T& x)const
{
for (size_t i = 0; i < size; ++i)
{
if (x == base[i])
return i;
}
return -1;
}
T getvalue(size_t pos) const
{
if (pos > size)
{
cout << "位置非法 " << endl;
return -1;
}
return base[pos];
}
Status modify(size_t pos,T& x)
{
if (pos > size)
{
cout << "位置非法 " << endl;
return FALSE;
}
base[pos] = x;
return TRUE;
}
void clear()
{
size = 0;
}
void destroy()
{
if (base)
delete[]base;
size = capacity = 0;
}
Status sort()
{
T tmp;
for (size_t i = 1; i < size; i++)
{
for (size_t j = 0; j < size-i; j++)
{
if (base[j] > base[j + 1])
Swap(base[j], base[j + 1], tmp);
}
}
return TRUE;
}
Status resver()
{
T tmp ;
size_t top = begin();
size_t rear = end()-1;
while (top < rear)
{
Swap(base[top], base[rear], tmp);
top++;
rear--;
}
return TRUE;
}
Status Inc()
{
T* NewBase = new T[capacity + IncSize];
assert(NewBase != NULL);
return FALSE;
memcpy(NewBase, base, sizeof(T)*size);
delete[]base;
base = NewBase;
capacity += IncSize;
}
Status merge(SeqList<T>& lt1, SeqList<T>& lt2)
{
lt1.sort();
lt2.sort();
size_t i = 0;
size_t j = 0;
size_t k = 0;
while (i < lt1.size && j < lt2.size)
{
if (lt1.base[i] < lt2.base[j])
base[k++] = lt2.base[j++];
else
base[k++] = lt2.base[i++];
}
while (i < lt1.size)
base[k++] = lt1.base[i++];
while (j < lt2.size)
base[k++] = lt1.base[j++];
size = lt1.size + lt2.size;
return TRUE;
}
private:
enum {DefaultSize = 20,IncSize=4};
T* base;
size_t capacity;
size_t size;
};
值得注意的是,我们提供了函数Status Inc(),当顺序表满时,可以从内存重新申请空间,增长方式为定长增长。即一种类似与pool的设计概念。