DS Project Code For Browsing History
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_URL_LEN 100
// Node structure
typedef struct Node {
char url[MAX_URL_LEN];
struct Node* prev;
struct Node* next;
} Node;
Node* current = NULL;
// Function to visit a new page
void visit(char* url) {
Node* newPage = (Node*)malloc(sizeof(Node));
if (!newPage) {
printf("Memory allocation failed.\n");
return;
strcpy(newPage->url, url);
newPage->prev = current;
newPage->next = NULL;
// If there's a forward history, delete it
if (current && current->next) {
Node* temp = current->next;
while (temp) {
Node* toDelete = temp;
temp = temp->next;
free(toDelete);
current->next = NULL;
if (current != NULL)
current->next = newPage;
current = newPage;
printf("Visited: %s\n", url);
// Function to go back
void back() {
if (current != NULL && current->prev != NULL) {
current = current->prev;
printf("Back to: %s\n", current->url);
} else {
printf("No previous page.\n");
// Function to go forward
void forward() {
if (current != NULL && current->next != NULL) {
current = current->next;
printf("Forward to: %s\n", current->url);
} else {
printf("No forward page.\n");
// Function to show current page
void showCurrent() {
if (current != NULL) {
printf("Current page: %s\n", current->url);
} else {
printf("No page visited yet.\n");
// Function to show full history
void showHistory() {
Node* temp = current;
// Go to the first page
while (temp && temp->prev != NULL)
temp = temp->prev;
printf("Full History:\n");
while (temp != NULL) {
if (temp == current)
printf("%s <-- current\n", temp->url);
else
printf("%s\n", temp->url);
temp = temp->next;
// Function to free the entire history
void clearHistory() {
// Go to the beginning
while (current && current->prev != NULL)
current = current->prev;
// Free all nodes
Node* temp = current;
while (temp) {
Node* toDelete = temp;
temp = temp->next;
free(toDelete);
int main() {
int choice;
char url[MAX_URL_LEN];
do {
printf("\n===== Browser History Menu =====\n");
printf("1. Visit new page\n");
printf("2. Go back\n");
printf("3. Go forward\n");
printf("4. Show current page\n");
printf("5. Show full history\n");
printf("0. Exit\n");
printf("Choose an option: ");
scanf("%d", &choice);
getchar(); // clear newline from buffer
switch (choice) {
case 1:
printf("Enter URL: ");
fgets(url, MAX_URL_LEN, stdin);
url[strcspn(url, "\n")] = 0; // Remove newline
visit(url);
break;
case 2:
back();
break;
case 3:
forward();
break;
case 4:
showCurrent();
break;
case 5:
showHistory();
break;
case 0:
printf("Exiting...\n");
clearHistory();
break;
default:
printf("Invalid choice.\n");
} while (choice != 0);
return 0;
}