The most frequently used buttons in a browser are the forward and backward butto
ID: 3780474 • Letter: T
Question
The most frequently used buttons in a browser are the forward and backward buttons to traverse the URLs visited by the user. Based on the doubly linked lists data structure, write a C++ program to maintain the browsing history in the following ways. Design a class BrowsingHistory to maintain the browsing history of a user. Each node should store the URL (string) of a web page. The links of a node represent the previous and next page visited by the user. Write a CurrentPage function to return the URL of the current web page under browsing. Also provide a NewPage function to add a new URL into the history. Write a Forward and a Backward function to traverse the browsing history and return the URL of the new current page. Provide a Display function to print out the entire browsing history. Test your functions properly in the main program.Explanation / Answer
PROGRAM CODE:
#include <iostream>
using namespace std;
class BrowsingHistory
{
private:
string URL;
BrowsingHistory *next;
BrowsingHistory *prev;
public:
BrowsingHistory()
{
URL = "";
next = NULL;
prev = NULL;
}
BrowsingHistory(string url)
{
URL = url;
next = NULL;
prev = NULL;
}
BrowsingHistory(BrowsingHistory *bh)
{
URL = bh->URL;
next = bh->next;
prev = bh->prev;
}
string currentPage()
{
return URL;
}
void newPage()
{
cout<<"Enter the next address: ";
string nexturl;
cin>>nexturl;
next = new BrowsingHistory(nexturl);
prev = new BrowsingHistory(this);
next->next = new BrowsingHistory("End of history");
}
string forward()
{
BrowsingHistory current = this;
this->URL = current.next->URL;
this->next = current.next->next;
this->prev = ¤t;
return this->URL;
}
string backward()
{
if(prev == NULL)
return "Beginning of history";
else
{
BrowsingHistory previous = this->prev;
BrowsingHistory current = this;
this->URL = previous.URL;
this->next = ¤t;
this->prev = previous.prev;
return this->URL;
}
}
void display()
{
cout<<" Browsing History: "<<endl;
BrowsingHistory current = this;
while(current.prev != NULL)
{
current = current.prev;
}
while(current.URL != "End of history")
{
cout<<current.URL<<endl;
current = current.next;
}
}
}*his;
int main() {
his = new BrowsingHistory("www.google.com");
cout<<"Current Page: "<<his->currentPage()<<" ";
his->newPage();
cout<<" Current Page: "<<his->currentPage()<<" ";
cout<<" Going forward: "<<his->forward();
cout<<" Going backward: "<<his->backward();
cout<<" Displaying the history"<<endl;
his->display();
return 0;
}
OUTPUT:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.