Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

CASE STUDY REPORT

IMPLEMENTING BACKWARD AND FORWARD


BUTTONS OF BROWSER USING STACK

Introduction to Stacks:
A stack is a collection of objects that are inserted and removed according to the last-in, first-
out (LIFO) principle. A user may insert objects into a stack at any time, but may only access
or remove the most recently inserted object that remains. Stacks are the simplest of all data
structures, yet they are also among the most important. They are used in a host of different
applications, and as a tool for many more sophisticated data structures and algorithms.
Formally, a stack is an abstract data type (ADT). There are different operations that can be
performed on a stack such as:

 Push (): Adds an item in the stack. If the stack is full, then it is said to be an Overflow
condition.
 Pop (): Removes an item from the stack. The items are popped in the reversed order in
which they are pushed.
 Top (): Returns top element of stack.
 Isempty (): Returns true if stack is empty, else false.
 Len(s): Return the number of elements in stack S.
By convention, we assume that a newly created stack is empty, and that there is no a priori
bound on the capacity of the stack. Elements added to the stack can have arbitrary type. The
implementations for top, is empty, and Len use constant time of O(1) in the worst case. The
space usage for a stack is O(n).
They are used in many applications, including Internet Web browsers store the addresses of
recently visited sites in a stack. Each time a user visits a new site, that site’s address is
“pushed” onto the stack of addresses. The browser then allows the user to “pop” back to
previously visited sites using the “back” button.
Implementing backward and forward buttons of browser using stacks:
Stacks can be found in a wide range of computer applications. One of the real-time
application of stack is in a web browser. Almost all web browsers have back and forward
buttons that allows the users to navigate backwards and forward across a sequence of web
pages. The Back button takes us back to the previous web page on your browser. If we click
the back button again, we'll be taken to the previous page, and so on. This works because
the browser is maintaining a stack containing links to web pages. Each time we click the
back button it removes one link from this stack and displays the indicated page.
Each time the user moves to a new web page, the current web page is stored on a stack.
Pressing the back button causes the topmost element of this stack to be popped, and the
associated web page is displayed . To allow the user to move both forward and backward
two stacks are used. When the user presses the back button, the link to the current web page
is stored on a separate stack for the forward button. As the user moved backward through
previous pages, the link to each page is moved in turn from the back to the forward stack.
PSEUDOCODE:
void navigate(newPage) {
currentURL = newPage;
}
void linkVisited(page) {
backStack.push(currentURL);
forwardStack = [];
navigate(page);
}
void goBack() {
forwardStack.push(currentURL);
navigate(backStack.pop());
}
void goForward() {
backStack.push(currentURL);
navigate(forwardStack.pop());
}
The idea is to use two stacks “backStack” and “forwardStack” to keep track of visited URLs
and a variable “currentURL” to store the currently visited URL. Follow the steps below to
solve the problem:

 Initialize currentURL that will store the current URL of the Browser.


 Initialize two stacks forwardStack and backStack that will store the sequence of URLs
accessed when the forward and the backward buttons are pressed respectively.
 While visiting any new URL, push it into backStack.
 While pressing the forward button push currentURL into backStack and pop the last
URL from forwardStack and set it as currentURL.
 While pressing the backward button, push currentURL into forwardStack and pop
the last URL from backStack, and set it as currentURL.

EXAMPLE:
Suppose a user has visited the website “google.com” first. This URL would get stored as
the currentURL. Present stack status is as follows:
currentURL : google.com

backStack:

forwardStack:

Next, the user decides to visit “geekforgeeks.com”. Hence the currentURL- that is
“google.com”- will be pushed into backStack and “geeksforgeeks.com” will be stored as
the currentURL. Present stack status is as follows:

currentURL : geeksforgeeks.com

backStack: google.com

forwardStack:

Next, the user decides to visit “w3schools.com”. Hence the currentURL- that is
“geeksforgeeks.com”- will be pushed into backStack and “w3schools.com” will be stored
as the currentURL. Present stack status is as follows:

currentURL: w3schools.com

backStack: geeksforgeeks.com
google.com

forwardStack:

Now, the user wanted to go back to the previous website and hence would click on the
backward button in the browser. When the user clicks on the back button, the currentURL is
pushed into the forwardStack and the topmost URL in the backStack is popped and it is set as
the new currentURL. Present stack status is as follows:
currentURL: geeksforgeeks.com

backStack: google.com

w3schools.com forwardStack:

If the user wants to go back to website visited before the current website, they could use the
back button of their browser once again and go back to “google.com” following the same
steps as mentioned above:

currentURL: google.com

backStack:

geeksforgeeks.com
forwardStack: w3schools.com
Now if the user wants to navigate forward,
they would click on the forward button in their browser. When the user clicks on the forward
button, the currentURL is pushed into backStack and the topmost element in the
forwardStack is popped and stored as the currentURL. Present stack status is as follows:

currentURL: geeksforgeeks.com

backStack: google.com

forwardStack: w3schools.com

Conclusion:

The stack is a list-like structure in which elements may be inserted or removed from only one
end. Many applications require only the limited form of insert and remove operations that
stacks provide. In such cases, it is more efficient to use the simpler stack data structure rather
than the generic list. The accessible element of the stack is called the top element. Elements
are not said to be inserted, they are pushed onto the stack. When removed, an element is said
to be popped from the stack. The two approaches presented here are array-based and linked
stacks, which are analogous to array-based and linked lists, respectively. There are many
applications of stack. One of them is the implementation of backward and forward button of a
browser. It uses two stacks and a variable to keep track of all the URLs visited by the user
and to navigate backward and forward.

You might also like