C++编写顺序表无代码错误,可是就是插入不了数据元素

#include<stdlib.h>

#include"List.h"

#include<iostream>

using namespace std;

int  main(void)

{

List *list=new List(10);

int e1=5;

int e2=6;

list->ListInsert(0,&e1);

list->ListInsert(1,&e2);

list->ListTraverse();


    system("pause");

return 0;

}

#include"List.h"

#include<iostream>

using namespace std;


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_iSize)

{

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 temp=LocateElem(currentElem);

if(temp==-1)

{

return false;

}

else

{

if(temp==0)

{

return false;

}

else

{

*preElem=m_pList[temp-1];

return true;

}

}

}


bool List::NextElem(int *currentElem,int *nextElem)

{

int temp=LocateElem(currentElem);

if(temp==-1)

{

return false;

}

else

{

if(temp==m_iLength-1)

{

return false;

}

else

{

*nextElem=m_pList[temp+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)

{

if(i<0||i>=m_iLength)

{

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)

{

if(i<0||i>m_iLength)

{

return false;

}

else

{

*e=m_pList[i];

for(int j=i+1;j<m_iLength;j++)

{

m_pList[j-1]=m_pList[j];

}

m_iLength--;

return true;

}

}

#ifndef LIST_H

#define 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);

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;


};




#endif


junnaxing
浏览 1075回答 1
1回答

慕仔3118017

List::ListInsert,if(i<0||i>m_iLength),这条语句一开始就成立,就返回False了,应该是i<0||i>m_iLength||m_iLenth==m_size吧??
打开App,查看更多内容
随时随地看视频慕课网APP