//-------------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;
}