How do I create a list by arrangement to save a struct?

1

How can I implement a struct containing an ID, a priority (medium, low, high, urgent) and a telephone?

basically:

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

I would have to write this in a list by arrangement, list by arrangement I just learned using vector, would anyone know how to implement using struct?

    
asked by anonymous 07.03.2016 / 21:14

1 answer

1

This 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);
    }
}
    
07.03.2016 / 21:57