求救 数据结构的简单问题......
将1-10放进L1中,然后把L1中的所有元素复制到L2,并且输出L2
分别用线性表和单链表实现,新手上路,只能对着书的代码完成一点点,线性表的代码如下:
#include<iostream.h>
//#include<dslib.h>
const int maxSize=100;
typedef int ListElementType;
class List
{
public:
List();
void insert(const ListElementType & elem);
bool first(ListElementType & elem);
bool next(ListElementType & elem);
private:
ListElementType listArray[maxSize];
int numberOfElements;
int currentPosition;
};
List::List()
{
numberOfElements=0;
currentPosition=-1;
}
void List::insert(const ListElementType & elem)
{
//assert(numberOfElements<maxSize);
listArray[numberOfElements]=elem;
numberOfElements++;
}
bool List::first(ListElementType & elem)
{
if(numberOfElements==0)
return false;
else
{
currentPosition=0;
elem=listArray[currentPosition];
return true;
}
}
bool List::next(ListElementType & elem)
{
if(currentPosition>=numberOfElements-1)
return false;
else
{
currentPosition++;
elem=listArray[currentPosition];
return true;
}
}
void main()
{
List L1,L2;
ListElementType elem;
bool notEmpty(L1.first(elem));
for(int i=1;i<=10;i++)
{
L1.insert(i);
}
while(notEmpty)
{
cout<<elem;
notEmpty=L1.next(elem);
}
}
//接下来就....,请各位兄弟改正完成,还有用单链表怎么实现呢?谢谢!!!
//我的可用分好像所剩无几了...大家将就点做一下吧...谢谢
[解决办法]
线性表是数组或容器中的vector,deque,下面是线性表实现的:
void copyarray(){ int a1[10],a2[10]; for(int i = 0; i < 10;++i) a1[i] = i + 1; for(int i = 0; i < 10; ++i) a2[i] = a1[i]; for(int i = 0; i < 10; ++i) cout << a2[i] << " ";}
[解决办法]
貌似有好几个错误的,但不知道怎么改!
[解决办法]
一个人的头像要被和谐了