第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

順序表中插入元素不成功怎么回事,代碼沒有錯誤

#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


正在回答

1 回答

ListInsert這個方法中if(i<0||i>=m_iLength) { return false;} 這個判斷錯了。

一開始m_iLength=0; 你執(zhí)行 list->ListInsert(0,&e1);時,傳入i=0 就return false;了,當(dāng)然也就沒法正確插入元素。

要理解m_iLength這個變量其實總是指向于數(shù)組最后一個元素的下一個元素。插入時是允許插入在該位置上的。刪除時才不能刪除該位置的元素。


應(yīng)該改成if(i<0 || i > m_iLength) {return false;}

順便一說,ListDelete這個方法相應(yīng)的地方也是錯的。

2 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消
數(shù)據(jù)結(jié)構(gòu)探險之線性表篇
  • 參與學(xué)習(xí)       57609    人
  • 解答問題       264    個

線性表的主體順序表和鏈表,讓學(xué)員能夠?qū)⒅R融會貫通學(xué)以致用

進(jìn)入課程

順序表中插入元素不成功怎么回事,代碼沒有錯誤

我要回答 關(guān)注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號