- 31 mins
- 17
- secs ago
The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Cod - Guest
19th April 2025 02:22:26 PM
C
46 views
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct page {
- int id;
- char url[50];
- char *description;
- } page;
- typedef struct pagesList {
- struct page *data;
- struct pagesList *next;
- struct pagesList *prev;
- } pagesList;
- typedef struct stack {
- struct pagesList *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);
- }
- tab *createTab(struct tab *t, int id_tab, struct page pages[400]) {
- t->id = id_tab;
- // createPage(t->currentPage, pages[0].id, pages[0].url, pages[0].description);
- t->currentPage = &pages[0];
- t->backwardStack->top = NULL;
- t->forwardStack->top = NULL;
- return t;
- }
- void freeStack(struct stack *s) {
- while (s->top) {
- struct pagesList *temp = s->top;
- s->top = s->top->next;
- }
- }
- void freeTab(struct tab *t) {
- // if (t->currentPage) {
- // if (t->currentPage->description) {
- // free(t->currentPage->description);
- // t->currentPage->description = NULL;
- // }
- // free(t->currentPage);
- // t->currentPage = NULL;
- // }
- if (t->backwardStack) {
- freeStack(t->backwardStack);
- t->backwardStack = NULL;
- }
- if (t->forwardStack) {
- freeStack(t->forwardStack);
- t->forwardStack = NULL;
- }
- }
- void createBrowser(struct browser *b, struct page pages[400]) {
- // 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
- createPage(&pages[0], 0, "https://acs.pub.ro/", "Computer Science");
- createTab(new_tab->data, 0, pages);
- // Insert the new tab after the sentinel node
- new_tab->next = b->list.sant->next;
- new_tab->prev = b->list.sant;
- b->list.sant->next->prev = new_tab;
- b->list.sant->next = new_tab;
- // Set the current tab
- b->current = new_tab->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);
- aux = next;
- }
- b->list.sant = NULL;
- b->current = NULL;
- }
- void freePages(struct page pages[400], int n) {
- for (int i = 0; i <= n; i++) {
- if (pages[i].description) {
- // printf("eliberez: %d\n%s\n%s\n", pages[i].id,
- // pages[i].url, pages[i].description);
- pages[i].description = NULL;
- }
- }
- }
- void read(FILE *f, struct page pages[400], char comands[501][501], int *n,
- int *m) {
- int i;
- for (i = 1; i <= *n; i++) {
- pages[i].url[index] = '\0';
- char buffer[501];
- // Nu am lasat un '\n' la finalul descrierii
- buffer[ind] = '\0';
- pages[i].description = strdup(buffer);
- }
- for (i = 0; i < *m; i++) {
- }
- }
- void NEW_TAB(struct browser *b, int new_id, struct page pages[400]) {
- // Initialize the new tab
- new->data = createTab(new->data, new_id, pages);
- if (b->list.sant) {
- if (b->current) {
- // Insert the new tab after the last tab
- struct List *aux = b->list.sant->next;
- while (aux->next != b->list.sant) {
- 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 NEXT(struct browser *b, FILE *g) {
- struct List *aux = b->list.sant->next;
- while (aux->data != b->current) {
- aux = aux->next;
- }
- if (aux->next != b->list.sant) {
- aux = aux->next;
- b->current = aux->data;
- } else {
- aux = b->list.sant->next;
- b->current = aux->data;
- }
- }
- void PREV(struct browser *b, FILE *g) {
- struct List *aux = b->list.sant->next;
- while (aux->data != b->current) {
- aux = aux->next;
- }
- if (aux->prev != b->list.sant) {
- aux = aux->prev;
- b->current = aux->data;
- } else {
- aux = b->list.sant->prev;
- b->current = aux->data;
- }
- }
- void push(struct stack *s, struct page *p) {
- new->data = p;
- new->next = s->top;
- s->top = new;
- }
- struct page *pop(struct stack *s) {
- if (s->top == NULL)
- return NULL; // Return an empty page if the stack is empty
- struct pagesList *temp = s->top;
- struct page *p = temp->data;
- s->top = s->top->next;
- return p;
- }
- void PAGE(struct browser *b, FILE *g, int id_specif, struct page pages[400],
- int *n) {
- int i, ind = -1;
- for (i = 1; i <= *n; i++) {
- if (id_specif == pages[i].id)
- ind = i;
- }
- if (ind != -1) {
- push(b->current->backwardStack, b->current->currentPage);
- b->current->currentPage = &pages[ind];
- freeStack(b->current->forwardStack);
- } else {
- }
- }
- void BACKWARD(struct browser *b, FILE *g) {
- struct page *p = pop(b->current->backwardStack);
- if (p) {
- push(b->current->forwardStack, b->current->currentPage);
- b->current->currentPage = p;
- } else {
- }
- }
- void FORWARD(struct browser *b, FILE *g) {
- struct page *p = pop(b->current->forwardStack);
- if (p) {
- push(b->current->backwardStack, b->current->currentPage);
- b->current->currentPage = p;
- } else {
- }
- }
- 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 is_empty(struct stack *s) { return s->top == NULL; }
- void inv_stack(struct stack *s, struct stack *inv) {
- temp->top = NULL;
- while (!is_empty(s)) {
- push(inv, pop(s));
- push(temp, pop(s));
- }
- while (!is_empty(temp)) {
- push(s, pop(temp));
- }
- }
- void PRINT_HISTORY(struct browser *b, FILE *g, int id_specif) {
- struct List *temp = b->list.sant->next;
- while (temp->data->id != id_specif && temp->next != b->list.sant) {
- temp = temp->next;
- }
- if (temp->data->id == id_specif) {
- inv->top = NULL;
- inv_stack(temp->data->forwardStack, inv);
- while (!is_empty(inv))
- struct pagesList *aux = temp->data->backwardStack->top;
- while (aux != NULL) {
- aux = aux->next;
- }
- } else {
- }
- }
- int main() {
- struct page pages[400];
- char comands[501][501];
- struct browser b; // Declare the browser
- int i, n, m, id_specif, new_id = 0;
- // Initialize the browser directly in main
- createBrowser(&b, pages);
- read(f, pages, comands, &n, &m);
- for (i = 0; i < m; i++) {
- new_id++;
- NEW_TAB(&b, new_id, pages);
- PRINT(&b, g);
- CLOSE(&b, g);
- OPEN(&b, id_specif, g);
- NEXT(&b, g);
- PREV(&b, g);
- PAGE(&b, g, id_specif, pages, &n);
- BACKWARD(&b, g);
- FORWARD(&b, g);
- PRINT_HISTORY(&b, g, id_specif);
- }
- }
- freeBrowser(&b);
- freePages(pages, n);
- return 0;
- }
Raw Paste