栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++实现顺序表

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++实现顺序表

数据结构和算法C++一线性表)
  • 顺序表
    • 实现

顺序表

概念啥的去看B站王卓数据结构预算法

实现

1.普通实现

#define MAXSIZE 100
typedef int  ElemType;


typedef struct {
	ElemType* elem;
	int length;
}SqList;


//初始化
bool InitList(SqList& L) {//构造空的顺序表L
	L.elem = new ElemType[MAXSIZE];
	if (!L.elem)//异常处理,分配失败是L.elem是空值NULL
	{
		return false;
	}
	L.length = 0;
	return true;
}

//销毁线性表
void DestroyList(SqList& L) {
	if (L.elem) {
		delete L.elem;//释放内存空间
	}
}
//清空线性表
void ClearList(SqList& L) {
	L.length = 0;//长度设置为0
}
//返回线性表的长度
int GetLength(SqList L) {
	return L.length;
}
//判断是否为空
bool IsEmpty(SqList L) {
	if (L.length == 0) {
		return true;
	}
	else {
		return false;
	}
}
//取值
bool GetElem(SqList L, int i, ElemType& e) {
	if (i<1 || i>L.length) {
		return false;//序号不合法
	}
	e = L.elem[i - 1];
	return true;
}
//查找
int LocateElem(SqList L, ElemType e) {
	for (int i = 0; i < L.length; i++) {
		if (L.elem[i] == e) {
			return i + 1;//查到返回序号
		}
		return -1;//未找到返回-1
	}
}
//插入
bool ListInsert(SqList& L, int i, ElemType e) {
	if (i<1 || i>L.length + 1)return false;//序号不合法
	if (L.length == MAXSIZE)return false;//空间已满
	for (int j = L.length - 1; j >= i - 1; j--) {//把第i(序号为i-1)个开始的元素都往后移
		L.elem[j + 1] = L.elem[j];
	}
	L.elem[i-1] = e;
	L.length++;
	return true;
}
//删除元素
bool ListDelete(SqList& L, int i, ElemType& e) {
	if (i<1 || i>L.length)return false;//序号不合法
	e = L.elem[i - 1];//导出删除的元素
	for (int j = i - 1; j < L.length-1; j++) {//把第i(序号i-1)个之后的元素前移
		L.elem[j] = L.elem[j + 1];
	}
	L.length--;
	return false;
}
  1. 用类实现
  • SqList.h
#pragma once
#include
typedef int ElemType;
using namespace std;

class SqList
{
private:
    int myLength;//保存表长
    int mySize;//最大容量
    ElemType *myElem;
public:
    SqList(int size);
    SqList( SqList  &L);
    ~SqList();
    void PrintSqList();//打印输出
    void CreateSqList();//创建线性表
    ElemType GetElem(int i) ;//取值,取位序为i的元素,下标为i-1
    int FindElem(ElemType elem);//查找值为elem的位序i
    void InsertElem(int i, ElemType elem);//在指定位序插入元素
    void DeleteElem(int i);//删除指定位序的元素
    int GetLength () ;//返回表长 
    int GetSize();//返回容量
};
  • SqList.cpp
#include"SqList.h"

//构造函数
SqList::SqList(int size){
    this->myLength = 0;
    this->mySize = size;
    this->myElem = new ElemType[this->mySize];
    this->CreateSqList();
}
SqList::SqList(  SqList  &L){
    this->myLength = L.GetLength();
    this->mySize = L.GetSize();
    this->myElem = new ElemType[this->mySize];
    for(int i = 1; i < this->myLength+1; i++){
        this->myElem[i-1] = L.GetElem(i);
    }
}
//析构函数
SqList::~SqList(){
    delete [] this->myElem;
}
//打印输出
void SqList::PrintSqList(){
    for(int i = 0; i < this->myLength; i++){
        cout << this->myElem[i] <<" ";
    }
    cout << endl;
}
//创建线性表
void SqList::CreateSqList(){
    cout << "请构造一个线性表,输入-1时结束输入" << endl;
    ElemType x;
    while(true){
        if(this->myLength==this->mySize){
            cout << "顺序表已满" << endl;
            break;
        }
        cin >> x;
        if(x==-1)break;
        this->myElem[this->myLength++] = x;
    }
}
//取值
ElemType SqList::GetElem(int i){
    if(i >= 1 && i <= this->myLength){
       return this->myElem[i-1];
    }
    else{
        cout << "位序不合法" << endl;
        return -1;//不合法,返回数组中不可能存在的元素
    }
}
//查找
int SqList::FindElem(ElemType elem){
    for(int i = 0; i < this->myLength; i++){
        if(this->myElem[i]==elem){
            return i+1;
        }
    }
    return -1;//元素存在返回-1
}
//插入
void SqList::InsertElem(int i, ElemType elem){
    if (i < 1 || i > this->myLength + 1){//合法位置是1到表长+1
        cout << "位序不合法" << endl;
        return;
    }
    for (int j = this->myLength - 1; j >= i - 1; j--){
        this->myElem[j + 1] = this->myElem[j];
    }
    this->myElem[i - 1] = elem;
    this->myLength++;
}
//删除
void SqList::DeleteElem(int i){
    if (i < 1 || i > this->myLength){
        cout << "位序不合法" << endl;
        return;
    }
    for (int j = i - 1;j < this->myLength - 1; j++ ){
        this->myElem[j] = this->myElem[j + 1];
    }
    this->myLength--;
}
//表长
int SqList::GetLength(){
    return this->myLength;
}
//容量
int SqList::GetSize(){
    return this->mySize;
}

-main.cpp

int main(){
    SqList L(100);
    L.PrintSqList();
    //取值
    cout << " 取值" <
        cout << "元素不存在" << endl;
    }
    else{
        cout << elem << "的序号为" << endl;
    }
    cout << "表长为:" << L.GetLength() <
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/948006.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号