Question

So I am working on an assignment where we make a rudimentary browser history with c#....

So I am working on an assignment where we make a rudimentary browser history with c#. The requirement is that we need to use a linked list to do this. The issue that I am having is that when I want to go backwards in the history and print it takes from the beginning of the list and not the front. (Example is if I had a list with 1, 2, 3, 4 in it and went back It would be 2, 3, 4 and 1 gets moved to the future category).

Here is what I currently have:

public class UnderflowException : Exception

    { public UnderflowException(string s) : base(s) { } }

    public class OverflowException : Exception

    { public OverflowException(string s) : base(s) { } }

    class History

    {

        protected class IntListNode

        {                          

            public string Data;

            public IntListNode Next;

            public IntListNode(string data)

            {

                Data = data;

            }

            public IntListNode(string data, IntListNode next)

            {

                Next = next;

            }

        }

        protected IntListNode first;

        private int i;

        public void PrintAll()

        {

            int j = 0;

            IntListNode node = first;

            Console.WriteLine("Privious things that you have viewed.");

            while (node != null)

            {

                if (counter <= j)

                {

                    break;

                }

                Console.WriteLine(node.Data);

                node = node.Next;

                j++;

            }

            Console.WriteLine("Things to go forward to.");

            while (node != null)

            {

                Console.WriteLine(node.Data);

                node = node.Next;

                j++;

            }

           

        }

        private int counter;

        public void MoveBackwards()

        {

            if(counter >= 0)

            {

                counter = counter -1;

            }

            else

            {

                throw new UnderflowException("underflow");

            }

        }

        public void MoveForwards()

        {

            if (counter > i)

            {

                throw new OverflowException("overflow");

            }

            else

            {

                counter++;

            }

            counter++;

        }

        public void VisitPage(string desc)

        {

            IntListNode n = new IntListNode(desc);

            n.Next = this.first;

            this.first = n;

            counter++;

            i = counter;

        }

    }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

**this is my code . If you have any doubt then write to me in comment section.**

Here's an example based on your code, though changed slightly.

Instead of tracking counter and i for our state, I just keep track of two nodes: head (the first one) and current (the one the user is on right now). I'm also inserting new pages at the current.Next node instead of at the headnode because that's how I'm used to using a linked list.

By doing this, it makes navigation easy. To go forward, we just set current = current.Next, and to go backward we start at the head and move forward until we find the node whose Next is pointing to current. Then we set current to that node.

To print out the history, we just start at the head and keep moving Next. When we see that Next == current, we know we're at the current page (and I print that in a different color). Then we can keep printing the Nextnodes to show the future nodes, until Next is null.

Note that this is really navigation history, not a complete record of browsing history, because if you go back and then visit a new page, you lose the page you went back from.

Hope this helps:

class History
{
    private class Node
    {
        public string Data { get; set; }
        public Node Next { get; set; }
        public Node(string data) { Data = data; }
    }

    private Node head;
    private Node current;

    public void VisitNewPage(string desc)
    {
        // Create a node for this page
        var node = new Node(desc);

        // If it's our first page, set the head
        if (head == null) head = node;

        // Update our current.Next pointer
        if (current != null) current.Next = node; 

        // Set this page as our current page
        current = node;
    }

    public void MoveBackwards()
    {
        // Can't move backwards from the head
        if (current == head) return;

        var previous = head;

        // Find the node that's behind (pointing to) the current node
        while (previous.Next != current)
        {
            previous = previous.Next;
        }

        // Make that node our new current
        current = previous;
    }

    public void MoveForwards()
    {
        // Just move to the next node
        if (current.Next != null) current = current.Next;
    }

    public void PrintCurrent()
    {
        Console.WriteLine($"You are on page: {current.Data}");
    }

