黎大佬提出了一个方法:在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://blog.csdn.net/rsy56640/article/details/78494242
悲剧文本 Broken Keyboard
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;
}