- 1 hour
- 31 mins
- 4
The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Cod - Guest
15th April 2025 08:36:14 PM
C
67 views
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- typedef struct page {
- int id;
- char url[50];
- char *description;
- } page;
- typedef struct stack {
- struct page *top;
- } stack;
- typedef struct tab {
- int id;
- struct page *currentPage;
- struct stack *backwardStack;
- struct stack *forwardStack;
- } tab;
- typedef struct List {
- struct tab *data;
- struct List *next;
- struct List *prev;
- } List;
- typedef struct tabsList {
- struct List *sant;
- } tabsList;
- typedef struct browser {
- struct tab *current;
- struct tabsList list;
- } browser;
- void createPage(struct page *p, int id, char *url, char *description) {
- p->id = id;
- p->description = strdup(description);
- }
- void initTab(struct tab *t) {
- t->id = 0;
- createPage(t->currentPage, 0, "https://acs.pub.ro/", "Computer Science");
- t->backwardStack->top = NULL;
- t->forwardStack->top = NULL;
- }
- tab* createTab(struct tab *t, int id_tab, int id, char *url, char *description) {
- t->id = id_tab;
- createPage(t->currentPage, id, url, description);
- t->backwardStack->top = NULL;
- t->forwardStack->top = NULL;
- return t;
- }
- void freeTab(struct tab *t) {
- if (t->currentPage) {
- }
- }
- void createBrowser(struct browser *b) {
- // Initialize the sentinel node
- b->list.sant->data = NULL;
- b->list.sant->next = b->list.sant;
- b->list.sant->prev = b->list.sant;
- // Create the first tab
- createTab(new->data, 0, 0, "https://acs.pub.ro/", "Computer Science");
- // Insert the new tab after the sentinel node
- new->next = b->list.sant->next;
- new->prev = b->list.sant;
- b->list.sant->next->prev = new;
- b->list.sant->next = new;
- // Set the current tab
- b->current = new->data;
- }
- void freeBrowser(struct browser *b) {
- if (b->list.sant == NULL) return;
- struct List *aux = b->list.sant->next;
- while (aux != b->list.sant) {
- struct List *next = aux->next;
- freeTab(aux->data); // Eliberăm tabul asociat nodului
- aux = next;
- }
- // Eliberăm memoria pentru sentinelă
- b->list.sant = NULL;
- b->current = NULL; // Resetăm tabul curent
- }
- void freePages(struct page pages[51], int n) {
- for (int i = 0; i < n; i++) {
- }
- }
- void read(FILE *f, struct page pages[51], char comands[501][51], int *n, int *m) {
- int i;
- for (i = 0; i < *n; i++) {
- char buffer[501];
- pages[i].description = strdup(buffer);
- }
- for (i = 0; i < *m; i++) {
- }
- }
- void NEW_TAB(struct browser *b) {
- // Generate a new ID
- int new_id = b->current->id + 1;
- // Initialize the new tab
- new->data = createTab(new->data, new_id, 0, "https://acs.pub.ro/", "Computer Science");
- if (b->list.sant) {
- if (b->current) {
- // Insert the new tab after the current tab
- struct List *aux = b->list.sant->next;
- while (aux->data != b->current) {
- aux = aux->next;
- }
- new->next = aux->next;
- new->prev = aux;
- aux->next->prev = new;
- aux->next = new;
- } else {
- // If no current tab, insert after the sentinel node
- new->next = b->list.sant->next;
- new->prev = b->list.sant;
- b->list.sant->next->prev = new;
- b->list.sant->next = new;
- }
- } else {
- // If the list is empty, initialize the sentinel node
- new->next = new->prev = new;
- b->list.sant = new;
- }
- // Set the new tab as the current tab
- b->current = new->data;
- }
- void CLOSE(struct browser *b, FILE *g) {
- struct List *aux = b->list.sant->next;
- while (aux->data != b->current) {
- aux = aux->next;
- }
- if(aux->data->id == 0) {
- } else {
- // Remove the current tab from the list
- aux->prev->next = aux->next;
- aux->next->prev = aux->prev;
- // Set the current tab to the previous one, if it exists
- if (aux->prev != b->list.sant) {
- b->current = aux->prev->data;
- } else {
- b->current = NULL; // No tabs left
- }
- // Free the current tab
- freeTab(aux->data);
- }
- }
- void OPEN(struct browser *b, int id_specif, FILE *g) {
- struct List *aux = b->list.sant->next;
- // Traverse the list to find the tab with the specified ID
- do {
- if (aux->data->id == id_specif) {
- b->current = aux->data; // Set the current tab to the one with the specified ID
- return;
- }
- aux = aux->next;
- } while (aux != b->list.sant);
- // If the tab with the specified ID was not found
- }
- void PRINT(struct browser *b, FILE *g) {
- struct List *temp = b->list.sant->next;
- while (temp->data != b->current) {
- temp = temp->next;
- }
- // Print all tabs in the circular doubly linked list
- do {
- temp = temp->next;
- if(temp == b->list.sant)
- temp = temp->next;
- if (temp->data->id != b->current->id)
- } while (temp->data->id != b->current->id);
- // Print the current tab's page description if it exists
- if (b->current && b->current->currentPage) {
- }
- }
- int main() {
- struct page pages[51];
- char comands[501][51];
- struct browser b; // Declare the browser
- int i, n, m;
- // Initialize the browser directly in main
- b.list.sant->data = NULL;
- b.list.sant->next = b.list.sant;
- b.list.sant->prev = b.list.sant;
- b.current = NULL;
- createBrowser(&b);
- read(f, pages, comands, &n, &m);
- for (i = 0; i < m; i++) {
- NEW_TAB(&b);
- PRINT(&b, g);
- CLOSE(&b, g);
- int id_specif;
- OPEN(&b, id_specif, g);
- }
- }
- freePages(pages, n);
- freeBrowser(&b);
- return 0;
- }
Raw Paste