[数据结构]双链表模拟网站前进后退

//web_site.h
#pragma once
enum Error_code{success,underflow,overflow};
template<class Node_entry>
class Webset
{
public:
	Webset();
	~Webset();
	Error_code insert(const Node_entry &item);
	Error_code back();
	Error_code forward();
	Error_code print()const;
private:
	struct Node
	{
		Node *next;
		Node *previous;
		Node_entry entry;
		Node();
		Node(Node_entry item, Node *pre_add = NULL, Node *next_add = NULL);
	};
	Node *head;
	Node *current;
	void clear(Node *&ptr);
};

template<class Node_entry>
inline Webset<Node_entry>::Node::Node()
{
	next = NULL;
	previous = NULL;
}

template<class Node_entry>
inline Webset<Node_entry>::Node::Node(Node_entry item, Node * pre_add, Node * next_add)
{
	entry = item;
	next = next_add;
	previous = pre_add;
}

//web_site.cpp
#include"website.h"
using namespace std;

template<class Node_entry>
Webset<Node_entry>::Webset()
{
	current = head = NULL;
}

template<class Node_entry>
Webset<Node_entry>::~Webset()
{
	clear(head);
}

template<class Node_entry>
Error_code Webset<Node_entry>::insert(const Node_entry & item)
{
	if (current == NULL) {
		head = new Node(item);
		current = head;
		return success;
	}
	else if (current->next == NULL) {
		current->next = new Node(item, current, NULL);
		current = current->next;
		return success;
	}
	else {
		clear(current->next);
		current->entry = item;
		return success;
	}
}

template<class Node_entry>
Error_code Webset<Node_entry>::print() const
{
	if (current == NULL)return underflow;
	else cout << current->entry;
	return success;
}

template<class Node_entry>
Error_code Webset<Node_entry>::back()
{
	if (current==NULL||current->previous == NULL)return underflow;
	current = current->previous;
	return success;
}

template<class Node_entry>
Error_code Webset<Node_entry>::forward()
{
	if (current==NULL||current->next == NULL)return overflow;
	current = current->next;
	return success;
}

template<class Node_entry>
void Webset<Node_entry>::clear(Node *& ptr)
{
	while (ptr) {
		Node *temp = ptr;
		ptr = ptr->next;
		delete temp;
	}
}

//main.cpp
#include<iostream>
#include<string>
#include"website.cpp"
using namespace std;
void UI() {
	cout << "***************************************Simulation website************************************" << endl;
	cout << "MENU" << endl;
	cout << "[b].Go back to the previous website." << endl;
	cout << "[f].Go front to the next website." << endl;
	cout << "[i].Open a new website." << endl;
	cout << "[p].Print current website." << endl;
	cout << "[q].Exit the programm." << endl;
}
void flash() {
	system("pause");
	system("cls");
	UI();
}
char Get_command() {
	cout << "Please enter the command." << endl;
	char command='0';
	cin.get(command);
	while (command != 'b'&&command != 'f'&&command != 'p'
		&&command != 'q'&&command != 'i') {
		cout << "illegal command.Please enter again." << endl;
		cin >> command;
	}
	return command;
}
bool do_command(char command,Webset<string> &test) {
	switch (command)
	{
	case'q': {
		cout << "You'll exit the programm." << endl;
		flash();
		return false;
	}
	case'b': {
		if (test.back() == underflow) {
			cout << "This is the first website.Can't go back." << endl;
			flash();
			return true;
		}
		else {
			cout << "Current website:";
			test.print();
			cout << endl;
			flash();
			return true;
		}
	}
	case'f': {
		if (test.forward() == overflow) {
			cout << "This is the last website.Can't go forward." << endl;
			flash();
			return true;
		}
		else {
			cout << "Current website:";
			test.print();
			cout << endl;
			flash();
			return true;
		}
	}
	case'i': {
		string item;
		cout << "Please enter the website you want to visit." << endl;
		cin >> item;
		test.insert(item);
		cout << "Visit success." << endl;
		cout << "Current website:";
		test.print();
		cout << endl;
		flash();
		return true;
	}
	case'p': {
		cout << "Current website:";
		if (test.print() == underflow)cout << "You haven't visit any website." << endl;
		flash();
		return true;
	}
	default:return true;
	}
}
void main() {
	UI();
	Webset<string>test;
	bool flag;
	do {
		flag=do_command(Get_command(), test);
	} while (flag);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值