home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / CataniaB / teach-act / esempi / Comp-Sep / list.c < prev    next >
C/C++ Source or Header  |  1997-04-19  |  1KB  |  75 lines

  1. /* List.c: implementazione :  */
  2.  
  3. #include<stdio.h>
  4. #include "list.h"
  5.  
  6. bool is_empty(dlist *p) { return(p->head->next == p->tail); }
  7.  
  8. list makecell(void) { return (list) malloc (sizeof (struct cell));}
  9.  
  10. void init(dlist *p)
  11. {
  12.     p->head = makecell();
  13.     p->tail = makecell();
  14.     p->head->next=p->tail;
  15.  
  16. void first(element *x,element *y,dlist *p)
  17. {
  18.     if (!is_empty(p)) 
  19.      {
  20.       *x=p->head->next->x;
  21.       *y=p->head->next->y;
  22.      }
  23.     else printf("\n Errore lista vuota"); 
  24.  
  25. void insert_head(element x,element y,dlist *p)
  26.   list q;
  27.   
  28.   p->head->x=x;
  29.   p->head->y=y;
  30.   q = makecell();  
  31.   q->next=p->head;
  32.   p->head=q;
  33. }
  34.  
  35.  
  36. void insert_tail(element x,element y,dlist *p)
  37. {  
  38.   list q;
  39.  
  40.   p->tail->x=x;
  41.   p->tail->y=y;
  42.   q = makecell();
  43.   p->tail->next=q;
  44.   p->tail=q;
  45.    
  46. void delete_head(dlist *p)
  47. {  
  48.   list q;
  49.   if (!is_empty(p))
  50.     {
  51.       q=p->head->next;
  52.       p->head->next=q->next;
  53.       free(q);
  54.      }
  55.   else printf("\n Errore stack vuoto"); 
  56.    
  57.  
  58. void print(dlist *q)
  59. {
  60.   list p;
  61.   p=q->head; 
  62.   printf("\n-");
  63.   while (p->next!=q->tail) 
  64.    { printf(" (%d,%d)",p->next->x,p->next->y);
  65.      p=p->next;
  66.    } 
  67.   printf(" -\n");
  68. }
  69.  
  70.