链表 类

#include 
#include 
using namespace std;

const int ERR_DEL = 0;  //没有可删除的元素
const int ERR_UNDEFINED = 1; //当前位置不存在 
const int ERR_OVERSTEP = 2;  //下标越界 

struct Value
{
	int value;
	Value *next;
	Value *prev;
};
class Chain
{
public:
	Chain(void);
	~Chain();
	int getLength(void);  //获取链表长度 
	int getValue(void);   //获取当前位置元素的值 
	void setValue(int value);  //设置当前位置元素的值
	bool next(void);     //跳到下一个元素 
	bool prev(void);     //跳到上一个元素 
	void first(void);    //跳到第一个元素 
	void last(void);     //跳到最后一个元素 
	void del(void);      //删除当前位置元素 
	void moveTo(int index);  //跳到指定序号(从0开始) 
	void insert(int value);  //在当前位置插入元素 
	void append(int value);  //在链表末尾插入元素 
	bool locate(int value);  //从头到尾寻找元素,定位到元素位置 
	void clear(void);       //清空 
	void fillRandom(int begin, int end, int length);   //以随机数据填充 
	void printAll(void);     //打印链表中元素
	void sort(bool ascending = true);  //排序,默认为升序 
private:
	int length;
	Value *current;
	Value *head;
	Value *tail;
	Value *deletedNext;
	Value *deletedPrev;
};

Chain::Chain(void)
{
	length = 0;
	current = NULL;
	head = NULL;
	tail = NULL;
	srand((unsigned)time(NULL));
}
Chain::~Chain()
{
	clear();
}
void Chain::clear(void)
{
	if (length > 0)
	{
		first();
		do
		{
			del();
		} while (next());
	}
	length = 0;
	current = head = tail = NULL;
}
void Chain::append(int value)
{
	Value *temp = new Value;
	temp -> value = value;
	temp -> next = NULL;
	if (tail == NULL)
	{
		current = head = tail = temp;
		length = 1;
		temp -> prev = NULL;
	}
	else
	{
		tail -> next = temp;
		length++;
		temp -> prev = tail;
		tail = temp;
	}
}
int Chain::getLength(void)
{
	return length;
}
void Chain::first(void)
{
	current = head;
}
void Chain::last(void)
{
	current = tail;
}
bool Chain::next(void)
{
	Value *next;
	if (current == NULL)
	{
		if (length <= 1)
		{
			return false;
		}
		next = deletedNext;
	}
	else
	{
		next = current -> next;
	}
	if (next != NULL)
	{
		current = next;
		return true;
	}
	return false;
}
bool Chain::prev(void)
{
	Value *prev;
	if (current == NULL)
	{
		if (length <= 1)
		{
			return false;
		}
		prev = deletedPrev;
	}
	else
	{
		prev = current -> prev;
	}
	if (prev != NULL)
	{
		current = prev;
		return true;
	}
	return false;
}
int Chain::getValue(void)
{
	if (current == NULL)
	{
		cerr << "当前位置不存在!" << endl;
		throw(ERR_UNDEFINED);
		return -1;
	}
	return current -> value;
} 
void Chain::setValue(int value)
{
	if (current == NULL)
	{
		cerr << "当前位置不存在!" << endl;
		throw(ERR_UNDEFINED);
		return;
	}
	current -> value = value;
}
void Chain::moveTo(int index)
{
	if (index < 0 || index > length - 1)
	{
		cerr << "下标越界!" << endl;
		throw(ERR_OVERSTEP);
		return;
	}
	first();
	for (int i = 0; i < index; i++)
	{
		next();
	}
} 
void Chain::printAll(void) 
{
	if (length == 0)
	{
		cout << "链表是空的!" << endl;
	}
	else
	{
		cout << "链表中所有元素为:" << endl;
		Value *temp = head;
		do
		{
			cout << temp -> value << " ";
			temp = temp -> next;
		} while (temp != NULL);
		cout << endl;
	}
}
void Chain::del(void) 
{
	if (length == 0)
	{
		cerr << "没有元素可以删除!" << endl;
		throw(ERR_DEL);
		return;
	}
	else if (length == 1)
	{
		delete current;
		current = head = tail = NULL;
		length = 0;
	}
	else
	{
		Value *prev, *next;
		prev = current -> prev;
		next = current -> next;
		if (next != NULL)
		{
			next -> prev = prev;
		}
		else
		{
			tail = prev;
		}
		if (prev != NULL)
		{
			prev -> next = next;
		}
		else
		{
			head = next;
		}
		delete current;
		current = NULL;
		deletedNext = next;
		deletedPrev = prev;
		length--;
	}
} 
void Chain::fillRandom(int begin, int end, int length)
{
	clear();
	for (int i = 0; i < length ; i++)
	{
		append(rand() % (end - begin + 1) + begin);
	}
}
bool Chain::locate(int value)
{
	first();
	while (getValue() != value && next());
	return (getValue() == value);
}
void _swap(int &a, int &b)
{
	a = a ^ b;
	b = a ^ b;
	a = a ^ b;
}
void Chain::sort(bool ascending)
{	
	int left, right;
	bool swapped = false;
	do
	{
		first();
		swapped = false;
		while (1) 
		{
			left = getValue();
			if (next())
			{
				right = getValue();
				if (ascending && left > right || !ascending && left < right)
				{
					swap(current -> value, current -> prev -> value);
					swapped = true;
				}
			}
			else
			{
				break;
			}
		}
	} while (swapped);
}
void Chain::insert(int value)
{
	if (current == NULL)
	{
		cerr << "当前位置不存在!" << endl;
		throw(ERR_UNDEFINED);
		return;
	}
	Value *temp = new Value;
	temp -> value = value;
	if (current -> prev == NULL)
	{
		head = temp;
		current -> prev = temp;
		temp -> prev = NULL;
		temp -> next = current;
	}
	else
	{
		temp -> prev = current -> prev;
		current -> prev = temp;
		temp -> next = current;
		temp -> prev -> next = temp;
	}
}

int main()
{
	Chain chain;
	/*for (int i = 0; i < 10; i++)
	{
		chain.append(i + 5);
	}
	chain.printAll(); 
	chain.moveTo(9);
	chain.del();
	chain.printAll(); */
	chain.fillRandom(1, 100, 10);
	chain.printAll(); 
	chain.moveTo(-1);
	chain.insert(10000); 
	chain.printAll(); 
	return 0;
} 

 

你可能感兴趣的:(C++算法和类)