Write a program that defines a list of integers, the prints its elements.
Difficulty level
This exercise is mostly suitable for students
#include <stdlib.h>
#include <stdio.h>
struct cell {
int elem;
struct cell *next;
};
typedef struct cell* list;
void display_list(list l)
{
struct cell *cell;
cell = l;
while ( cell != NULL ) {
printf("%d\n", cell->elem);
cell = cell->next;
}
}
void main() {
struct cell m1 = { 5, NULL };
struct cell m2 = { 4, &m1 };
struct cell m3 = { 3, &m2 };
list l = &m3;
display_list(l);
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Integer division