lista.c

Vai alla documentazione di questo file.
00001 
00012 #include "Include/lista.h"
00013 
00014 void inizializza(struct lista *stack){
00015         stack->num_threads=0;   /* azzeramento del contatore */
00016         stack->top_elem=NULL;   /* azzeramento del puntatore */
00017 }
00018 
00019 int ins_new_elem(channel_t sk_cl, struct lista *s){
00020         struct thread *new = NULL;
00021 
00022         if (!(new = (struct thread *) malloc(sizeof(struct thread)))){
00023                 errore(__FILE__,__LINE__,
00024                 "Errore nel creare un nuovo elemento (STACK)",errno);
00025                 return errno;
00026         }
00027         new->sk_cl = sk_cl;
00028         new->next = s->top_elem;/* fa puntare il nuovo elemento al primo della lista */
00029         s->top_elem = new;              /* inserisce il nuovo elemento in testa alla lista */
00030         s->num_threads++;               /* incrementa il valore del contatore */
00031         return 0;
00032 }
00033 
00034 channel_t estrazione(struct lista *s){
00035         int sk_cl = -1;
00036         /* Puntatore temporaneo che puntera'
00037          * all'area di memoria da eliminare */
00038         struct thread *tmp;
00039         
00040         /* Se la lista non e' vuota... */
00041         if (s->top_elem){
00042                 sk_cl = s->top_elem->sk_cl;
00043                 /* Ora tmp punta all'area di memoria dell'elemento in cima alla lista */        
00044                 tmp = s->top_elem;
00045                 /* Il secondo elemento della lista diventa il primo,
00046                  * vengono scambiati cioe' gli indirizzi */
00047                 s->top_elem = s->top_elem->next;
00048                 s->num_threads--;
00049                 free(tmp);
00050         }
00051         return sk_cl;
00052 }
00053 
00067 int delete_elem(const channel_t sk_cl, struct thread **pun){
00068         struct thread* nodo = *pun;
00069         int ret_val = 0;
00070         
00071         if (!nodo) return -1;
00072         if (nodo->sk_cl == sk_cl){
00073                 *pun = nodo->next;
00074                 free(nodo);
00075                 return 0;
00076         }
00077         else
00078                 ret_val = delete_elem(sk_cl, &(nodo->next));
00079 
00080         return ret_val;
00081 }
00082 
00083 int rimuovi(channel_t sk_cl, struct lista *s){
00084         int ret_val=0;
00085         
00086         if (s->top_elem){
00087                 /* Cerchiamo e rimuoviamo... */
00088                 ret_val = delete_elem(sk_cl, &(s->top_elem));
00089                 if (!ret_val) s->num_threads--;
00090                 return ret_val;
00091         }
00092         /* Altrimenti errore lista vuota... */
00093         return -1;
00094 }
00095 
00096 boolean full(const struct lista *s){
00097         return ((boolean) (s->num_threads == FULL));
00098 }
00099 
00100 channel_t leggi_sk_cl(struct lista *s){
00101         return s->top_elem->sk_cl;
00102 }
Generato il Fri Jan 28 22:16:29 2011 per SFAT: Simplified File Allocation Table Project da  doxygen 1.6.3