未完成,待细学
List.h:
//
#ifndef INC_0131_LIST_H
#define INC_0131_LIST_H
class List
{
public:
List(int size);
~List();
void ClearList();//清空线性表不等于释放内存
bool ListEmpty();
int ListLength();
bool GetElem(int i, int *e);//获取指定元素
int LocateElem(int *e);//寻找第一个满足e的元素的位序
bool PriorElem(int *currentElem,int *preElem);//获取指定元素的前驱
bool NextElem(int *currentElem,int *nextElem);//获取指定元素的后继
bool ListInsert(int i,int *e);
bool ListDelete(int i,int *e);
void ListTraverse();//遍历链表元素
private:
int *m_pList;
int m_iSize;
int m_iLength;
};
#endif //INC_0131_LIST_HList.cpp:
//n 2020-02-06.
//
#include "List.h"
#include <iostream>
using namespace std;
//构造函数
List::List(int size) {
m_iSize = size;
//c++中分配内存,确定此线性表的容量:
m_pList = new int[size];
m_iLength = 0;
}
//析构函数:作用主要是将在构造函数中申请的内存释放掉
List::~List() {
delete []m_pList;
m_pList = NULL;//iSize置0无所谓,因为内存被释放后该对象也不存在了
}
void List::ClearList(){
m_iLength = 0;
}
bool List::ListEmpty(){
if(m_iLength == 0){
return true;
} else{
return false;
}
//也可:
//reture m_iLength == 0 ? true :false;
}
int List::ListLength(){
return m_iLength;
}
bool List::GetElem(int i,int *e){
if(i < 0 || i >= m_iSize){
return false;
}
*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 tmp = LocateElem(currentElem);
//不是先判断是否是第一个元素,先判断是否找得到这个数
if(tmp == -1){
return false;
} else{
if(tmp == 0){
return false;
} else{
*preElem = m_pList[tmp - 1];//注意是*preElem
return true;
}
}
}
bool List::NextElem(int *currentElem,int *nextElem){
//判断是否存在这样一个元素
int tmp = LocateElem(currentElem);
if(tmp == -1){
return false;
} else{
if(tmp == m_iLength-1){
return false;
} else{
*nextElem = m_pList[tmp+1];
return true;
}
}
}
//自己写的,有错误
//bool List::PriorElem(int *currentElem,int *preElem){
// //先判断是否是第一个元素
// int tmp = LocateElem(currentElem);//注意用之前写好的函数
// if(tmp == 1){
// return false;
// } else {
// preElem = m_pList[tmp - 1];
// return true;
// }
//}
//
//bool List::NextElem(int *currentElem,int *nextElem){
// //先判断是否为最后一个元素
// int tmp = LocateElem(currentElem);
// if(tmp == m_iLength-1){
// return false;
// } else{
// nextElem = m_pList[tmp+1];
// return true;
// }
//}
void List::ListTraverse(){
for(int i = 0; i < m_iLength;i++){
cout << m_pList[i] <<endl;
}
}
bool List::ListInsert(int i,int *e){
//先判断是否超过size了
if(m_iSize == m_iLength || i < 0 || i > m_iLength){//已经满了或i不符合标准
return false;
} else{
for(int k = m_iLength - 1; k >= i; k--){
m_pList[k+1] = m_pList[k];
}
m_pList[i] = *e;
m_iLength++;//容易忘
return true;//记得返回正确
}
}
bool List::ListDelete(int i,int *e){
//先判断i是否合法
if(i < 0 || i >= m_iLength){
return false;
}
*e = m_pList[i];
for(int k = i+1;k < m_iLength;k++){
m_pList[k-1] = m_pList[k];
}
m_iLength--;
return true;
}main.cpp:
#include <iostream>
#include <stdlib.h>
#include "List.h"
using namespace std;
int main(){
int e1 = 1;
int e2 = 2;
int e3 = 3;
int e4 = 4;
int e5 = 5;
int e6 = 6;
int tmp = 1;
List *list1 = new List(10);
cout << "length:" << list1->ListLength() <<endl;
list1->ListInsert(0,&e1);
cout << "length:" << list1->ListLength() <<endl;
list1->ListInsert(1,&e2);
list1->ListInsert(2,&e3);
list1->ListInsert(3,&e4);
list1->ListInsert(4,&e5);
list1->ListInsert(5,&e6);
list1->ListDelete(0,&tmp);
cout << "#" << tmp <<endl;
if(!list1->ListEmpty()){
cout << "not empty" <<endl;
}
list1->ClearList();
list1->ListTraverse();
list1->ListInsert(0,&e1);
list1->ListInsert(1,&e2);
list1->ListInsert(2,&e3);
list1->ListInsert(3,&e4);
list1->ListInsert(4,&e5);
list1->ListInsert(5,&e6);
list1->GetElem(4,&tmp);
cout << "tmp:" << tmp <<endl;
cout << list1->LocateElem(&tmp) << endl;//注意是传地址
list1->PriorElem(&e4,&tmp);
cout<<"前驱:" << tmp <<endl;
list1->NextElem(&e4,&tmp);
cout<<"后继:" << tmp <<endl;
delete list1;
list1 = NULL;
return 0;
}
==运算符重载
coordinate.cpp
coordinate.h