C/C++編程面試題——單鏈表功能實現

#include <iostream>
using namespace std;
template<typename _Ty>
struct Node {
	Node* next;
	_Ty data;
	Node(_Ty value = {}) :next(nullptr), data(value) {}
};
template<typename _Ty>
class List {
	using pNode = Node<_Ty>*;
public:
	//單鏈表的初始化
	List():head(new Node<_Ty>()),end(head) {}
	
	//單鏈表的元素追加
	void push_back(_Ty value) {
		end->next = new Node<_Ty>(value);
		end = end->next;
	}

	//單鏈表的測長
	int length() {
		int count = 0;
		traverse([&count](pNode) {
				count++; 
			});
		return count;
	}

	//單鏈表的打印
	friend ostream& operator<<(ostream& out, List& list) {
		list.traverse([&out](List::pNode it) {
				out << it->data;
			});
		return out;
	}

	//根據值查找結點
	pNode find(_Ty key) {
		return find_if([&key](pNode node) {
				return node->data == key;
			});
	}

	//通過值插入
	void insert(unsigned int index, _Ty value) {
		insert(index, new Node<_Ty>(value));
	}

	//通過結點插入
	void insert(unsigned int index, pNode node) {
		pNode preNode ;
		if (index == 0) {				//添加到鏈表首部
			preNode = head;
		}
		else {
			index--;
			preNode = find_if([&index](pNode node) {
				return index == 0;
				});
			if (preNode == nullptr)		//超出長度時追加到末尾
				preNode = end;
		}
		node->next = preNode->next;
		preNode->next = node;	
	}

	//通過值刪除
	void remove(_Ty value) {
		pNode preNode = head;
		while (true) {
			if (preNode->next->data == value || preNode == nullptr)
				break;
			preNode = preNode->next;
		}
		if (preNode != nullptr) {
			pNode node = preNode->next;
			preNode->next = node->next;
			delete node;
		}
	}

	//反轉鏈表
	void reverse() {
		pNode  p, q, r;
		p = head->next;
		if (p == nullptr)
			return;
		q = p->next;
		r = nullptr;
		while (q!=nullptr) {
			p->next = r;
			r = p;
			p = q;
			q = q->next;
		}
		p->next = r;
		head->next = p;
	}

	//有序鏈表的合併
	List& ordered_merge(List& o) {
		pNode pos=head;
		pNode a = head->next, b = o.head->next;
		while (a != nullptr && b != nullptr) {
			if (a < b) {
				pos->next = a;
				a = a->next;
			}
			else {
				pos->next = b;
				b = b->next;
			}
			pos = pos->next;
		}
		if (a == nullptr) {
			pos->next = b;
		}
		else {
			pos->next = a;
		}
	}
	
	//模板函數:單鏈表的遍歷
	template<typename _F>
	void traverse(_F func) {
		pNode trav = head;
		while (true) {
			trav = trav->next;
			if (trav == nullptr)
				return;
			func(trav);
		}
	}

	//模板函數:查找函數
	template<typename _F>
	pNode find_if(_F func) {
		pNode trav = head;
		while (true) {
			trav = trav->next;
			if (trav == nullptr||func(trav))
				return trav;
		}
	}

	_Ty operator[](unsigned int index) {
		return find_if([&index](pNode) {
			return index-- == 0;
			})->data;
	}
	List& operator+(List& o) {
		end->next = o.head->next;
		return *this;
	}

private:
	pNode head;
	pNode end;
};
int main() {
	List<int> list;
	cout << list.length();
	cout << list << endl;
	list.push_back(1);
	list.push_back(2);
	list.push_back(3);
	list.insert(100, 5);
	list.remove(2);
	cout << list.length();
	cout << list << endl;
	cout << list[1] << endl;
	list.reverse();
	cout << list << endl;
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章