线性表数组实现


LinearList.h

#pragma once
#ifndef LINEARLIST_H
#define LINEARLIST_H
class LinearList
{
public:
	LinearList();
	~LinearList();
	bool empty() const;
	int size() const;
	int get(int index) const;
	int indexof(int data) const;
	LinearList* insert(int data);
	LinearList* insert(int index,int data);//index之后插入data
	LinearList* del(int index);
	void output() const;
private:
	int ListSize;
	int MaxLength;
	int *Element;
};
#endif


LinearList.cpp

#include "LinearList.h"
#include<stdio.h>
#define X 1.8



LinearList::LinearList()
{
	ListSize = 0;
	MaxLength = 2;
	Element = new int[MaxLength];
}


LinearList::~LinearList()
{
	delete Element;
}


bool LinearList::empty() const {
	return ListSize == 0;
}


int LinearList::size() const {
	return ListSize;
}


int LinearList::get(int index) const {
	if (index > ListSize) {
		printf("out of range\n");
		return -1;
	}
	return Element[index];
}


int LinearList::indexof(int data) const {
	for (int _index_ = 0; _index_ < ListSize; _index_++) {
		if (Element[_index_] == data) {
			return _index_;
		}
	}
	printf("target not found\n");
	return -1;
}


LinearList* LinearList::insert(int data) {
	if (ListSize == MaxLength) {
		MaxLength *= X;
		int *_Element_ = Element;
		Element = new int[MaxLength];
		for (int _index_ = 0; _index_ < ListSize; _index_++) {
			Element[_index_] = _Element_[_index_];
		}
		delete _Element_;
	}
	Element[ListSize] = data;
	ListSize++;
	return this;
}


LinearList* LinearList::insert(int index, int data) {
	if (index > ListSize) {
		printf("out of range\n");
		return this;
	}
	if (ListSize == MaxLength) {
		MaxLength *= X;
		int *_Element_ = Element;
		Element = new int[MaxLength];
		for (int _index_ = 0; _index_ < index; _index_++) {
			Element[_index_] = _Element_[_index_];
		}
		delete _Element_;
	}
	for (int _index_ = ListSize; _index_ > index; _index_--) {
		Element[_index_] = Element[_index_ - 1];
	}
	Element[index] = data;
	ListSize++;
	return this;
}


LinearList* LinearList::del(int index) {
	if (index >= ListSize) {
		printf("out of range\n");
	}
	else if (ListSize == int(MaxLength / X) + 1) {
		MaxLength /= X;
		int *_Element_ = new int[MaxLength];
		ListSize--;
		for (int _index_ = 0; _index_ < index; _index_++) {
			_Element_[_index_] = Element[_index_];
		}
		for (int _index_ = index; _index_ < ListSize; _index_++) {
			_Element_[_index_] = Element[_index_+1];
		}
		delete Element;
		Element = _Element_;
	}
	else {
		ListSize--;
		for (int _index_ = index; _index_ < ListSize; _index_++) {
			Element[_index_] = Element[_index_ + 1];
		}
	}
	return this;
}


void LinearList::output() const {
	if (empty()) {
		printf("list is empty\n");
		return;
	}
	for (int _index_ = 0; _index_ < ListSize; _index_++) {
		printf("%d ", Element[_index_]);
	}
	printf("\n");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值