DSA Case Study PDF
Document Details
P.Manmayi & M.J.S.Samanvitha
Tags
Summary
This document details a case study on browser navigation simulation using stacks. It explores the implementation of stack data structures for managing 'back' and 'forward' browsing history. The study includes code examples using C.
Full Transcript
DSA CASE STUDY P.MANMAYI - 2023001961 M.J.S.SAMANVITHA - 2023001970 22. Browser Back/Forward Navigation: Model the back and forward navigation of a web browser using stacks to keep track of visited pages. CODE: #include #include #include #define MAX 100 #define PAGE_SIZE...
DSA CASE STUDY P.MANMAYI - 2023001961 M.J.S.SAMANVITHA - 2023001970 22. Browser Back/Forward Navigation: Model the back and forward navigation of a web browser using stacks to keep track of visited pages. CODE: #include #include #include #define MAX 100 #define PAGE_SIZE 100 // Stack structure for browser history typedef struct { char pages[MAX][PAGE_SIZE]; // stack to store URLs int top; } Stack; // Function to initialize the stack void init(Stack *s) { s->top = -1; } // Function to check if the stack is empty int isEmpty(Stack *s) { return s->top == -1; } // Function to check if the stack is full int isFull(Stack *s) { return s->top == MAX - 1; } // Function to push an item onto the stack void push(Stack *s, const char *page) { if (!isFull(s)) { strcpy(s->pages[++s->top], page); } else { printf("Stack is full!\n"); } } // Function to pop an item from the stack char* pop(Stack *s) { if (!isEmpty(s)) { return s->pages[s->top--]; } else { printf("Stack is empty!\n"); return NULL; } } // Function to view the top item of the stack char* peek(Stack *s) { if (!isEmpty(s)) { return s->pages[s->top]; } else { return NULL; } } int main() { Stack backStack, forwardStack; char current_page[PAGE_SIZE] = "homepage"; char page[PAGE_SIZE]; int choice; // Initialize the stacks init(&backStack); init(&forwardStack); while (1) { printf("\nCurrent page: %s\n", current_page); printf("1. Visit new page\n2. Back\n3. Forward\n4. Exit\n"); printf("Enter your choice: "); if (scanf("%d", &choice) != 1) { printf("Invalid input. Please enter a valid number.\n"); while (getchar() != '\n'); // clear input buffer continue; } switch (choice) { case 1: // Visit a new page printf("Enter the new page URL: "); getchar(); // clear newline left by scanf fgets(page, PAGE_SIZE, stdin); page[strcspn(page, "\n")] = '\0'; // remove trailing newline // Push the current page onto the back stack push(&backStack, current_page); // Clear the forward stack when a new page is visited init(&forwardStack); // Set the new page as the current page strcpy(current_page, page); break; case 2: // Go back to the previous page if (!isEmpty(&backStack)) { // Push the current page to the forward stack push(&forwardStack, current_page); // Pop from the back stack to get the previous page strcpy(current_page, pop(&backStack)); } else { printf("No pages in the back history!\n"); } break; case 3: // Go forward to the next page if (!isEmpty(&forwardStack)) { // Push the current page to the back stack push(&backStack, current_page); // Pop from the forward stack to get the next page strcpy(current_page, pop(&forwardStack)); } else { printf("No pages in the forward history!\n"); } break; case 4: printf("Exiting...\n"); exit(0); default: printf("Invalid choice! Please enter a number between 1 and 4.\n"); } } return 0; } OUTPUT: Logics Used: 1. Browser Navigation Simulation: The main idea behind the code is to simulate a browser's "back" and "forward" navigation using two stacks. The backStack keeps track of pages visited before the current page, and the forwardStack stores the pages you move forward to if you've gone back. 2. Visiting a New Page: When a new page is visited, the current page is pushed onto the backStack. The forwardStack is cleared because any forward history becomes irrelevant when you visit a new page. 3. Back Navigation: If the user chooses to go back, the current page is pushed onto the forwardStack, and the most recent page from the backStack becomes the current page. If the backStack is empty, it means there's no page to go back to. 4. Forward Navigation: If the user navigates back and then wants to move forward, the current page is pushed onto the backStack, and the top page from the forwardStack becomes the current page. If the forwardStack is empty, it means there is no forward history available. 5. Error Handling: The code checks if the stack is full before pushing and if the stack is empty before popping to avoid stack overflow or underflow errors. Input validation ensures the user enters valid choices. Data Structures Used: 1. Stack: The stack is implemented using an array and an integer variable top to keep track of the index of the last element added. This is used to manage both the backStack and the forwardStack. Key operations for the stack include: push: Adds an element to the top of the stack. pop: Removes the top element from the stack. peek: Returns the top element without removing it from the stack. isEmpty: Checks if the stack is empty. isFull: Checks if the stack is full. 2. String Array: The pages array within the Stack struct is used to store the URLs as strings. Each page is stored in a 2D character array pages[MAX][PAGE_SIZE], where MAX is the maximum number of URLs that can be stored, and PAGE_SIZE is the maximum length of a URL. Summary: This program simulates a basic browser history system using two stacks, backStack and forwardStack, to allow the user to navigate back and forward between visited pages. It supports operations like visiting a new page (which clears the forward history), moving back to the previous page, and moving forward. The stack ensures that the navigation is properly managed, allowing the user to return to previously visited pages in the correct sequence. It demonstrates fundamental stack operations like push and pop, along with error handling for stack overflow and underflow conditions.