首先给一下extendedchain和checkIndex的代码:
checkIndex:
template<class T>//判断索引是否有效
void chain<T>::checkIndex(int theIndex) const
{// Verify that theIndex is between 0 and listSize - 1.
if (theIndex < 0 || theIndex >= listSize)
{
ostringstream s;
cout<< "index = " << theIndex << " size = " << listSize<<endl;
}
}
extendedchain:
#include <string>
#include"chain.h"
#include "extendlinearList.h"
using namespace std;
template<class T>
class extendedChain : public extendedLinearList<T> , public chain<T>
{
public:
// constructor and copy constructor
extendedChain(int initialCapacity = 10) :
chain<T>(initialCapacity) {}
extendedChain(const extendedChain<T>& c) :
chain<T>(c) {}
// ADT methods
bool empty() const { return chain<T>::listSize == 0; }
int size() const { return chain<T>::listSize; }
T& get(int theIndex) const
{
return chain<T>::get(theIndex);
}
int indexOf(const T& theElement) const
{
return chain<T>::indexOf(theElement);
}
void erase(int theIndex);
void insert(int theIndex, const T& theElement);
void setSize(int theSize);
void clear()
{// Delete all nodes in chain.
while (chain<T>::firstNode != NULL)
{// delete chain<T>::firstNode
chainNode<T>* nextNode = chain<T>::firstNode->next;
delete chain<T>::firstNode;
chain<T>::firstNode = nextNode;
}
chain<T>::listSize = 0;
}
void push_back(const T& theElement);
void ceshi();
void set(int theIndex, T theElement) {
chain<T>::set(theIndex,theElement);
}
void output(ostream& out) const
{
chain<T>::output(out);
}
// additional method
void zero()
{
chain<T>::firstNode = NULL; chain<T>::listSize = 0;
}
void removeRange(int fromIndex, int toIndex);
protected:
chainNode<T>* lastNode; // pointer to last node in chain
};
template<class T>
void extendedChain<T>::erase(int theIndex)
{// Delete the element whose index is theIndex.
// Throw illegalIndex exception if no such element.
chain<T>::checkIndex(theIndex);
// valid index, locate node with element to delete
chainNode<T>* deleteNode;
if (theIndex == 0)
{// remove first node from chain
deleteNode = chain<T>::firstNode;
chain<T>::firstNode = chain<T>::firstNode->next;
}
else
{ // use p to get to predecessor of desired node
chainNode<T>* p = chain<T>::firstNode;
for (int i = 0; i < theIndex - 1; i++)
p = p->next;
deleteNode = p->next;
p->next = p->next->next; // remove deleteNode from chain
if (deleteNode == lastNode)
lastNode = p;
}
chain<T>::listSize--;
delete deleteNode;
}
template<class T>
void extendedChain<T>::insert(int theIndex, const T& theElement)
{// Insert theElement so that its index is theIndex.
if (theIndex < 0 || theIndex > chain<T>::listSize)
{// invalid index
ostringstream s;
cout<< "index = " << theIndex << " size = " << chain<T>::listSize<<endl;
}
if (theIndex == 0)
{// insert at front
chain<T>::firstNode = new chainNode<T>(theElement, chain<T>::firstNode);
if (chain<T>::listSize == 0)
lastNode = chain<T>::firstNode;
}
else
{ // find predecessor of new element
chainNode<T>* p = chain<T>::firstNode;
for (int i = 0; i < theIndex - 1; i++)
p = p->next;
// insert after p
p->next = new chainNode<T>(theElement, p->next);
if (chain<T>::listSize == theIndex)
lastNode = p->next;
}
chain<T>::listSize++;
}
template<class T>
void extendedChain<T>::setSize(int theSize)
{
chain<T>::setSize(theSize);
}
template<class T>
void extendedChain<T>::push_back(const T& theElement)
{// Insert theElement at the end of the chain.
chainNode<T>* newNode = new chainNode<T>(theElement, NULL);
if (chain<T>::firstNode == NULL)
// chain is empty
chain<T>::firstNode = lastNode = newNode;
else
{ // attach next to lastNode
lastNode->next = newNode;
lastNode = newNode;
}
chain<T>::listSize++;
}
然后是题目:
2:
template<class T>//设置链表的范围
inline void chain<T>::setSize(int theSize)
{
if (theSize < 0) {
cout << "the size is" << theSize << "must >0";
}
if (theSize < listSize) {
int i = listSize - theSize;
for (int j = 0; j != i+1; j++)
{
chainNode<T>* currntNode = firstNode->next;
delete firstNode;
firstNode = currntNode;
}
listSize = theSize;
}
}
3:
template<class T>//替换索引为theIndex的元素为theElement
void chain<T>::set(int theIndex, T theElement)
{
if (theIndex > listSize || theIndex < 0) {
cout << "theIndex is" << theIndex << "must meet the scope" << endl;
}
else {
chainNode<T>* temporaryNode = firstNode;
for (int i = 0; i != theIndex; i++) {
temporaryNode = temporaryNode->next;
}
temporaryNode->element = theElement;
}
}
4:
template<class T>
void chain<T>::removeRange(int fromIndex, int toIndex) {
int i = toIndex - fromIndex;
if (fromIndex<0 || toIndex>listSize) {
cout << "theIndex is must meet the scope" << endl;
}
else {
chainNode<T>* temporary1Node = firstNode;
if (fromIndex != 0) {
for (int i = 0; i != fromIndex - 1; i++) {
temporary1Node = temporary1Node->next;
}
chainNode<T>* temporaryNode = temporary1Node->next;
for (int j = 0; j != i + 1; j++) {
chainNode<T>* currntNode = temporaryNode->next;
delete temporaryNode;
temporaryNode = currntNode;
}
}
else {
chainNode<T>* temporaryNode = firstNode;
for (int j = 0; j != i + 1; j++) {
chainNode<T>* currntNode = temporaryNode->next;
delete temporaryNode;
temporaryNode = currntNode;
}
firstNode = temporaryNode;
}
listSize = listSize - i;
listSize = listSize - 1;
}
}
5:这个题目不能用数组,因为链表的值不确定
int chain<T>::lastIndexOf(T & theElement)
{
vector<int> v1;
int i = 0;
chainNode<T>* temporaryNode = firstNode;
for (int j = 0; j != listSize; j++) {
if (temporaryNode->element == theElement) {
cout << 10086 << endl;
v1.push_back(j);
i++;
cout << v1[0] << endl;
}
temporaryNode = temporaryNode -> next;
}
if (i == 0) {
return -1;
}
else {
return v1[i-1];
}
}
6:
template<class T>
T & chain<T>::operator[](size_t theIndex)
{
if (theIndex <0 || theIndex>listSize - 1) {
ostringstream s;
cout<< "index = " << theIndex << " size = " << listSize;
exit;
}
else {
chainNode<T>* temporaryNode = firstNode;
for (int i = 0; i != theIndex; i++) {
temporaryNode = temporaryNode->next;
}
return temporaryNode->element;
}
}
:7:
bool operator==(const chain<T>& b) {
if (listSize == b.listSize) {
int j = 0;
chainNode<T>* bNode = b.firstNode;
chainNode<T>* aNode = firstNode;
for (int i = 0; i != listSize; i++) {
if (aNode->element == bNode->element) {
aNode = aNode->next;
bNode = bNode->next;
j++;
}
else {
return false;
break;
j--;
}
}
if (j == listSize) {
return true;
}
}
else {
return false;
}
}
8:
bool operator!=(const chain<T>& b) {
if (*this == b) {
return false;
}
else {
return true;
}
}
9:
bool operator<(const chain<T>& b) {
if (listSize == b.listSize) {
int j = 0;
chainNode<T>* bNode = b.firstNode;
chainNode<T>* aNode = firstNode;
for (int i = 0; i != listSize; i++) {
if (aNode->element<bNode->element) {
aNode = aNode->next;
bNode = bNode->next;
j++;
}
else {
return false;
break;
j--;
}
}
if (j == listSize) {
return true;
}
}
else {
return false;
}
}
10:
//替换thechain与this
template<class T>
void chain<T>::swap(chain<T>& theChain)
{
chainNode<T>* aNode;
aNode = firstNode;
firstNode = theChain.firstNode;
theChain.firstNode = aNode;
int i = theChain.listSize;
theChain.listSize = listSize;
listSize = i;
}