[数据结构]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;
		}
	}
}

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请参考我给出的代码框架,实现对EMPLOYEE结构体为数据的双向链表的排序算法,要求按照按employeeId升序排列 typedef struct linkNode { void* data; //使用空指针使得NODE适配多种数据结构 struct linkNode* preNode; struct linkNode* nextNode; }LINKED_NODE; /*Define the struct of double linked list.*/ typedef struct { LINKED_NODE* head; LINKED_NODE* tail; size_t size; }DOUBLE_LINK_LIST; typedef struct { int employeeId; char name[20]; char ipAddress[30]; char seatNumber[20]; char group[10]; } EMPLOYEE; DOUBLE_LINK_LIST* createDoubleLinkedList() { DOUBLE_LINK_LIST* newList = (DOUBLE_LINK_LIST*)malloc(sizeof(DOUBLE_LINK_LIST)); newList->head = NULL; newList->tail = NULL; newList->size = 0; return newList; } void destroyDoubleLinkedList(DOUBLE_LINK_LIST* list) {} /*Add a new node before the head.*/ void insertHead(DOUBLE_LINK_LIST* list, void* data) // void执政适配其他data类型? {} /*Add a new node after tail.*/ void insertTail(DOUBLE_LINK_LIST* list, void* data) // 如何适配其他data类型? {} /*Insert a new node.*/ void insertNode(DOUBLE_LINK_LIST* list, void* data,int index) // 如何适配其他data类型? {} void deleteHead(DOUBLE_LINK_LIST* list) {} void deleteTail(DOUBLE_LINK_LIST* list) {} void deleteNode(DOUBLE_LINK_LIST* list, int index) {} LINKED_NODE* getNode(DOUBLE_LINK_LIST* list, int index) {} /* 遍历链表,对每个节点执行指定操作*/ void traverseList(DOUBLE_LINK_LIST* list, void (*callback)(void*)) { LINKED_NODE* currentNode = list->head; while (currentNode != NULL) { callback(currentNode->data); currentNode = currentNode->nextNode; } } void printEmployee(void* data) {}
最新发布
07-25

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值