线性表- 顺序表

线性表的ADT

List.h

template<class Elem> 
class List
{
public:
	virtual void clear() = 0;

	virtual bool insert(const Elem&) = 0;

	virtual bool append(const Elem&) = 0;

	virtual bool remove(Elem&) = 0;

	virtual void setStart() = 0;

	virtual void setEnd() = 0;

	virtual void prev() = 0;

	virtual void next() = 0;

	virtual int leftLength() const = 0;

	virtual int rightLength() const = 0;

	virtual bool setPos(int pos) = 0;

	virtual bool getValue(Elem&) const = 0;

	virtual void print() const = 0;
};


顺序表的事先声明 & 没有定义成内嵌的顺序表成员函数的实现

AList.h

#include "stdafx.h"
#include "List.h"

#define defaultListSize 255 

template <class Elem>
class AList : public List<Elem>
{
private:
	int maxSize;
	int listSize;
	int fence;
	Elem* listArray;

public:
	AList(int size = defaultListSize)
	{
		maxSize = size;
		listSize = fence = 0;
		listArray  = new Elem[maxSize];
	}

	~AList()
	{
		delete[] listArray;
	}

	void clear();

	bool insert(const Elem&);

	bool append(const Elem&);

	bool remove(Elem&);

	void setStart();

	void setEnd();

	void prev();

	void next();

	int leftLength() const;

	int rightLength() const;

	bool setPos(int pos);

	bool getValue(Elem& it) const;

	void print() const;
};


template <class Elem>        
void AList<Elem>::clear()
{
	delete[] listArray;
	listSize = fence = 0;
	listArray = new Elem[maxSize];
}
	
template <class Elem>                           // insert at front of right partition
bool AList<Elem>::insert(const Elem& item)
{
	if(listSize == maxSize) return false;
	for(int i = listSize; i > fence; i--)
	{
		listArray[i] = listArray[i - 1];
	}
	listArray[fence] = item;
	listSize++;
	return true;
}

template <class Elem>  
bool  AList<Elem>::append(const Elem& item)     //在表尾插入元素  
{  
    if(listSize == maxSize)  
        return false;  
    listArray[listSize++]=item;  
    return true;  
}

template < class Elem>
bool AList<Elem>::remove(Elem& it)
{
	if(rightLength() == 0) return false;
	it = listArray[fence];
	for(int i = fence; i < listSize - 1; i++)
	{
		listArray[i] = listArray[i + 1];
	}
	listSize--;
	return true;
}
template < class Elem>
void AList<Elem>::setStart()
{
	fence = 0;
}
template < class Elem>
void AList<Elem>::setEnd()
{
	fence = listSize;
}
template < class Elem>
void AList<Elem>::prev()
{
	if(fence != 0) fence--;
}
template < class Elem>
void AList<Elem>::next()
{
	if(fence <= listSize) fence++;
}
template < class Elem>
int AList<Elem>::leftLength() const
{
	return fence;
}
template < class Elem>
int AList<Elem>::rightLength() const
{
	return listSize - fence;
}
template < class Elem>
bool AList<Elem>::setPos(int pos)
{
	if((pos >= 0) && (pos <= listSize)) fence = pos;
	return (pos >= 0) && (pos <= listSize);
}

template < class Elem>
bool AList<Elem>::getValue(Elem& it) const
{
	if(rightLength() == 0) return false;
	else
	{
		it = listArray[fence];
		return true;
	}
}
///
// 定义模板类,实现部分和声明部分都要放在头文件(*.h)里,不能放在cpp文件里。

template < class Elem>
void AList<Elem>::print() const
{
	int temp = 0;
	cout << " < ";
	while(temp < fence) 
	{
		cout << listArray[temp++] << " ";
	}
	cout << " | ";
	while(temp < listSize)
	{
		cout << listArray[temp++] << " ";
	}
	cout << " > \n";
}

注意:一开始,我把 没有定义成内嵌的顺序表成员函数的实现 写在了AList.cpp文件里,一直编译通过不了。事实上,这样是不对的,定义模板类,实现部分和声明部分都要放在头文件(*.h)里,不能放在cpp文件里。

http://blog.csdn.net/ljfxmf/article/details/2510786

主函数

Test.cpp

#include "stdafx.h"
#include <iostream>
#include "AList.h"

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	AList<int> a;
	a.append(1);
	a.append(10);
	a.append(20);
	a.append(30);
	a.append(60);
	cout << "content:" << endl;
	a.print();
	cout << endl;
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值