線性表之順序表(C語言版本)http://t.csdn.cn/GHizI
成都創(chuàng)新互聯(lián)擁有十余年成都網(wǎng)站建設(shè)工作經(jīng)驗(yàn),為各大企業(yè)提供網(wǎng)站制作、成都做網(wǎng)站服務(wù),對(duì)于網(wǎng)頁設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、app軟件開發(fā)公司、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、程序開發(fā)、網(wǎng)站優(yōu)化(SEO優(yōu)化)、微網(wǎng)站、申請(qǐng)域名等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了很多網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營銷經(jīng)驗(yàn),集策劃、開發(fā)、設(shè)計(jì)、營銷、管理等網(wǎng)站化運(yùn)作于一體,具備承接各種規(guī)模類型的網(wǎng)站建設(shè)項(xiàng)目的能力。SeqlistC.h文件
#pragma once
#include#include
using namespace std;
const int InitSize = 8;
templateclass SeqList
{
private:
T* data;
int capacity;//容量
int size;//大小
public:
SeqList() :size{ 0 }, capacity{InitSize} //無參構(gòu)造函數(shù),創(chuàng)建一個(gè)容量為InitSize的空表
{
data = new T[InitSize];
if (data == nullptr)
{
cout<< "erro"<< endl;
}
}
SeqList(T arr[], int n); //初始化容量,創(chuàng)建一個(gè)有值的順序表
//拷貝構(gòu)造函數(shù)
SeqList(const SeqList& seq)
{
size = seq.size;
capacity = seq.capacity;
data = new T[seq.size];
memcpy(data, seq.data, sizeof(T) * seq.capacity);
}
//重載“+”運(yùn)算符
SeqList operator+(const SeqList& sq)
{
int n = size + sq.size;
T* newdata = new T[n];
int a = size;//this線性表大小
int b = sq.size;//aq線性表大小
T* sra = data;
T* srb= sq.data;
T* newsrc = newdata;
while (a--)
{
*newsrc++ = *sra++;
}
while (b--)
{
*newsrc++ = *srb++;
}
return SeqList(newdata,n);//調(diào)用其構(gòu)造函數(shù)
}
//賦值函數(shù)
SeqList& operator=(const SeqList& a)
{
if (this != &a)
{
//釋放原有空間,防止原有空間大小不夠,重新申請(qǐng)空間
delete[] data;
data = new T[a.size];
memcpy(data, a.data, sizeof(T) * a.capacity);
this->size = a.size;
this->capacity = a.capacity;
}
return *this;
}
~SeqList()
{
delete[] data;
size = 0;
capacity = 0;
}
void push_back(T val);//尾插
void Show()const;//顯示
void push_front(T val);//頭插
void pop_back();//尾刪
void pop_front();//頭刪
void inser_pos(T val, int pos);//按位置插入
T find(T val)const;//按值查找
int length()const { return size; }//長度
void delete_pos(int pos);//按位置刪除
void delete_val(T val);//按值刪除
void Sort();//排序
void Reverse();//逆序
void Clear() { size = 0; }//清空
bool Inc();//增容
};
//交換函數(shù)
templatevoid Swap(T& a, T& b)
{
T tmp = 0;
tmp = a;
a = b;
b = tmp;
}
//初始化容量,創(chuàng)建一個(gè)有值的順序表
templateSeqList:: SeqList(T arr[], int n)
{
data = new T[InitSize];
if (data == nullptr)
{
cout<< "erro"<< endl;
}
capacity = InitSize;
while (n >this->capacity)
{
if (this->Inc())
{
break;
}
else
{
cout<< "erro"<< endl;
}
}
for (int i = 0; i< n; ++i)
{
data[i] = arr[i];
}
size = n;
}
//增容
templatebool SeqList::Inc()
{
T* newdata = new T[capacity * 2];
if (newdata == nullptr)
{
cout<< "erro"<< endl;
}
int n = size;//線性表大小
T* src = data;
T* newsrc = newdata;
while (n--)
{
*newsrc++ = *src++;
}
this->data = newdata;
this->capacity = this->capacity * 2;
return true;
}
//顯示
templatevoid SeqList::Show()const
{
if (this->size == 0)
{
cout<< "沒有數(shù)據(jù)"<< endl;
}
else
{
for (int i = 0; i< this->size; ++i)
{
cout<< this->data[i]<< " ";
}
cout<< endl;
}
}
//尾插
templatevoid SeqList::push_back(T val)
{
if (size >= capacity && !Inc())
{
cout<< "順序表滿了,不能尾插"<< endl;
return;
}
data[size] = val;
size++;
}
//頭插
templatevoid SeqList::push_front(T val)
{
if (size >= capacity && !Inc())
{
cout<< "順序表滿了,不能頭插"<< endl;
return;
}
for (int i = size; i >0; --i)
{
data[i] = data[i - 1];
}
data[0] = val;
size++;
}
//尾刪
templatevoid SeqList::pop_back()
{
assert(this != nullptr);
size--;
}
//頭刪
templatevoid SeqList::pop_front()
{
assert(this != nullptr);
for (int i = 1; i< size; ++i)
{
data[i - 1] = data[i];
}
size--;
}
//按位置插入
templatevoid SeqList::inser_pos(T val, int pos)
{
//判斷位置是否合法
if (pos<0 || pos>size)
cout<< "pos erro"<< endl;
if (size == capacity && !Inc())
{
cout<< "Inc erro"<< endl;
}
for (int i = size; i >pos; --i)
{
data[i] = data[i - 1];
}
data[pos] = val;
size++;
}
//按值查找
templateT SeqList::find(T val)const
{
if (size == 0)
cout<< "空表"<< endl;
for (int i = 0; i< size; ++i)
{
if (data[i] == val) return i;
}
return -1;
}
//按位置刪除
templatevoid SeqList::delete_pos(int pos)
{
assert(this != nullptr);
if (pos< 0 || pos >= size)
cout<< "pos err"<< endl;
for (int i = pos; i< size - 1; i++)
{
data[i] = data[i + 1];
}
size--;
}
//按值刪除
templatevoid SeqList::delete_val(T val)
{
assert(this != nullptr);
int pos = find(val);
if (pos == -1)
cout<< "val不存在"<< endl;
delete_pos(pos);
}
//排序
templatevoid SeqList::Sort()
{
for (int i = 0; i< size - 1; ++i)
{
for (int j = 0; j< size - i - 1; j++)
{
if (data[j] >data[j + 1])
{
Swap(data[j], data[j + 1]);
}
}
}
}
//逆序
templatevoid SeqList::Reverse()
{
int left = 0; int right = size - 1;
while (left< right)
{
Swap(data[left], data[right]);
left++;
right--;
}
}
test.cpp測(cè)試代碼
#include"SeqlistC.h"
int main()
{
int arr[5] = { 1,2,3,4,5};
int brr[3] = { 6,7,8 };
SeqListsq = SeqList(arr, 5);
sq.Show();
SeqListsq1;
sq1.Show();
SeqListsq2 = SeqList(arr, 3);
sq2.Show();
sq = sq + sq2;
sq.Show();
sq.push_back(6);
sq.Show();
sq.push_front(0);
sq.Show();
sq.pop_back();
sq.Show();
sq.pop_front();
sq.Show();
sq.inser_pos(0, 3);
sq.Show();
cout<<"元素0的位置是:"<
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