0% found this document useful (0 votes)
21 views6 pages

BrowserHistory.txt.cpp

The document presents a C++ implementation of a web browser history tracker using a linked list structure. It allows users to add URLs, navigate back and forth in their history, and validates URL formats using regular expressions. The main function includes a menu for user interaction and handles URL input from a file.

Uploaded by

Lebo Modika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views6 pages

BrowserHistory.txt.cpp

The document presents a C++ implementation of a web browser history tracker using a linked list structure. It allows users to add URLs, navigate back and forth in their history, and validates URL formats using regular expressions. The main function includes a menu for user interaction and handles URL input from a file.

Uploaded by

Lebo Modika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

#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);

You might also like