Linked_List

//Linked_List.h
enum Error_code {underflow,overflow,range_out,success};
template<class Node_entry>
struct Node {
	Node_entry entry;
	Node *next;
	Node();
	Node(Node_entry item, Node<Node_entry> *add_on = NULL);
};

template<class List_entry>
class List
{
public:
	//methods of the List ADT
	List();
	int size()const;
	bool full()const;
	bool empty()const;
	void clear();
	void traverse(void(*visit)(List_entry &item));
	Error_code retrieve(int position, List_entry &x)const;
	Error_code replace(int position, const List_entry &x);
	Error_code remove(int position, List_entry &x);
	Error_code insert(int position, const List_entry &x);

	//safety features for the List
	List(const List<List_entry> &copy);
	void operator=(const List<List_entry> &copy);
	~List();

protected:
	int count;
	Node<List_entry> *head;
	Node<List_entry> *set_position(int position)const;
};

template<class Node_entry>
inline Node<Node_entry>::Node()
{
	next = nullptr;
}

template<class Node_entry>
inline Node<Node_entry>::Node(Node_entry item, Node<Node_entry>* add_on)
{
	entry = item;
	next = add_on;
}<pre name="code" class="cpp">//Linked_List.cpp
#include"Linked_List.h"
#include<iostream>
using namespace std;

template<class List_entry>
List<List_entry>::List() {
	head = NULL;
	count = 0;
}

template<class List_entry>
int List<List_entry>::size() const
{
	return count;
}

template<class List_entry>
bool List<List_entry>::full() const
{
	Node<List_entry> *p = new Node<List_entry>;
	if (p == NULL)return true;
	else return false;
}

template<class List_entry>
bool List<List_entry>::empty() const
{
	return (count==0);
}

template<class List_entry>
void List<List_entry>::clear()
{
	List_entry x;
	while (count != 0)remove(0, x);
}

template<class List_entry>
void List<List_entry>::traverse(void(*visit)(List_entry &item))
{
	Node<List_entry> *p = head;
	for (int i = 0; i < count; i++){
		(*visit)(p->entry);
		p = p->next;
	}
}

template<class List_entry>
Error_code List<List_entry>::retrieve(int position, List_entry & x) const
{
	if (position<0||position>=count)return range_out;
	Node<List_entry> *p = set_position(position);
	x = p->entry;
	return success;
}

template<class List_entry>
Error_code List<List_entry>::replace(int position, const List_entry & x)
{
	if (position<0 || position >= count)return range_out;
	Node<List_entry> *p = set_position(position);
	p->entry = x;
	return success;
}

template<class List_entry>
Error_code List<List_entry>::remove(int position, List_entry & x)
{
	if (position<0 || position >= count)return range_out;
	if (position == 0) {
		x = head->entry;0
		Node<List_entry> *p=head;
		head = head->next;
		delete p;
		count--;
		return success;
	}
	Node<List_entry> *pre, *cur;
	pre = set_position(position-1);
	cur = pre->next;
	pre->next = cur->next;
	x = cur->entry;
	delete cur;
	count--;
	return success;
}

template<class List_entry>
Error_code List<List_entry>::insert(int position, const List_entry & x)
{
	if (position<0 || position > count)return range_out;
	if (full())return overflow;
	if (position == 0) {
		head = new Node<List_entry>(x, head);
		count++;
		return success;
	}
	Node<List_entry> *pre = set_position(position - 1);
	Node<List_entry> *cur = pre->next;
	pre->next =	new Node<List_entry>(x,cur);
	count++;
	return success;
}

template<class List_entry>
List<List_entry>::List(const List<List_entry>& copy)
{
	//version 1:
	for (int i = 0; i < copy.size(); i++) {
		List_entry item;
		copy.retrieve(i, item);
		insert(i, item);
	}
	//version 2:
	/*void make(List_entry item) {
		insert(count, item);
	}
	copy.tranverse(make());*/
}

template<class List_entry>
void List<List_entry>::operator=(const List<List_entry>& copy)
{
	clear();
	List(copy);
}

template<class List_entry>
List<List_entry>::~List()
{
	clear();
}

template<class List_entry>
Node<List_entry>* List<List_entry>::set_position(int position) const
{
	/*precondition: position is vaild*/
	Node<List_entry> *p = head;
	for (int i = 0; i < position; i++)p = p->next;
	return p;
}

//main.cpp
#include"Linked_List.cpp"
#include<iostream>
using namespace std;
void UI() {
	cout << "**********************************Linked_List**********************************" << endl;
	cout << "Please according to the prompt to input." << endl;
	cout << "MENU:" << endl;
	cout << "[1].insert." << endl;
	cout << "[2].remove." << endl;
	cout << "[3].replace." << endl;
	cout << "[4].retrieve." << endl;
	cout << "[5].size." << endl;
	cout << "[6].is_full." << endl;
	cout << "[7].is_empty." << endl;
	cout << "[8].clear." << endl;
	cout << "[0].exit." << endl;
}
void flash() {
	system("pause");
	system("cls");
	UI();
}
int get_command() {
	cout << "please enter the command." << endl;
	int command;
	cin >> command;
	while (command < 0 || command>8) {
		cout << "Illegal input.Please enter again." << endl;
		cin >> command;
	}
	return command;
}

void print(int &item) {
	cout << item << ' ';
}
void main() {
	List<int> test;
	UI();
	while (1) {
		switch (get_command())
		{
		case 0:
			cout << "You'll exit the programm later." << endl;
			return;
		case 1: {
			cout << "Please enter the characters to be inserted." << endl;
			int x;
			cin >> x;
			cout << "Please enter the position to be inserted." << endl;
			int pos;
			cin >> pos;
			if (test.insert(pos, x) == success)cout << "Insert success." << endl;
			else cout << "Insert fail.Please check you position." << endl;
			flash();
			break;
		}
		case 2: {
			cout << "Please enter the position to be inserted." << endl;
			int pos;
			cin >> pos;
			int x;
			if (test.remove(pos, x) == success)cout << x <<" Remove success." << endl;
			else cout << "Remove fail.Please check you position." << endl;
			flash();
			break;
		}
		case 3: {
			cout << "Please enter the characters to replace." << endl;
			int x;
			cin >> x;
			cout << "Please enter the position to be inserted." << endl;
			int pos;
			cin >> pos;
			if (test.replace(pos, x) == success)cout << "Replace success." << endl;
			else cout << "Replace fail.Please check you position." << endl;
			flash();
			break;
		}
		case 4:
			test.traverse(print);
			cout << endl;
			flash();
			break;
		case 5: 
			cout << "Current size of this List is : " << test.size() << endl;
			flash();
			break;
		case 6:
			if (test.full())cout << "FULL" << endl;
			else cout << "Not full" << endl;
			flash();
			break;
		case 7:
			if (test.empty())cout << "EMPTY" << endl;
			else cout << "Not empty" << endl;
			flash();
			break;
		case 8:
			test.clear();
			cout << "Clear success." << endl;
			flash();
			break;
		}
	}
}

 
 


你可能感兴趣的:(Linked_List)