数组实现链表

黎大佬提出了一个方法:在next[]中表示两个置换轨道,一个是occupied,另一个是 vacant。

然后每个操作都维护这两个轨道。

有空再写吧。。。

---------------------------------------------------------------

2018-1-2

寻找可用位置的方法可以优化一下:

利用一个数据结构(栈或队列)存储可用位置,

就可以达到O(1)的效率。

----------------------------------------------------------------

应曹大佬邀请,写一下关于用数组实现链表的方法。

2017-12-31

参考:《代数学引论》——柯斯特利金 P36 置换

https://zh.wikipedia.org/wiki/%E7%BD%AE%E6%8F%9B

参考题目:

http://www.lintcode.com/zh-cn/problem/recover-rotated-sorted-array/

http://www.cnblogs.com/zx-zhang/p/7719944.html

http://www.voidcn.com/article/p-oxiboomv-bkb.html

#include <iostream> 
#include <string>
using namespace std;

class MyListException {
public:
	MyListException(std::string _msg) :msg(_msg) {}
	friend ostream& operator<<(ostream& out, const MyListException& e);
private:
	std::string msg;
};

ostream& operator<<(ostream& out, const MyListException& e) {
	out << e.msg;
	return out;
}


class MyList {
public:
	MyList(int _maxSize);
	void insert(const int e, const  int pos);	//insert an element at nth position
	void push_back(const int e);				//push back
	void erase(const int num);					//delete the nth element.
	int front() const;							//access the first element
	int back() const;							//access the last element
	int size() const;
	int max_size() const;
	bool empty() const;
	MyList(const MyList&) = delete;
	MyList& operator=(const MyList&) = delete;
	~MyList();
private:
	int pos_available() const;					//find the available pos
	int head, cur, list_size;
	const int maxSize, null;
	int *list, *next;
};

MyList::MyList(int _maxSize)
	:head(0), cur(-1), list_size(0), maxSize(_maxSize), null(_maxSize + 1)
{
	list = new int[maxSize];
	next = new int[maxSize];
	for (int i = 0; i < maxSize; ++i)
		next[i] = null;
}

//insert an element at nth position.
void MyList::insert(const int e, const int num) {
	if (num < 1 || num > list_size + 1)throw MyListException("The position is invalid.");
	if (list_size == maxSize)throw MyListException("List is full.");
	//after the cur
	if (num == list_size + 1) {
		push_back(e);
		return;
	}
	list_size++;
	int index = head, pre, pos = pos_available();
	for (int i = 1; i < num; ++i) {
		pre = index;
		index = next[index];
	}
	//before the head
	if (num == 1) {
		list[pos] = e;
		next[pos] = head;
		head = pos;
		return;
	}
	//other trivial conditions
	next[pre] = pos;
	list[pos] = e;
	next[pos] = index;
}

void MyList::push_back(const int e) {
	if (list_size == maxSize)throw MyListException("List is full.");
	if (list_size++ == 0) {
		list[head] = e;
		cur = head;
		return;
	}
	int pos = pos_available();
	next[cur] = pos;
	list[pos] = e;
	cur = pos;
}

//delete the nth element.
void MyList::erase(const int num) {
	if (num < 1 || num > maxSize)throw MyListException("The position is invalid.");
	if (list_size < num)throw MyListException("Not enough elements.");
	if (list_size == 0)throw MyListException("List is empty.");
	list_size--;
	int index = head, pre;
	for (int i = 1; i < num; ++i) {
		pre = index;
		index = next[index];
	}
	//delete head == cur
	if (list_size == 0) {
		cur = -1;
		return;
	}
	//delete head element.
	if (num == 1) {
		head = next[index];
		next[index] = null;
		return;
	}
	//delete the last;
	if (index == cur) {
		cur = pre;
		next[pre] = null;
		return;
	}
	//other trivial conditions
	next[pre] = next[index];
	next[index] = null;
}

int MyList::front() const {
	return list[head];
}

int MyList::back() const {
	if (list_size == 0)throw MyListException("List is empty.");
	return list[cur];
}

int MyList::size() const {
	return list_size;
}

int MyList::max_size() const {
	return maxSize;
}

bool MyList::empty() const {
	return list_size == 0;
}

int MyList::pos_available() const {
	int pos = 0;
	for (int i = 1; i < maxSize; ++i) {
		pos = (i + cur) % maxSize;
		if (next[pos] == null)return pos;
	}
}

MyList::~MyList() {
	delete[] list;
	delete[] next;
}


int main() {

	MyList list(5);

	try {
		//do something


	}
	catch (MyListException e) {
		cout << e << endl;
	}

	system("pause");
	return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值