0% found this document useful (0 votes)
9 views17 pages

Assignment - Data Structures Final

The document outlines a project to create a browser history simulator using a stack data structure that tracks web pages visited by the user. It describes the implementation of stack operations such as Push, Pop, Peek, and Display All, allowing users to navigate their browsing history. The program can store up to 100 web pages, each with a unique ID, URL, and visit time, and includes a user interface for interaction.

Uploaded by

qurratain
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views17 pages

Assignment - Data Structures Final

The document outlines a project to create a browser history simulator using a stack data structure that tracks web pages visited by the user. It describes the implementation of stack operations such as Push, Pop, Peek, and Display All, allowing users to navigate their browsing history. The program can store up to 100 web pages, each with a unique ID, URL, and visit time, and includes a user interface for interaction.

Uploaded by

qurratain
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PART I

INTRODUCTION

In today's web browsers, navigation history is crucial for enhancing the user experience, as it
enables easy movement between web pages you've already viewed. This challenge entails
emulating a feature utilising the stack data structure, which functions on the Last-In-First-Out
(LIFO) principle. When a user navigates to a page, it is added to the top of the stack. When
they click the Back button, the most recent entry is removed, and the user goes back to the
previous page.

The purpose of this project is to utilise dynamic memory allocation to create a pointer-based
stack system that tracks a browser's history. The system can store up to 100 web pages, each
with its own unique page ID, URL, and visit time. Users will interact with the simulated
surfing environment by using well-defined stack operations, including Push, Pop, Peek, and
Display All. A test software will verify that the system functions correctly by performing a
set of tasks that simulate how people typically browse the web.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_URL_LENGTH 100


#define MAX_TIME_LENGTH 20
#define MAX_STACK_SIZE 100

typedef struct {
int pageID;
char URL[MAX_URL_LENGTH + 1];
char visitTime[MAX_TIME_LENGTH + 1];
} WebPage;

typedef struct StackNode {


WebPage data;
struct StackNode* next;
} StackNode;

typedef struct {
StackNode* top;
int size;
} Stack;

void initStack(Stack* s) {
s->top = NULL;
s->size = 0;
}

int isFull(Stack* s) {
return s->size >= MAX_STACK_SIZE;
}

int isEmpty(Stack* s) {
1
return s->top == NULL;
}

void push(Stack* s, WebPage page) {


if (isFull(s)) {
printf("\nError: History full! Cannot add '%s' (max %d pages)\n", [Link],
MAX_STACK_SIZE);
return;
}

StackNode* newNode = (StackNode*)malloc(sizeof(StackNode));


if (newNode == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}

newNode->data = page;
newNode->next = s->top;
s->top = newNode;
s->size++;
printf("\nAdded: %s\n", [Link]);
}

void pop(Stack* s) {
if (isEmpty(s)) {
printf("\nError: History empty! Cannot go back.\n");
return;
}

StackNode* temp = s->top;


WebPage poppedPage = temp->data;
s->top = s->top->next;
free(temp);
s->size--;
2
printf("\nBack button pressed. Removed: %s\n", [Link]);

if (s->top) {
printf("Current page: %s\n", s->top->[Link]);
} else {
printf("History is now empty.\n");
}
}

void peek(Stack* s) {
if (isEmpty(s)) {
printf("\nHistory is empty. No current page.\n");
return;
}
printf("\nCurrent page:\n");
printf("ID: %d, URL: %s, Time: %s\n",
s->top->[Link],
s->top->[Link],
s->top->[Link]);
}

void displayAll(Stack* s) {
if (isEmpty(s)) {
printf("\nHistory is empty.\n");
return;
}

printf("\nBrowser History (Most recent first):\n");


printf("----------------------------------\n");

StackNode* current = s->top;


int count = 1;

3
while (current != NULL) {
printf("%d. [ID: %d] %s - Visited at %s\n",
count++,
current->[Link],
current->[Link],
current->[Link]);
current = current->next;
}
printf("----------------------------------\n");
}

void freeStack(Stack* s) {
while (s->top != NULL) {
StackNode* temp = s->top;
s->top = s->top->next;
free(temp);
}
s->size = 0;
}

int main() {
Stack browserHistory;
initStack(&browserHistory);
int choice;
WebPage newPage;

printf("===== Web Browser History Simulator =====\n");

while (1) {
printf("\nMenu:\n");
printf("1. Visit new page\n");
printf("2. Press Back button\n");
printf("3. View current page\n");
printf("4. View full history\n");
4
printf("5. Exit\n");
printf("Enter choice: ");

if (scanf("%d", &choice) != 1) {
printf("\nInvalid input! Please enter a number.\n");
while (getchar() != '\n');
continue;
}

switch (choice) {
case 1:
printf("\nEnter Page ID: ");
scanf("%d", &[Link]);
getchar();

printf("Enter URL: ");


fgets([Link], MAX_URL_LENGTH, stdin);
[Link][strcspn([Link], "\n")] = '\0';

printf("Enter visit time (e.g., 3:30 PM): ");


fgets([Link], MAX_TIME_LENGTH, stdin);
[Link][strcspn([Link], "\n")] = '\0';

push(&browserHistory, newPage);
break;

case 2:
pop(&browserHistory);
break;

case 3:
peek(&browserHistory);
break;

5
case 4:
displayAll(&browserHistory);
break;

case 5:
freeStack(&browserHistory);
printf("\nExiting program. History cleared.\n");
return 0;

default:
printf("\nInvalid choice! Please choose 1-5.\n");
}
}
}

6
OUTPUT SCREENSHOTS SHOWING PROGRAM RESULTS

 INSERTS AT LEAST FIVE WEB PAGES INTO THE HISTORY


USING THE PUSH FUNCTION

7
 DISPLAYS ALL PAGES IN THE STACK

8
 USES PEEK FUNCTION TO DISPLAY THE CURRENT PAGE

9
 REMOVES THE LAST TWO VISITED PAGES USING POP FUNCTION

10
 DISPLAYS THE UPDATED HISTORY AFTER DELETIONS

11
12
PART II

POSTINGS
1
2
3

You might also like