Объясните код списка - C (СИ)
Формулировка задачи:
#include <stdlib.h> #include <stdio.h> #define SIZE 11 typedef int TE; struct list { TE data; struct list *next; }; struct list* add ( struct list *list0, TE n ) { struct list *pointer = malloc( sizeof( struct list ) ); pointer -> data = n; pointer -> next = list0; list0 = pointer; return list0; } void print ( struct list *list0 ) { struct list *pointer = list0; while ( pointer != 0 ) { printf( "%4d", pointer -> data ); pointer = pointer -> next; } printf( "\n" ); } void printToFile( FILE *f, struct list *list0 ) { struct list *pointer = list0; while ( pointer != 0 ) { fprintf( f, "%4d", pointer -> data ); pointer = pointer -> next; } fprintf( f, "\n" ); } unsigned int is_empty( struct list * list0 ) { return list0 == NULL; } struct list* insert( struct list *list0, TE n ) { struct list *p = malloc( sizeof( struct list ) ), *cur; p -> data = n; p -> next = 0; if ( !list0 ) list0 = p; else { cur = list0; while ( cur -> next != 0 ) cur = cur -> next; p -> next = cur -> next; cur -> next = p; } return list0; } float average( struct list *list0 ) { struct list *pointer = list0; float sum = 0.0; unsigned int number = 0; while ( pointer ) { number++; sum += pointer -> data; pointer = pointer -> next; } return sum * 1.0 / number; } struct list* swapFrontBack( struct list *list0 ) { struct list *pointer = list0; while ( pointer -> next ) pointer = pointer -> next; int temp = list0 -> data; list0 -> data = pointer -> data; pointer -> data = temp; return list0; } struct list* replace( struct list *list0, TE e1, TE e2 ) { struct list *pointer = list0; while ( pointer ) { if ( pointer -> data == e1 ) pointer -> data = e2; pointer = pointer -> next; } return list0; } int main() { struct list *head = NULL; int index; for( index = 0; index <= SIZE; ++index ) { head = insert( head, index ); } head = swapFrontBack( head ); replace( head, 4, 111 ); print( head ); printf( "Average = %7.4f\n", average( head ) ); FILE *file = fopen( "input1.txt", "w" ); printToFile( file, head ); fclose( file ); return 0; }
Решение задачи: «Объясните код списка»
textual
Листинг программы
struct list* replace( struct list *list0, TE e1, TE e2 )
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д