首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

大家帮看看这个顺序表的insert和display函数好吗?在display的时候好像输出的是数组的地址.挺急得,8号要交的作业,多谢了

2012-03-02 
大家帮看看这个顺序表的insert和display函数好吗?在display的时候好像输出的是数组的地址.挺急得,8号要交

大家帮看看这个顺序表的insert和display函数好吗?在display的时候好像输出的是数组的地址.挺急得,8号要交的作业,谢谢了。
/*--基于动态数组的顺序表的实现------------------------------------------
This headfile include the List class declaration & definition
-----------------------------------*/
#include<cassert>

template<typename ElementType>
class List
{
public:
/****Function Members****/
List(int maxSize=1024);
~List();
List(const List & origList);
bool empty() const;
  void insert(ElementType item,int pos);
void erase(int pos);
void display() const;
void traverse();
  int length();
  ElementType getItem(int);
int locateElem(ElementType);
ElementType PriorElem(ElementType);
ElementType NextElem(ElementType);

private:
/*******data member*******/
int mySize; //the size of the currenting list
int myCapacity; //the capacity of the array
ElementType * myArray; //the point pointing the animate array
};


template <typename ElementType>
List<ElementType>::List(int maxSize)
{
mySize=0;
myCapacity=maxSize;
myArray=new(nothrow) ElementType[maxSize];
assert(myArray!=0);
}

template<typename ElementType>
List<ElementType>::~List()
{
delete [] myArray;
}

template<typename ElementType>
List<ElementType>::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
{
cout<<"***Inadequate memory to allocate storage for list ***\n";
exit(1);
}
}

template<typename ElementType>
bool List<ElementType>::empty() const
/*---------------------------------------
Examine whether the list is empty
precondition:list exits
postconditon:if the list is empty,return true;else return false
---------------------------------------*/
{
return mySize==0;
}

template<typename ElementType>
void List<ElementType>::insert(ElementType item,int pos)
/*-----------------------------------------
Insert a element into an appointed position of the list
precondition:list exits;pos is an positive integer;list is not full
postcondition:if the precondition has not been satisfied,fail;else 
the item has been inserted into the right position of the list
-----------------------------------------*/
{
if(mySize==myCapacity)
{
cout<<"***No space for list element--terminating execution***\n";
exit(1);
} elseif(pos<0||pos>=myCapacity)
{
cout<<"***Illegeal location to insert--"<<pos<<"List unchanged.***\n";
return;
}else
{
for(int i=mySize;i>pos;i--)
{
myArray[i]=myArray[i-1];
}
myArray[pos]=item;
mySize++;
}
}

template<typename ElementType>
void List<ElementType>::erase(int pos)
/*-------------------------------------------
Erase the element in the appointed position of the list
preconditon:list exits;pos is an positive interger;list is not empty
postconditon:the element in the appointed position has been erased
-----------------------------------------*/
{
if(mySize==0)
{
cout<<"***The list is empty--terminating execution***\n";


return 1;
}
if(pos<0||pos>=mySize)
{
cout<<"***Illegal location to erase--"<<pos<<"List unchanged.***\n";
return;
}
for(int i=pos;i<mySize-1;i++)
{
myArray[i]=myArray[i+1];
}
}

template <typename ElementType>
void List<ElementType>::display() const
/*-------------------------------------------
cout the elements of the array
precondition:the list exits 
postcondition:if the list is empty,break;else cout the elements of the list
-------------------------------------------*/
{
for(int i=0;i<mySize;i++)
{
cout<<myArray[i]+' '<<endl;
}
}

template <typename ElementType>
void List<ElementType>::traverse()
/*traverse the list
precondition:the list exits & not empty
postcondition:every element of the list has been visited,if visits() fails,the operation fails.
  */
{
for(int i=0;i<mySize;i++)
{
if(!myArray[i])
{
return 1;
cout<<"Traverse failed.";
}
}
}

template <typename ElementType>
int List<ElementType>::length()
/*return the length of the list
precondition:list exits
postcondition:the length of the list has been returned */
{
return mySize;
}

template <typename ElementType>
ElementType List<ElementType>::getItem(int pos)
/*return an element in a given position
precondition:list exits;pos is an positive interger;pos<=mySize
postcondition:if precondition is true,the item has been return;else,break.*/
{
if(pos>0 && pos<=mySize)
return myArray[pos];
else{ 
cout<<"invalid position value.Break!"<<endl;
exit(1);
}
}

template <typename ElementType>
int List<ElementType>::locateElem(ElementType e)
/*return the location of an given Element
  precondition:e is of ElementType;list exits
  postcondition:if e is an element in the list,return its position in the array.If e isn't an element of the list,return -1;
For example,if e is the fifth of the list,the function will return 5.*/
{
int i=0;
while(myArray[i]!=e)
{
i++;
}
if(i<mySize-1)
return i+1;
else if(i=mySize)
{
if(myArray[i]=e)
return i;
}else return -1;
}

template <typename ElementType>
ElementType List<ElementType>::PriorElem(ElementType e)
/*return the prior element
preconditon:list exits;e is an element of the list;e is not the first element
postcondition:the prior element has been returned*/
{

int a=this.locateElem(e);
if(a=-1)
cout<<"It's not the list's element."<<endl;
else if(a=1)
cout<<"It's the first element which has no prior element."<<endl;
else return myArray[a-1]; 
}

template <typename ElementType>
ElementType List<ElementType>::NextElem(ElementType e)
/*return the next element
precondition:list exits;e is an element of the list;e is not the last element
postcondition:the next element has been returned*/
{
int b=this.locateElem(e);
if(b=-1)
cout<<"It's not the list's element."<<endl;
else if (b=mySize)
cout<<"It's the last element which has no next element."<<endl;
else return myArray[b-1];
}
-----------------------------------------------
#include<iostream>

#include "List.h"

using namespace std;

int main()


{
cout<<"Welcome to have a try to use List & Chain"<<endl;
cout<<"We create one List object declared as list & a Chain object declared as chain."<<endl;
cout<<"You are suggested here to experience the List Class & the Chain Class."<<endl;
cout<<"During this experience,we dealing with int data type."<<endl;
cout<<"First,you are provided with the List class."<<endl;
List<int> list;
cout<<"We create an empty list,the size of it is ";
cout<<list.length()<<endl;
cout<<"Please enter an interger,we will put it in the first place of the list."<<endl;
int a;
cin>>a;
list.insert(a,1);
cout<<"Now we call the length() function to see the size of the list."<<endl;
cout<<list.length()<<endl;
cout<<"Now we want you to enter several intergers to build a list.A -1 represents the end of the input."<<endl;
cout<<"Please be well known of the sequence is according to the array's index."<<endl;
cin>>a;
int i=1;
while(a!=-1)
{
list.insert(a,i);
i++;
cin>>a;
}
cout<<"Ok.Let's have a look at the size of list & display each data.We'll call two functions:length() & display()."<<endl;
cout<<"The length of the list is "<<list.length()<<endl;
list.display();

return 0;
}

在insert几个数之后的display输出的并不是刚输入的数。

[解决办法]

C/C++ code
//display函数中        cout <<myArray[i]+ ' ' <<endl;//应为:        cout <<myArray[i] << ' ' <<endl; 

热点排行