//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);
}