您的位置:首页 > 编程语言 > C语言/C++

顺序表的实现C++封装

2015-10-03 10:35 501 查看
从今天开始,将记录有关数据结构的内容;首先是循序表的使用。先给出顺序表的节点,然后给出算法,最后附上完整的C++封装好的代码。(给出的函数为类封装函数,若要改为C代码,形参变量需改变)

以下代码存在一个class中

定义节点

const int MAX_N = 100;
//节点类
typedef class node
{
public:
//链表中数据值
int date[MAX_N];
//链表长度
int length;
}SqList_Node;


以下函数存在另一个class中
以下函数存在于private类型中

线性表初始化

//初始化
void InitSqList(){
L = (SqList_Node *)malloc(sizeof(SqList_Node));
L->length = 0;
}


获取线性表

//获取顺序表
SqList_Node * GetList(){
return L;
}


以下函数存在于public类型中

初始化顺序表

//构造
SqList(SqList_Node *&L){
InitSqList();
}


释放顺序表

//析构并释放
~SqList(){
free(GetList());
}


创建顺序表

//构建顺序表
void CreateList(int a[],int n){
int i;
for (i = 0; i < n; i++){
L->date[i] = a[i];
}
L->length = n;
}


判断是否为空

//判断是否为空
bool isEmpty(){
return (L->length == 0);
}


求顺序表长度

//顺序表长度
int m_ListLength(){
return (L->length);
}


输出顺序表

//输出表
void PrintList(){
if (!isEmpty()){
int i;
for (i = 0; i < L->length; i++){
cout << L->date[i] << ends;
}
cout << endl;
}
else{
cout << "It is empty." << endl;
}
}


查找序号并返回对应值

//查找序号并返回对应值
int FindListElem(int i){
i--;
if (!isEmpty() && i >= 0 && i < L->length){
int j = 0;
while (j!=i){
j++;
}
return L->date[j];
}
else{
cout << "It is Empty or invalid location." << endl;
}
}


按值查找并返回第一个与之匹配的序号

<pre name="code" class="cpp">	//按值查找并返回序号
int FindElem_by_Value(int value){
if (isEmpty()){
cout << "It is Empty." << endl;
return 0;
}
else{
int i = 0;
while (i >= 0 && i < L->length&&L->date[i] != value){
i++;
}
return i;
}
return 0;
}



插入元素

//插入元素
void InsertElem(int i, int elem){
i--;
if (!isEmpty() && i >= 0 && i < L->length){
int j = L->length - 1;
while (j >= i){
L->date[j + 1] = L->date[j];
j--;
}
L->date[i] = elem;
L->length++;
}
else if (!isEmpty() && i >= L->length){
cout << "It is bigger than the length." << endl;
}
else if (isEmpty()){
cout << "It is empty,insert in the first place" << endl;
L->date[0] = elem;
L->length++;
}
else{
cout << "error" << endl;
}
}


删除元素

//删除元素
int DeleteElem(int i){
int res = 0;
i--;
if (!isEmpty() && i >= 0 && i < L->length){
int j = 0;
while (j < i){
j++;
}
res = L->date[j];
while (j < L->length - 1){
L->date[j] = L->date[j + 1];
j++;
}
L->length--;
}
else if (!isEmpty() && i>L->length){
cout << "It is bigger than the length." << endl;
}
else{
cout << "it is empty or no such element." << endl;
}
return res;
}


以上便是顺序表的所有算法的C++实现,下面给出完整的封装好的代码

/*
*顺序表的实现
*/

#include<iostream>
using namespace std;
const int MAX_N = 100; //节点类 typedef class node { public: //链表中数据值 int date[MAX_N]; //链表长度 int length; }SqList_Node;
//顺序表类
class SqList
{
public:

//构造
SqList(SqList_Node *&L){
InitSqList();
}

//析构并释放
~SqList(){
free(GetList());
}

//构建顺序表
void CreateList(int a[],int n){
int i;
for (i = 0; i < n; i++){
L->date[i] = a[i];
}
L->length = n;
}

//判断是否为空
bool isEmpty(){
return (L->length == 0);
}

//顺序表长度
int m_ListLength(){
return (L->length);
}

//输出表
void PrintList(){
if (!isEmpty()){
int i;
for (i = 0; i < L->length; i++){
cout << L->date[i] << ends;
}
cout << endl;
}
else{
cout << "It is empty." << endl;
}
}

//查找序号并返回对应值
int FindListElem(int i){
i--;
if (!isEmpty() && i >= 0 && i < L->length){
int j = 0;
while (j!=i){
j++;
}
return L->date[j];
}
else{
cout << "It is Empty or invalid location." << endl;
}
}

//按值查找并返回序号
int FindElem_by_Value(int value){
if (isEmpty()){
cout << "It is Empty." << endl;
return 0;
}
else{
int i = 0;
while (i >= 0 && i < L->length&&L->date[i] != value){
i++;
}
return i;
}
return 0;
}

//插入元素
void InsertElem(int i, int elem){
i--;
if (!isEmpty() && i >= 0 && i < L->length){
int j = L->length - 1;
while (j >= i){
L->date[j + 1] = L->date[j];
j--;
}
L->date[i] = elem;
L->length++;
}
else if (!isEmpty() && i >= L->length){
cout << "It is bigger than the length." << endl;
}
else if (isEmpty()){
cout << "It is empty,insert in the first place" << endl;
L->date[0] = elem;
L->length++;
}
else{
cout << "error" << endl;
}
}

//删除元素
int DeleteElem(int i){
int res = 0;
i--;
if (!isEmpty() && i >= 0 && i < L->length){
int j = 0;
while (j < i){
j++;
}
res = L->date[j];
while (j < L->length - 1){
L->date[j] = L->date[j + 1];
j++;
}
L->length--;
}
else if (!isEmpty() && i>L->length){
cout << "It is bigger than the length." << endl;
}
else{
cout << "it is empty or no such element." << endl;
}
return res;
}
private:

//定义节点
SqList_Node *L;

//获取顺序表
SqList_Node * GetList(){
return L;
}

//初始化
void InitSqList(){
L = (SqList_Node *)malloc(sizeof(SqList_Node));
L->length = 0;
}
};
int main()
{
SqList_Node *m_node;
SqList m_list(m_node);
int a[MAX_N], i, n;
cout << "Input list's length:" << ends;
cin >> n;
cout << "Input " << n << " elements:" << endl;
for (i = 0; i < n; i++)
cin >> a[i];
//创建表
m_list.CreateList(a, i);
//输出表
m_list.PrintList();
//是否为空,非空返回元素个数
if (m_list.isEmpty()){
cout << "It is empty." << endl;
}
else{
cout << "It is not empty." << endl;
cout << "It contains " << m_list.m_ListLength() << " elements." << endl;
}
//插入元素并输出
cout << "Insert a element,give the number and the element:" << ends;
cin >> i >> n;
m_list.InsertElem(i,n);
m_list.PrintList();
//删除元素并输出
cout << "Input the number that need delete:" << ends;
cin >> n;
m_list.DeleteElem(n);
m_list.PrintList();
return 0;
}


测试结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: