How do I create a list of arrangements to save a struct?

Asked

Viewed 349 times

1

Someone would help me create an array-like structure so I can implement a struct containing an ID, a priority (medium, low, high, urgent) and a phone?

basically:

typedef struct Atividades{
      int id;
      int telefone
      char prioridade[20];
}

I would have to record this in a list of arrangements, list of arrangements only learned using vector, someone would know how to implement using struct?

1 answer

1

That code should help you.

#include <stdio.h>
#include <malloc.h>

struct activity{
    int id;
    int phone;
    char prio[20];
};

typedef struct _list{
    // Armazena data
    struct activity *data;
    struct _list *next; //proximo item da list
}list;

// Adiciona na lista
void append(list **l, struct activity *ac){
    if(!*l){// insere o primeiro valor
        (*l) = (list *) malloc(sizeof(list));
        (*l)->data = (struct activity*) malloc(sizeof(struct activity));
        (*l)->next = NULL;
    }

    //Percorre até o final da lista
    list *swap = (*l);
    while((*swap)->next)
        swap = swap->next;

    //adiciona um novo item no final da lista
    swap->next = (list *) malloc(sizeof(list));
    swap->data = (struct activity*) malloc(sizeof(struct activity));
    swap->next->next = NULL;
}

// Remove um item do inicio da lista
void remove(list **l){
    if(*l){
        list *del = (*l);
        (*l) = (*l)->next;
        free(del);
    }
}
  • helped a lot! thanks!

  • @Viníciusbarbosa helped herself "a lot", so mark the answer as accepted

Browser other questions tagged

You are not signed in. Login or sign up in order to post.