Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include <string.h>
- typedef struct node {
- size_t len;
- void *value;
- struct node *next;
- } Node ;
- Node* init_node(void* value, size_t len) {
- Node* node = (Node*) calloc(1, sizeof(Node));
- node->len = len, node->value = value, node->next = NULL;
- return node;
- }
- void enqueue(Node *node, void *value) {
- if(value == NULL) return;
- printf("--\n");
- node->len++;
- if(node->next == NULL)
- node->next = init_node(value, 1), printf("aaa\n");
- else
- enqueue(node->next, value);
- }
- void* dequeue(Node **head, size_t n) {
- void *aux = calloc(1, n);
- memcpy(aux, (*head)->value, n);
- if((*head)->next == NULL)
- free(head);
- else {
- Node* node_aux = (*head);
- *head = node_aux->next;
- free(node_aux);
- }
- return aux;
- }
- void show_queue(Node* head, void (*show_val)(void*)) {
- if(head->next == NULL) {
- show_val(head->value);
- return;
- } else {
- show_val(head->value);
- show_queue(head->next, show_val);
- }
- }
- size_t queue_len(Node* head) { return head->len ;}
- void show_int(void *n) {
- if(n == NULL)
- printf("Null Pointer\n");
- else {
- int *k = (int*) n;
- printf("%d->", *k);
- }
- }
- int main(void) {
- int n = 5;
- int *arr = (int*) calloc(n, sizeof(int));
- for(int i = 0; i < n; i++)
- *(arr + i) = i + 1, printf("%d ", *(arr + i));
- printf("\n");
- Node* head = init_node(&arr[0], 1);
- for(int i = 1; i < n; i++)
- enqueue(head, &arr[i]);
- show_queue(head, &show_int);
- int *p = (int*) dequeue(&head, sizeof(int));
- printf("\ndequeue: %d\n", *p);
- show_queue(head, &show_int);
- p = (int*) dequeue(&head, sizeof(int));
- printf("\ndequeue: %d\n", *p);
- show_queue(head, &show_int);
- p = (int*) dequeue(&head, sizeof(int));
- printf("\ndequeue: %d\n", *p);
- show_queue(head, &show_int);
- printf("\nTamanho da fila: %zu", queue_len(head));
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment