目录
前言
一、顺序表是什么?
二、功能介绍
1.初始化
2.建表
3.销毁顺序表
4.顺序表插入
5. 顺序表删除
6.顺序表查找
7.顺序表修改
8.输入输出功能
9.菜单界面
10.菜单功能
总代码
总结
c++数据结构中的方法通过面向对象的机制提高了程序的效率。
顺序表是一种线性表,作为线性表的一种,它是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
#define MAX 100
template
class SeqList
{
protected:
//数据成员的定义
T* data;
int maxSize;//最大数量
int last;//指向最后一位所在位置
public:
SeqList(int sz = MAX);//构造函数
~SeqList();//析构函数
SeqList(const SeqList& L);//拷贝函数
bool Insert(int i, const T& elem);//插入函数
bool Remove(int i, T& elem);//删除函数
int Search(const T& elem);//查找函数
void Setdata(int i, const T& elem);//修改数据
void input();//输入
void output();//输出
bool IsFull()//满了
{
return last == maxSize - 1;
}
bool IsEmpty()//空的
{
return last == -1;
}
};
//在这个构造函数里面创建顺序表
template
SeqList::SeqList(int sz)
{
maxSize = sz;//创建顺序表
data = new T[maxSize];//创建顺序表
last = -1;//使线性表为空
}
//顺序表的拷贝构造函数
template
SeqList::SeqList(const SeqList& L)
{
maxSize = L.maxSize;
last = L.last;
data = new T[maxSize];
if (data == NULL)
{
cerr << "存储分配失败" << endl;
exit(1);
}
for (int i = 0;i <= last;i++)
{
data[i] = L.data[i];
}
}
//用析构函数销毁顺序表。释放data所指的内存空间
template
SeqList::~SeqList()
{
delete[] data;
}
//顺序表的插入函数
template
bool SeqList::Insert(int i, const T& elem)
{
if (last == maxSize - 1)
{
cerr << "表已满" << endl;
return false;
}
if (i<1 || i>last + 2)
{
cerr << "插入位置越界" << endl;
return false;
}
for (int j = last;j >= i - 1;j--)
{
data[j + 1] = data[j];
}
data[i - 1] = elem;
last++;
cout << "插入成功" << endl;
return true;
}
//顺序表的删除函数
template
bool SeqList::Remove(int i, T& elem)
{
if (last == -1)
{
cerr << "表为空" << endl;
return false;
}
if (i<1 || i>last + 1)
{
cerr << "删除位置越界" << endl;
return false;
}
elem = data[i - 1];
for (int j = i - 1;j < last;j++)
{
data[j] = data[j + 1];
}
last--;
cout << "删除成功" << endl;
return true;
}
//顺序表的查找函数
template
int SeqList::Search(const T& elem)
{
for (int j = 0;j <= last-1;j++)
{
if (data[j] == elem)
{
cout <<"所在位置为:" << j + 1 << endl;
break;
}
}
return 0;
}
//顺序表的修改函数
template
void SeqList::Setdata(int i, const T& elem)
{
if (i<1 || i>last + 1)
{
cerr << "插入位置越界" << endl;
}
else
{
data[i - 1] = elem;
}
cout << "修改成功" << endl;
}
template
void SeqList::input()//从键盘输入
{
cout << "开始建立顺序表请输入表中元素";
while (1) {
cin >> last;
if (last <= maxSize)break;
cout << "表中元素个数输入有误,范围不能超过" << maxSize - 1 << ":"; ;
}
for (int i = 0;i <= last-1;i++)
{
cout << i + 1 << " ";cin >> data[i];
}
cout << "建立成功" << endl;
}
template
void SeqList::output()//输出到屏幕
{
cout << "顺序表当前元素最后位置是" << last << endl;
for(int i=0;i<=last-1;i++)
{
cout << data[i] << " ";
}
cout << endl;
}
//菜单界面
void showMenu()
{
cout << "*******************************" << endl;
cout << "***** 1、输入顺序表内容 *****" << endl;
cout << "***** 2、显示顺序表内容 *****" << endl;
cout << "***** 3、删除顺序表内容 *****" << endl;
cout << "***** 4、查找顺序表内容 *****" << endl;
cout << "***** 5、插入顺序表内容 *****" << endl;
cout << "***** 6、修改顺序表内容 *****" << endl;
cout << "***** 0、退出顺序表**** *****" << endl;
cout << "*******************************" << endl;
}
while (true)
{
showMenu();
cin >> select;
switch (select)
{
case 1: //添加
lst.input();
system("pause");
break;
case 2: //显示
lst.output();
system("pause");
break;
case 3: //删除
cout << "输入删除位置:" << endl;
cin >> i ;
lst.Remove(i,elem);
system("pause");
break;
case 4: //查找
cout << "输入查找内容:" << endl;
cin >> elem;
lst.Search(elem);//无值输出为错误
system("pause");
break;
case 5: //插入
cout << "输入插入位置和内容:" << endl;
cin >> i >> elem;
lst.Insert(i,elem);
system("pause");
break;
case 6: //修改
cout << "请输入修改的位置和内容" << endl;
cin >> i >> elem;
lst.Setdata(i, elem);
system("pause");
break;
case 0: //退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
#define MAX 100
template
class SeqList
{
protected:
//数据成员的定义
T* data;
int maxSize;//最大数量
int last;//指向最后一位所在位置
public:
SeqList(int sz = MAX);//构造函数
~SeqList();//析构函数
SeqList(const SeqList& L);//拷贝函数
bool Insert(int i, const T& elem);//插入函数
bool Remove(int i, T& elem);//删除函数
int Search(const T& elem);//查找函数
void Setdata(int i, const T& elem);//修改数据
void input();//输入
void output();//输出
bool IsFull()//满了
{
return last == maxSize - 1;
}
bool IsEmpty()//空的
{
return last == -1;
}
};
//在这个构造函数里面创建顺序表
template
SeqList::SeqList(int sz)
{
maxSize = sz;//创建顺序表
data = new T[maxSize];//创建顺序表
last = -1;//使线性表为空
}
//顺序表的拷贝构造函数
template
SeqList::SeqList(const SeqList& L)
{
maxSize = L.maxSize;
last = L.last;
data = new T[maxSize];
if (data == NULL)
{
cerr << "存储分配失败" << endl;
exit(1);
}
for (int i = 0;i <= last;i++)
{
data[i] = L.data[i];
}
}
//用析构函数销毁顺序表。释放data所指的内存空间
template
SeqList::~SeqList()
{
delete[] data;
}
//顺序表的插入函数
template
bool SeqList::Insert(int i, const T& elem)
{
if (last == maxSize - 1)
{
cerr << "表已满" << endl;
return false;
}
if (i<1 || i>last + 2)
{
cerr << "插入位置越界" << endl;
return false;
}
for (int j = last;j >= i - 1;j--)
{
data[j + 1] = data[j];
}
data[i - 1] = elem;
last++;
cout << "插入成功" << endl;
return true;
}
//顺序表的删除函数
template
bool SeqList::Remove(int i, T& elem)
{
if (last == -1)
{
cerr << "表为空" << endl;
return false;
}
if (i<1 || i>last + 1)
{
cerr << "删除位置越界" << endl;
return false;
}
elem = data[i - 1];
for (int j = i - 1;j < last;j++)
{
data[j] = data[j + 1];
}
last--;
cout << "删除成功" << endl;
return true;
}
//顺序表的查找函数
template
int SeqList::Search(const T& elem)
{
for (int j = 0;j <= last-1;j++)
{
if (data[j] == elem)
{
cout <<"所在位置为:" << j + 1 << endl;
break;
}
}
return 0;
}
//顺序表的修改函数
template
void SeqList::Setdata(int i, const T& elem)
{
if (i<1 || i>last + 1)
{
cerr << "插入位置越界" << endl;
}
else
{
data[i - 1] = elem;
}
cout << "修改成功" << endl;
}
template
void SeqList::input()//从键盘输入
{
cout << "开始建立顺序表请输入表中元素";
while (1) {
cin >> last;
if (last <= maxSize)break;
cout << "表中元素个数输入有误,范围不能超过" << maxSize - 1 << ":"; ;
}
for (int i = 0;i <= last-1;i++)
{
cout << i + 1 << " ";cin >> data[i];
}
cout << "建立成功" << endl;
}
template
void SeqList::output()//输出到屏幕
{
cout << "顺序表当前元素最后位置是" << last << endl;
for(int i=0;i<=last-1;i++)
{
cout << data[i] << " ";
}
cout << endl;
}
//菜单界面
void showMenu()
{
cout << "*******************************" << endl;
cout << "***** 1、输入顺序表内容 *****" << endl;
cout << "***** 2、显示顺序表内容 *****" << endl;
cout << "***** 3、删除顺序表内容 *****" << endl;
cout << "***** 4、查找顺序表内容 *****" << endl;
cout << "***** 5、插入顺序表内容 *****" << endl;
cout << "***** 6、修改顺序表内容 *****" << endl;
cout << "***** 0、退出顺序表**** *****" << endl;
cout << "*******************************" << endl;
}
int main()
{
SeqList lst;
int i;
int elem;
int select;
string name;
while (true)
{
showMenu();
cin >> select;
switch (select)
{
case 1: //添加
lst.input();
system("pause");
break;
case 2: //显示
lst.output();
system("pause");
break;
case 3: //删除
cout << "输入删除位置:" << endl;
cin >> i ;
lst.Remove(i,elem);
system("pause");
break;
case 4: //查找
cout << "输入查找内容:" << endl;
cin >> elem;
lst.Search(elem);//无值输出为错误
system("pause");
break;
case 5: //插入
cout << "输入插入位置和内容:" << endl;
cin >> i >> elem;
lst.Insert(i,elem);
system("pause");
break;
case 6: //修改
cout << "请输入修改的位置和内容" << endl;
cin >> i >> elem;
lst.Setdata(i, elem);
system("pause");
break;
case 0: //退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
system("cls");
}
system("pause");
return 0;
}
以上就是c++数据结构代码的全部内容,其中运用到了类,类模板,new和delete,希望能够帮助到大家。