【线性表】顺序表

SeqList.h

#ifndef SEQLIST_H
#define SEQLIST_H

#include<iostream>

const int default_size = 100;
//SeqList
template<typename T>
class SeqList{
public:
	typedef T value_type;
	typedef T* pointer;
	SeqList(int size = default_size);//constructor
	~SeqList();//destructor
	int get_length();//get length
	bool is_empty(); // whether SeqList is empty or not
	void traverse();//traverse SeqList
	void clear();//clear SeqList
	bool get_elem(int i, pointer value);//get the ith data
	int locate(value_type value);//find the position of value
	bool insert(int i, value_type value);//insert data in the ith place
	bool remove(int i, pointer value);//remove the ith data
private:
	pointer element;
	const int max_size;//max size
	int cur_len;//current length
};
//constructor
template<typename T>
SeqList<T>::SeqList(int size):cur_len(0){
	if (size > 0){
		element = new T[size];
		max_size = size;
	}
	else{
		element = new T[default_size];
		max_size = default_size;
	}
}
//destructor
template<typename T>
SeqList<T>::~SeqList(){
	delete[] element;
}
//get length
template<typename T>
int SeqList<T>::get_length(){
	return cur_len;
}
// whether SeqList is empty or not
template<typename T>
bool SeqList<T>::is_empty(){
	return cur_len == 0 ? true : false;
}
//traverse SeqList
template<typename T>
void SeqList<T>::traverse(){
	for (int i = 0; i < cur_len; i++){
		std::cout << element[i] << " ";
	}
	std::cout << std::endl;
}
//clear SeqList
template<typename T>
void SeqList<T>::clear(){
	for (int i = 0; i < cur_len; i++){
		element[i]=T();
	}
	cur_len = 0;
}
//get the ith data
template<typename T>
bool SeqList<T>::get_elem(int i, pointer value){
	if (cur_len == 0 || i<1 || i>cur_len)
		return false;
	*value = element[i - 1];
	return true;
}
//find the position of value
template<typename T>
int SeqList<T>::locate(value_type value){
	for (int i = 0; i < cur_len; i++){
		if (element[i] == value)
			return i + 1;
	}
	return 0;
}
//insert data in the ith place
template<typename T>
bool SeqList<T>::insert(int i, value_type value){
	if (cur_len == max_size || i<1 || i>cur_len + 1)
		return false;
	for (int j = cur_len; j>i-1; j--){
		element[j] = element[j - 1];
	}
	element[i - 1] = value;
	cur_len++;
	return true;
}
//remove the ith data
template<typename T>
bool SeqList<T>::remove(int i, pointer value){
	if (cur_len == 0 || i<1 || i>cur_len)
		return false;
	*value = element[i - 1];
	for (int j = i; j < cur_len; j++){
		element[j - 1] = element[j];
	}
	cur_len--;
	return true;
}

#endif
main.cpp

#include"SeqList.h"
using namespace std;

int main(){
	SeqList<int> int_list;
	cout << int_list.get_length() << endl; //0
	cout << boolalpha << int_list.is_empty() << endl;//true
	for (int i = 0; i < 10; i++){
		int_list.insert(i, i);
	}
	int_list.traverse();//1 2 3 4 5 6 7 8 9 
	int value;
	if (int_list.get_elem(5, &value))
		cout << "get element succeed,value is " << value << endl;//get element succeed,value is 5
	else
		cout << "get element fail" << endl;

	cout << int_list.locate(8) << endl;//8

	if (int_list.insert(7, 20))
		cout << "insert succeed" << endl;//insert succeed
	else
		cout << "insert fail" << endl;

	if (int_list.remove(8, &value))
		cout << "remove succeed,remove value is " << value << endl;//remove succeed,remove value is 7
	else
		cout << "remove fail" << endl;
	int_list.traverse();//1 2 3 4 5 6 20 8 9 
	cout << int_list.get_length() << endl;//9
	int_list.clear();
	cout << int_list.get_length() << endl;//0

	return 0;
}

你可能感兴趣的:(【线性表】顺序表)