线性表的简单实现

 

Linearlist.h:

#include
using namespace std;
const int maxLen=20;

struct Student
{
string stuName;
int stuId;
int age;
int score;
};

class List
{
private:
int listLen;
public:
bool IsEmpty() {return listLen == 1;}
bool IsFull() {return listLen == maxLen;}
void Insert(List &list, Student stud, int pos);
void Delete(List &list, int pos);
List();
Student stu[maxLen];
};


 

Liearlist.cpp:

#include "List.h"
List::List()
{
this->listLen = 1;
}
void List::Insert(List &list, Student stud, int pos)
{
if(listLen < maxLen && pos >= 0 && pos < listLen)
{
for(int i = listLen-1; i >= pos; i--)
{
stu[i+1] = stu[i];
}
stu[pos] = stud;
}
listLen++;
}

void List::Delete(List &list, int pos)
{
if(listLen > 0 && pos >= 0 && pos < listLen)
{
for(int i = pos; i < listLen; i++)
{
stu[i] = stu[i+1];
}
}
listLen--;
}

 

Testlist:

#include <iostream>
#include "List.h"
using namespace std;

int main()
{
List list;
Student student,student1,student2,student3;
student.age = 20;
student.score = 80;
student.stuId = 1;
student.stuName = "zhang";
student1.age = 21;
student1.score = 80;
student1.stuId = 1;
student1.stuName = "li";
student2.age = 22;
student2.score = 80;
student2.stuId = 1;
student2.stuName = "wang";
student3.age = 23;
student3.score = 80;
student3.stuId = 1;
student3.stuName = "zhao";
list.Insert(list, student, 0);
list.Insert(list, student2, 1);
list.Insert(list, student3, 2);
list.Insert(list, student1, 1);
list.Delete(list, 2);

cout << list.stu[0].age << " " << list.stu[1].age << " " << list.stu[2].age << " " << list.stu[3].age<< endl;

cout << list.IsFull() << " " << list.IsEmpty() << endl;

return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值