#include <iostream>
#include <queue>
#include <string>
#include <regex>
using namespace std;
// Node structure for linked implementation of stack
struct Node {
string url;
Node* next;
};
// Class for web browser history tracker
class HistoryTracker {
private:
Node* top; // Pointer to the top of the stack
int size; // Size of the stack
public:
HistoryTracker() {
top = nullptr;
size = 0;
// Add a new URL to the stack
void addUrl(string url) {
Node* newNode = new Node;
newNode->url = url;
newNode->next = top;
top = newNode;
size++;
// Navigate back to the previous URL
string goBack() {
if (isEmpty()) {
cout << "Error: No history available." << endl;
return "";
string url = top->url;
Node* temp = top;
top = top->next;
delete temp;
size--;
return url;
// Navigate forward to the next URL
string goForward() {
if (isEmpty()) {
cout << "Error: No forward history available." << endl;
return "";
Node* newTop = top->next;
string url = newTop->url;
Node* temp = top;
top = newTop;
delete temp;
size++;
return url;
// Check if the stack is empty
bool isEmpty() {
return top == nullptr;
// Get the size of the stack
int getSize() {
return size;
};
// Main function
int main() {
HistoryTracker historyTracker;
// Read initial URLs from file
ifstream file("C:\\Data\\BrowserHistory.txt");
string url;
while (getline(file, url)) {
if (validateURL(url)) {
historyTracker.addUrl(url);
} else {
cout << "Error: Invalid URL format - " << url << endl;
file.close();
// Main menu loop
int choice;
while (true) {
cout << "\n1. Add a new URL" << endl;
cout << "2. Navigate back" << endl;
cout << "3. Navigate forward" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
string newUrl;
switch (choice) {
case 1:
cout << "Enter a new URL: ";
cin >> newUrl;
if (validateURL(newUrl)) {
historyTracker.addUrl(newUrl);
} else {
cout << "Error: Invalid URL format - " << newUrl << endl;
break;
case 2:
if (!historyTracker.isEmpty()) {
cout << "Navigating back to - " << historyTracker.goBack() << endl;
} else {
cout << "Error: No history available." << endl;
break;
case 3:
if (historyTracker.getSize() > 1) {
cout << "Navigating forward to - " << historyTracker.goForward() << endl;
} else {
cout << "Error: No forward history available." << endl;
break;
case 4:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
break;
return 0;
// Function to validate URL using regular expression
bool validateURL(string url) {
regex pattern("www\\..+\\..+");
return regex_match(url, pattern);