    public void PrintHistory()
    {
        Console.WriteLine("
Browsing History");

        if (head == null)
        {
            Console.WriteLine("[Empty]");
            return;
        }

        var node = head;

        // Print previous pages
        while (node != current)
        {
            Console.WriteLine($" - {node.Data}");
            node = node.Next;
        }

        // Print current page in green
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine($" - {node.Data}");
        Console.ResetColor();
        node = node.Next;

        // Print next pages
        while (node != null)
        {
            Console.WriteLine($" - {node.Data}");
            node = node.Next;
        }

        Console.WriteLine();
    }
}

Sample Usage

Here's a simple infinite loop that lets you visit new sites, move forwards, backwards, and print the history:

private static void Main()
{
    var history = new History();

    while (true)
    {
        Console.Write("Enter new page to visit, [b]ack, [f]orward, or [p]rint: ");
        var input = Console.ReadLine();
        if (string.IsNullOrWhiteSpace(input)) continue;

        switch (input.ToLower())
        {
            case "b":
            case "back":
                history.MoveBackwards();
                break;
            case "f":
            case "forward":
                history.MoveForwards();
                break;
            case "p":
            case "print":
                history.PrintHistory();
                break;
            default:
                history.VisitNewPage(input);
                break;
        }
    }
}

Output:

Add a comment
Know the answer?
Add Answer to:
So I am working on an assignment where we make a rudimentary browser history with c#....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)...

    Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)    {        this.size = size;    }    /** Reference to list head. */    private Node<E> head = null;    /** The number of items in the list */    private int size = 0;       /** Add an item to the front of the list.    @param item The item to be added    */    public void addFirst(E...

  • This is my code for family tree, but i am confused on how to make a...

    This is my code for family tree, but i am confused on how to make a main class that will use these methods.   public class FamilyTree { private class Node { String name; Node next; Node child; public Node(String name) { this.name = name; next = null; child = null; } }    public Node addSibling(Node node, String name) { if(node == null) return null; while(node.next != null) node = node.next; return(node.next = new Node(name)); }    public Node addChild(Node...

  • I cannot figure out why my removeAll for var = 7 is not working. using System;...

    I cannot figure out why my removeAll for var = 7 is not working. using System; using System.Collections.Generic; using System.Linq; using System.Text; using UnorderedArrayListNamespace; namespace Array { public class Program { public static void Main(string[] args) { UnorderedArrayList u = new UnorderedArrayList(); u.print(); int var = 5; u.insert(ref var); var = 12; u.insert(ref var); var = 2; u.insert(ref var); var = 29; u.insert(ref var); var = 7; u.insert(ref var); var = 33; u.insert(ref var); var = 49; u.insert(ref var); var...

  • I am trying to make a linked list queue and I am trying to use the...

    I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not. public class PlaneSimulation { public static void main(String[] args) { int landTime = 2; int takeoffTime = 3; int avgArrivalInterval =...

  • C# Hey I am having trouble implementing additonal function to this assignment: Problem: Implement three IComparer classes on Employee - NameComparer, AgeComparer, and PayComparer - that allow Employee...

    C# Hey I am having trouble implementing additonal function to this assignment: Problem: Implement three IComparer classes on Employee - NameComparer, AgeComparer, and PayComparer - that allow Employees to be compared by the Name, Age, and Pay, respectively. Code: Employee.Core.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Employees { partial class Employee { // Field data. private string empName; private int empID; private float currPay; private int empAge; private string empSSN = ""; private string empBene...

  • Currently working on a Java Assignment. I have written most codes for swap, reverse and insert....

    Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...

  • Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a...

    Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a Circular Doubly Linked List and am having serious trouble creating a method retainAll. Here's the code, and my attempt. Initialization: public class CDoublyLinkedList {    private class Node {        private Object data; //Assume data implemented Comparable        private Node next, prev;        private Node(Object data, Node pref, Node next)        {            this.data = data;       ...

  • Improve the speed of public T get(int i) by having it work backward from the end...

    Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> {    private static class Node<T> {        public Node<T> prev, next;...

  • Can anyone helps to create a Test.java for the following classes please? Where the Test.java will...

    Can anyone helps to create a Test.java for the following classes please? Where the Test.java will have a Scanner roster = new Scanner(new FileReader(“roster.txt”); will be needed in this main method to read the roster.txt. public interface List {    public int size();    public boolean isEmpty();    public Object get(int i) throws OutOfRangeException;    public void set(int i, Object e) throws OutOfRangeException;    public void add(int i, Object e) throws OutOfRangeException; public Object remove(int i) throws OutOfRangeException;    } public class ArrayList implements List {   ...

  • i need help to modify this Tree class to include a method leavesCount that returns the...

    i need help to modify this Tree class to include a method leavesCount that returns the number of leaves in a binary tree. (NOTE: Complete leavesCount and leavesCountHelper methods in the Tree class). I already have the test class. java class TreeNode< T extends Comparable< T > > { TreeNode< T > leftNode; T data; TreeNode< T > rightNode; public TreeNode( T nodeData ) { data = nodeData; leftNode = rightNode = null; } public void insert( T insertValue )...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT