Repository navigation
Expand file tree
/
Copy pathbrowserstackhis.c
More file actions
109 lines (95 loc) · 3 KB
/
Copy pathbrowserstackhis.c
File metadata and controls
109 lines (95 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdio.h>
#include <string.h>
#define N 20
/*Harshada Avhad
CMPN-B
24102B0023
*/
typedef struct stack {
char a[N][100]; // Array to store page URLs (each URL is a string)
int top;
} stack;
void push(stack *s, char *x) {
if (s->top == N - 1)
printf("\nStack Overflow...");
else {
s->top = s->top + 1;
strcpy(s->a[s->top], x);
}
}
int isempty(stack *s) {
return (s->top == -1);
}
char* pop(stack *s) {
if (isempty(s))
return NULL;
else {
return s->a[s->top--];
}
}
char* peek(stack *s) {
if (isempty(s))
return NULL;
else
return s->a[s->top];
}
void display(stack *s) {
if (isempty(s)) {
printf("\nStack is empty...");
} else {
for (int i = s->top; i >= 0; i--) {
printf("\t%s", s->a[i]);
}
}
}
int main() {
int ch;
char url[100];
stack backStack, forwardStack;
char currentPage[100] = "Home"; // Starting page
backStack.top = -1;
forwardStack.top = -1;
while (1) {
printf("\n\nCurrent Page: %s", currentPage);
printf("\n1: Visit New Page\n2: Back\n3: Forward\n4: Display History\n5: Exit\nEnter choice: ");
scanf("%d", &ch);
if (ch == 5)
break;
switch (ch) {
case 1: // Visit new page
printf("\nEnter URL of new page: ");
scanf("%s", url);
push(&backStack, currentPage); // Push the current page to the back stack
strcpy(currentPage, url); // Set the new page as the current page
while (!isempty(&forwardStack)) { // Clear the forward stack
pop(&forwardStack);
}
break;
case 2: // Back
if (isempty(&backStack)) {
printf("\nNo pages in the back history.");
} else {
push(&forwardStack, currentPage); // Push the current page to the forward stack
strcpy(currentPage, pop(&backStack)); // Set the top of backStack as the current page
}
break;
case 3: // Forward
if (isempty(&forwardStack)) {
printf("\nNo pages in the forward history.");
} else {
push(&backStack, currentPage); // Push the current page to the back stack
strcpy(currentPage, pop(&forwardStack)); // Set the top of forwardStack as the current page
}
break;
case 4: // Display history
printf("\nBack History: ");
display(&backStack);
printf("\nForward History: ");
display(&forwardStack);
break;
default:
printf("\nInvalid Choice...");
}
}
return 0;
}