基于动态数组的列表实现

//-------------DList.h--------------
#include<iostream>

#ifndef DLIST
#define DLIST

typedef int ElementType;
class List
{
public:
	List(int maxSize=1024);
	~List();
	List(const List & origList);
	const List & operator=(const List & rightHandSide);
	bool empty() const;
	void insert(ElementType item,int pos);
	void erase(int pos);
	void display(ostream & out) const;
private:
	int mySize;
	int myCapacity;
	ElementType * myArray;
};
ostream & operator<< (ostream & out,const List & aList);
#endif

//--------------DList.cpp----------------
#include<cassert>
#include<new>
using namespace std;

#include"DList.h"

List::List(int maxSize)
:myArray(0),myCapacity(maxSize)
{
	myArray=new(nothrow) ElementType[maxSize];
	assert(myArray!=0);
}

List::~List()
{
	delete [] myArray;
}

List::List(const List & origList)
	:mySize(origList.mySize),myCapacity(origList.myCapacity)
{
	myArray=new (nothrow) ElementType[myCapacity];

	if(myArray!=0)
		for(int i=0;i<mySize;i++)
			myArray[i]=origList.myArray[i];
	else
	{
		cerr<<"***Inadequate memory to allocate storage for list***"
			<<endl;
			exit(1);
	}
}

const List & List::operator=(const List & rightHandSide)
{
	if(this!=&rightHandSide)
	{
		if(myCapacity!=rightHandSide.myCapacity)
		{
			delete [] myArray;
			myCapacity=rightHandSide.myCapacity;
			myArray=new(nothrow) ElementType[myCapacity];

			if(myArray==0)
			{
				cerr<<"***Inadequate memory to allocate stack***"
					<<endl;
					exit(1);
			}
		}
		mySize=rightHandSide.mySize;
		for(int i=0;i<mySize;i++)
			myArray[i]=rightHandSide.myArray[i];
	}
	return *this;
}

bool List::empty() const
{
	return mySize==0;
}

void List::display(ostream & out) const
{
	for(int i=0;i<mySize;i++)
		out<<myArray[i]<<" ";
}

ostream & operator<<(ostream & out,const List & aList)
{
	aList.display(out);
	return out;
}

void List::insert(ElementType item,int pos)
{
	if(mySize==myCapacity)
	{ 
		cerr<<"***No space for list element.***\n";
		exit(1);
	}
	if(pos<0||pos>mySize)
	{
		cerr<<"***Illegal location to insert.***\n";
		return;
	}
	for(int i=mySize;i>pos;i--)
		myArray[i]=myArray[i-1];
	myArray[pos]=item;
	mySize++;
}

void List::erase(int pos)
{
	if(mySize==0)
	{
		cerr<<"***List is empty.***\n";
		return;
	}
	if(pos<0||pos>=mySize)
	{
		cerr<<"***Illegal location to delete***.\n";
		return;
	}
	for(int i=pos;i<mySize;i++)
		myArray[i]=myArray[i+1];

	  mySize--;
}

//-------------main.cpp-----------------
#include<iostream>
using namespace std;

#include"DList.h"

int main()
{
	cout<<"What is the maximum size of lists you will need? ";
	int maxListSize;
	cin>>maxListSize;
	List aList,aLast2(maxListSize);
	/*
	//
	//
	                        */
     return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值