Untitled
unknown
plain_text
10 months ago
2.1 kB
17
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
struct Node {
char url[MAX];
struct Node *prev, *next;
} *head = NULL, *curr = NULL;
// ā Visit new page
void visit() {
char url[MAX];
getchar();
printf("Enter URL: ");
fgets(url, MAX, stdin);
url[strcspn(url, "\n")] = '\0';
struct Node *n = malloc(sizeof(struct Node));
strcpy(n->url, url);
n->prev = curr;
n->next = NULL;
if (curr) curr->next = n;
else head = n;
curr = n;
printf("ā
Visited: %s\n", curr->url);
}
// ā¬
ļø Go back x steps
void back() {
int x, c = 0;
if (!curr) return printf("No pages!\n"), (void)0;
printf("Steps to go back: ");
scanf("%d", &x);
while (curr->prev && c < x) curr = curr->prev, c++;
printf("š Back %d step(s): %s\n", c, curr->url);
}
// ā”ļø Go forward x steps
void forward() {
int x, c = 0;
if (!curr) return printf("No pages!\n"), (void)0;
printf("Steps to go forward: ");
scanf("%d", &x);
while (curr->next && c < x) curr = curr->next, c++;
printf("š Forward %d step(s): %s\n", c, curr->url);
}
// šļø Show current page
void showCurr() {
if (curr) printf("š Current: %s\n", curr->url);
else printf("No page visited yet!\n");
}
// š§ Show full history
void showHist() {
if (!head) return printf("History empty!\n"), (void)0;
printf("\nš Browser History:\n");
for (struct Node* t = head; t; t = t->next)
printf("%s%s\n", t == curr ? "-> " : " ", t->url);
}
// š Main menu
int main() {
int ch;
printf("š Browser History using Doubly Linked List\n");
while (1) {
printf("\n1.Visit 2.Back 3.Forward 4.Current 5.History 6.Exit\nEnter choice: ");
scanf("%d", &ch);
switch (ch) {
case 1: visit(); break;
case 2: back(); break;
case 3: forward(); break;
case 4: showCurr(); break;
case 5: showHist(); break;
case 6: printf("š Exiting...\n"); exit(0);
default: printf("ā Invalid!\n");
}
}
}Editor is loading...
Leave a Comment