Learn-dsa..in 30 days!



























CC-12 : Simulate Browser History using DoubleLinkedList.

Description:

Write and test a program to simulate Browser History using Double Linked List.

Test cases and expected outputs:

Input Parameters Expected outputs
Browser History Simulation can be shown in following format: “Page1->Page2->Page3->Page4->Page5 (Current Page)”.
User’s current Position in the Browser History Simulation should be tagged by the tag: “(Current Page)”.

User should have option to move to next page or previous page in the Browser History Simulation:

If User moves to previous page, Browser History Simulation should show:
Page1->Page2->Page3->Page4(Current Page)->Page5.
If User again moves to previous page, Browser History Simulation should show: Page1->Page2->Page3(Current Page)->Page4->Page5.
If User again moves to previous page, Browser History Simulation should show: Page1->Page2(Current Page)->Page3->Page4->Page5.
If User again moves to next page, Browser History Simulation should show: Page1->Page2->Page3(Current Page)->Page4->Page5.
If user navigates to a new page from current page, all history nodes after current node should be deleted and new page should be added as next page after current page. Browser History Simulation should show: Page1->Page2->Page3->Page6(Current Page).
Page4 and Page5 will no longer be part of browser history after this operation.

Pseudocode:

Initialize a DoubleLinkedList by the name of browserHistory and a DoubleLinkedListNode by the name of currentPage
To simulate “Visit a new Page”, create a new DoubleLinkedListNode, add an integer page number to it as data value and add the node to the end of browserHistory DoubleLinkedList and set the new node as currentPage.
To simulate “Visit Previous Page”, navigate to previous node of currentPage DoubleLinkedListNode using PrevNode link. Also set previous node as currentPage now. Handle special case of browserHistory being empty or currentPage not having any previous node.
To simulate “Visit Next Page”, navigate to next node of currentPage DoubleLinkedListNode using nextNode link. Also set next node as currentPage now. Handle special case of browserHistory being empty or currentPage not having any next node.
Print browserHistory after each operation.

Code:

public class DoubleLinkedListBrowserHistory {

private int newPageCounter=0;
private DoubleLinkedListNode currentPageInBrowserHistory=null;
private DoubleLinkedList browserHistory=null;

public void doubleLinkedListBrowserHistory(int action) {
	try {
	switch(action) {
	case 1://VISIT_NEW_PAGE
	newPageCounter++;
	if ((currentPageInBrowserHistory==null)||
	((currentPageInBrowserHistory!=null)&&(currentPageInBrowserHistory.getNextNode()==null))){
	browserHistory.addLast(newPageCounter);
	currentPageInBrowserHistory=browserHistory.getLastNode();
	printBrowserHistory(browserHistory,currentPageInBrowserHistory);
	}else if ((currentPageInBrowserHistory!=null)&&(currentPageInBrowserHistory.getNextNode()!=null)) {
	currentPageInBrowserHistory.getNextNode().setPreviousNode(null);
	currentPageInBrowserHistory.setNextNode(null);
	browserHistory.addLast(newPageCounter);
	currentPageInBrowserHistory=browserHistory.getLastNode();
	printBrowserHistory(browserHistory,currentPageInBrowserHistory);
	}
	break;
	case 2: //VISIT_PREVIOUS_PAGE_IN_BR_HISTORY 
	if ((currentPageInBrowserHistory==null)|| 
	((currentPageInBrowserHistory!=null)&&(currentPageInBrowserHistory.getPreviousNode()==null))) {
	System.out.println("\n\n No Previous Page exists !\n"); 
	printBrowserHistory(browserHistory,currentPageInBrowserHistory);
	break;
	}else {
	currentPageInBrowserHistory=currentPageInBrowserHistory.getPreviousNode();		
	printBrowserHistory(browserHistory,currentPageInBrowserHistory);
	}
	break;
	case 3: //VISIT_NEXT_PAGE_IN_BR_HISTORY 
	if ((currentPageInBrowserHistory==null)|| 
	((currentPageInBrowserHistory!=null)&&(currentPageInBrowserHistory.getNextNode()==null))) {
	System.out.println("\n\n No Next Page exists !\n"); 
	printBrowserHistory(browserHistory,currentPageInBrowserHistory);
	break;
	}else {
	currentPageInBrowserHistory=currentPageInBrowserHistory.getNextNode();		
	printBrowserHistory(browserHistory,currentPageInBrowserHistory);
	}
	 break;
	default: //user has input an option that is not available
	System.out.print("\n Invalid option selected; please use one of the options given above: ");
	break;
	}
	}catch (Exception exception) {
	System.out.print("Exception: "+ exception);
	}
}
}

Click here to download and run code and test cases !