arrlist

/* 1. 插入删除等位置参数指数组下标
 * 2. 判断条件有:表是否已满; 位置是否合法 */

#ifndef	LIST_H
#define	LIST_H

#include	<iostream>
using namespace std;

template <class T>
class arrList {
private:
	T* 	aList;
	int	maxSize;
	int	curLen;
	int	position;

public:
	arrList(const int size) {
		maxSize = size;
		aList = new T[maxSize];
		curLen = position = 0;
		cout<<"new empty list!"<<endl;
	}

	~arrList() {
		delete [] aList;
	}
	
	void print() {
		for (int i = 0; i < curLen; i++)
			cout<<aList[i]<<" ";
		cout<<endl;
	}

	void clear() {
		delete [] aList;
		curLen = position = 0;
		aList = new T[maxSize];
		cout<<"list has been cleared!"<<endl;
	}

	bool isEmpty() {
		if (curLen == 0) {
			cout<<"list is empty!"<<endl;
			return true;
		}
		
		return false;
	}

	int length() {
		return curLen;
	}
	
	bool getValue(const int p, T& value);
	bool setValue(const int p, const T value);
	bool getPos(int& p, const T value);
	bool append(const T value);
	bool insert(const int p, const T value);
	bool deletee(const int p);
};

template<class T>
bool arrList<T>::getValue(const int p, T& value) {
	if (p < 0 || p >= curLen) {
		cout<<"illegal getvalue!"<<endl;
		return false;
	}

	value = aList[p];
	
	return true;
}

//only can set list[0] - list[curLen-1]
template<class T>
bool arrList<T>::setValue(const int p, const T value) {
	if (p < 0 || p >= curLen) {
		cout<<"illegal setvalue!"<<endl;
		return false;
	}

	aList[p] = value;
	
	return true;
}

// return array subscript
template<class T>
bool arrList<T>::getPos(int& p, const T value) {
	for (int i = 0; i < curLen; i++)
		if (value == aList[i]) {
			p = i;
			return true;
		}
	
	return false;
}

template<class T>
bool arrList<T>::append(const T value) {
	if (curLen >= maxSize) {
		cout<<"The list is overflow!"<<endl;
		return false;
	}

	aList[curLen++] = value;
	
	return true;
}

/* 1. if list not full, can insert
 * 2. if insertion legal, can insert
 * 3. from last to insertion, array elements move to next position
 * 4. insert */
template<class T>
bool arrList<T>::insert(const int p, const T value) {
	if (curLen >= maxSize) {
		cout<<"list is overflow!"<<endl;
		return false;
	}
	
	if (p < 0 || p > curLen) {	
		cout<<"Insertion point is illegal!"<<endl;
		return false;
	}

	for (int i = curLen; i > p; i--) 
		aList[i] = aList[i-1];
	aList[p] = value;
	
	curLen++;

	return true;
}

/* 1. if list is empty, cann't delete
 * 2. if deletion illegal, cann't delete
 * 3. delete:from deletion to last, array elements move forward one */
template<class T>
bool arrList<T>::deletee(const int p) {
	if (curLen <= 0) {
		cout<<"No element to delete!"<<endl;
		return false;
	}
	
	if (p < 0 || p > curLen-1) {	
		cout<<"deletion is illegal!"<<endl;
		return false;
	}
	
	for (int i = p; i < curLen-1; i++) 
		aList[i] = aList[i+1];
	
	curLen--;

	return true;
}

#endif

#include	<iostream>
#include	"arrlist.h"
using namespace std;

int
main() {
	arrList<int> list(10);
	
	cout<<"The list is "<<(list.isEmpty()?"empty":"not empty")<<endl<<endl;
	
	cout<<"init list 0-4"<<endl;
	for (int i = 0; i < 5; i++) 
		list.append(i);
	cout<<"The list is "<<(list.isEmpty()?"empty":"not empty")<<endl;
	cout<<"print the list:"<<endl;
	list.print();
	cout<<endl;

	cout<<"insert(0,5) (3, 5) (7, 5) (9, 5) (-3, 0)"<<endl;
	list.insert(0, 5);
	list.insert(3, 5);
	list.insert(7, 5);
	list.insert(9, 5);
	list.insert(-3, 0);
	list.print();
	cout<<endl;
	
	cout<<"setvalue(4, 5) (-4, 5) (33, 5)"<<endl;
	list.setValue(4, 5);
	list.setValue(-4, 5);
	list.setValue(33, 5);
	list.print();
	cout<<endl;

	cout<<"getvalue 0-curLen"<<endl;
	int v;	
	for (int i = 0; i < list.length(); i++) {
		list.getValue(i, v);
		cout<<v<<" ";
	}
	cout<<endl;

	cout<<"getpos 5,4"<<endl;
	int p;
	list.getPos(p, 5);
	cout<<p<<" ";
	list.getPos(p, 4);
	cout<<p<<endl<<endl;

	cout<<"delete 0, -3, 9"<<endl;
	list.deletee(0);
	list.print();
	list.deletee(-3);
	list.deletee(9);
	cout<<endl;

	list.clear();
	list.isEmpty();
	
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值