顺序表


  
  

一、实验目的

巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。

二、实验内容

建立一个有n个学生成绩的顺序表,n的大小由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。

三、算法实现

//顺序表 #include<iostream> using namespace std; class List{ public: List(int size); ~List(); void clearList(); bool ListEmpty(); int ListLength(); bool getElem(int i,int &e); int locateElem(int ); bool priorElem(int *currentElem,int *preElem); bool nextElem(int *currentElem,int *nextElem); void ListTraverse(); bool ListInsert(int i,int *E); bool ListDelete(int i,int *E); private: int *m_pList; int m_iSize; int m_iLength; }; List::List(int size){ m_iSize = size; m_pList = new int[m_iSize]; m_iLength = 0; } List::~List(){ delete []m_pList; m_pList = NULL; } void List::clearList(){ m_iLength = 0; } bool List::ListEmpty(){ return m_iLength==0?true:false; } int List::ListLength(){ return m_iLength; } bool List::getElem(int i , int &e){ if(i < 0|| i > m_iLength){ return false; } else{ e = m_pList[i]; return true; } } int List::locateElem(int e){ for(int i = 0;i < m_iLength;i++){ if(m_pList[i] == e){ return i; } } return -1; } bool List::priorElem(int *currentElem,int *preElem){ int index = locateElem(*currentElem); if(index == -1||index ==0){ return false; } else{ *preElem = m_pList[index-1]; return true; } } bool List::nextElem(int *currentElem,int *nextElem){ int index = locateElem(*currentElem); if(index == -1||index == m_iLength-1){ return false; } else{ *nextElem = m_pList[index+1]; return true; } } void List::ListTraverse(){ for(int i = 0;i < m_iLength;i++){ cout << m_pList[i] ; } cout << endl; } bool List::ListInsert(int i,int *E){ if(i < 0 || i > m_iLength){ return false; } for(int j = m_iLength;j >= i;j--){ m_pList[j+1] = m_pList[j]; } m_pList[i] = *E; m_iLength++; return true; } bool List::ListDelete(int i,int *E){ if(i < 0||i > m_iLength){ return false; } *E = m_pList[i]; for(int j = i + 1;j < m_iLength;j++){ m_pList[j-1] = m_pList[j]; } m_iLength--; return true; } int main(void){ List *p = new List(30); int e1 = 1; int e2 = 2; int e3 = 3; int e4 = 4; int e5 = 5; int e6 = 6; int e7 = 7; int e8 = 8; int e9 = 9; cout<<"插入数据:"<<endl; p->ListInsert(0,&e1); p->ListTraverse();//插入操作 p->ListInsert(1,&e2); p->ListTraverse();//在第二位后面插入数据 p->ListInsert(2,&e3); p->ListTraverse(); p->ListInsert(2,&e4); p->ListTraverse(); p->ListInsert(2,&e5); p->ListTraverse(); p->ListInsert(2,&e6); p->ListTraverse(); p->ListInsert(2,&e7); p->ListTraverse(); p->ListInsert(2,&e9); p->ListTraverse(); int E = 0; p->ListDelete(3,&E); //删除第四位数据 cout <<"第四位数据为:"<< E << endl; cout<<"删除后的表为:"; p->ListTraverse(); cout <<"表长为:"<< p->ListLength() << endl; p->nextElem(&e1, &E); cout <<"第二位元素的后驱为:"<< E << endl; p->priorElem(&e2, &E); cout <<"第三位元素的前驱为:"<< E << endl; cout<<"寻找e5元素的位置:"<< p->locateElem(e5) << endl; cout<<"此时表为:"; p->ListTraverse(); p->getElem(3,E); cout <<"第四位元素为:"<< E << endl; p->clearList(); if(p->ListEmpty()) { cout << "顺序表为空" << endl; }; p->ListTraverse(); delete p; p = NULL; }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值