[WikiDyd] [TitleIndex] [WordIndex

Języki i metodyka programowania

Przykłady z wykładów w semestrze jesiennym 2011

Wykład VI, 22 listopada 2011

Tematem zajęć były listy liniowe.

   1 #include <stdio.h>
   2 #include <stdlib.h>
   3 
   4 typedef struct elem {
   5         int data;
   6         struct elem *next;
   7 } elem_t, *list_t;
   8 
   9 list_t add( list_t list, int data );
  10 list_t insert( list_t list, int data );
  11 list_t sort_insert( list_t list, int data );
  12 void fprint_list( list_t list, FILE *out );
  13 void fprint_list_iter( list_t list, FILE *out );
  14 
  15 int sumal( list_t list ) {
  16   int s =0;
  17   while( list != NULL ) {
  18     s += list->data;
  19     list = list->next;
  20   }
  21 
  22   return s;
  23 }
  24 
  25 int main ( int argc, char **argv ) 
  26 {
  27         list_t head= NULL;
  28         int i;
  29 
  30         for( i= 1; i < argc; i++ )
  31                 head = sort_insert( head, atoi( argv[i] ) );
  32 
  33         fprint_list( head, stdout );
  34         fprint_list_iter( head, stdout );
  35 
  36         printf( "Suma= %d\n", sumal( head ) );
  37 
  38         return 0;
  39 }
  40 
  41 list_t insert( list_t list, int data ) {
  42         list_t nowy = malloc( sizeof *nowy );
  43         nowy->data = data;
  44         nowy->next = list;
  45         return nowy;
  46 }
  47 
  48 list_t sort_insert( list_t list, int data ) {
  49         if( list == NULL || list->data >= data ) {
  50                 list_t nowy = malloc( sizeof *nowy );
  51                 nowy->data = data;
  52                 nowy->next = list;
  53                 return nowy;
  54         } else {
  55                 list->next = sort_insert( list->next, data );
  56                 return list;
  57         }
  58 }
  59                 
  60 list_t add( list_t list, int data ) {
  61 
  62         if( list == NULL ) {
  63                 list_t new= malloc( sizeof *new );
  64                 new->data = data;
  65                 new->next= NULL;
  66                 return new;
  67         } else {
  68                 list->next = add( list->next, data );
  69                 return list;
  70         }
  71 }
  72 
  73 void fprint_list( list_t list, FILE *out ) {
  74         if( list != NULL ) {
  75                 fprintf( out, "[%d]->", list->data );
  76                 fprint_list( list->next, out );
  77         } else {
  78                 fprintf( out, "NULL\n" );
  79         }
  80 }
  81 
  82 void fprint_list_iter( list_t list, FILE *out ) {
  83         list_t iterator= list;
  84         while( iterator != NULL ) {
  85                 fprintf( out, "[%d]->", iterator->data );
  86                 iterator= iterator->next;
  87         }
  88         fprintf( out, "NULL\n" );
  89 }

A tak się tym kodem bawiliśmy:

jstar-lap:~/jimp/w6> cc list.c 
jstar-lap:~/jimp/w6> ./a.out
NULL
NULL
Suma= 0
jstar-lap:~/jimp/w6> ./a.out 1 2 3 5
[1]->[2]->[3]->[5]->NULL
[1]->[2]->[3]->[5]->NULL
Suma= 11
jstar-lap:~/jimp/w6>

2015-09-23 06:44