有关数据结构与算法线性表的所有操作,初始化,插入,删除,查找

有关数据结构与算法线性表的所有操作,初始化,插入,删除,查找

课堂笔记

  1. 插入
    过程分析:①将插入点以后(包括插入点)到表尾的数据向后移动一位,
    ②要用插入的新元素赋值给插入点(插入位置)
    ③表长加1
    注意事项:①插入点进行判断:不能小于0,不能够大于表长
    ②表长不能小于我们申请的内存空间-1
    插入数据后顺序表发生的变化:①表的顺序发生变化
    ②表长发生变化

  2. 删除
    操作引起的变化:①顺序表元素少了一个
    ②顺序表长度减一
    ③顺序表中删除点的后继元素从删除点的直接后继开始依次位置前移
    数据移动循环操作:从删除点开始,依次将后继元素前移

#include <iostream>
#include <cstdlib> //exit的头文件
#define MAXSIZE 100
using namespace std;

// 构建结构体
struct Student{
   int StuID;
   float score;
   int height;
  };
struct SqListStu{
   Student *ElemStu;
   int length;
  };
  
  //顺序表的初始化
void InitSqlist(SqListStu &SqLExp){
  SqLExp.ElemStu=new Student[MAXSIZE];
  if(SqLExp.ElemStu == NULL){
        exit(0);
     }
  SqLExp.length = 0;
  };
  
  //顺序表的插入
void InsertSqlist(SqListStu &SqLExp,Student InsertData,int InsertPos){ // 插入所需的顺序表,插入的数据,以及往哪插入
if(InsertPos<0 || InsertPos > SqLExp.length) // 判断插入的位置是否合理
  exit(0);
if(SqLExp.length > MAXSIZE-1) // 判断线性表是否溢出
  exit(0);
for(int j=SqLExp.length;j>InsertPos;j--){ // 将插入点以后的位置全部往后移
SqLExp.ElemStu[j+1] = SqLExp.ElemStu[j];
    }
SqLExp.ElemStu[InsertPos] = InsertData;
SqLExp.length ++;
 };

//顺序的删除
 int DeleteElem (SqListStu &SqLExp, Student &ELemToDelete, int deleteLoc){
    if (deleteLoc < 1 || deleteLoc > SqLExp.length) return 0;
    if (SqLExp.ElemStu == NULL) return 0;
    ELemToDelete = SqLExp.ElemStu[deleteLoc]; //赋值
    for(int i = deleteLoc; i < SqLExp.length; i++){
        SqLExp.ElemStu[i] = SqLExp.ElemStu[i+1];
    }
    SqLExp.length --;
    return 1;
}

//查找
int findSqlist(SqListStu &SqLExp, int ID){
    for(int i=0;i<=SqLExp.length;i++){
        if(ID==SqLExp.ElemStu[i].StuID){
            return (i);
        }

    }
    return 9;
}

// 主函数
int main(){
    Student zhangsan;
    zhangsan.StuID = 001;
    zhangsan.score = 80;
    zhangsan.height = 180;
    Student lisi;
    lisi.StuID = 002;
    lisi.score = 90;
    lisi.height = 185;
    Student wangwu;
    wangwu.StuID = 003;
    wangwu.score = 95;
    wangwu.height = 180;
    Student newStu;
    cout << "*********inital test************" << endl;
    SqListStu SqStu;
    InitSqlist(SqStu);
    cout << SqStu.length << endl;

    cout << "*********insert test************" << endl;
    InsertSqlist(SqStu,zhangsan,SqStu.length);
    InsertSqlist(SqStu,lisi,SqStu.length);
    InsertSqlist(SqStu,wangwu,SqStu.length);
    cout << SqStu.length << endl;
    cout << SqStu.ElemStu[SqStu.length-1].score << endl;

    cout << "*********delete test************" << endl;
    DeleteElem(SqStu,newStu,SqStu.length-1);
    cout << SqStu.ElemStu[SqStu.length-1].score << endl;
    cout << SqStu.length << endl;

    cout << "*********find test************" << endl;
    findSqlist(SqStu,001);
    int k = findSqlist(SqStu,001);
    cout << k <<endl;

    cout << "*********cout test************" << endl;  // 顺序表的输出,倒序输出
	for( int counter = SqStu.length;counter >0;counter--){
    cout << SqStu.ElemStu[SqStu.length-1].StuID<<"       "<< SqStu.ElemStu[SqStu.length-1].score<<"       "<< SqStu.ElemStu[SqStu.length-1].height <<endl;
    SqStu.length --;
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yeopeq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值