IHYPRESS PROGRAMMING
Tutorials and C programs with code and output for beginners
c programming
HOME | ASP | C | CSS | GNUPLOT | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
CStructures & Linked Lists : Linked Lists Example
<10.03>
#include <stdio.h> #include <string.h> #define NL 20 typedef struct tree { char tree_name [NL]; struct tree* next; }tree; void print_trees(tree* head) { if (head == NULL) printf("\n"); //simple case: empty list else { printf("%s\n", head -> tree_name); //general case: first element print_trees(head -> next); //general case: rest of the list } } int main (void) { /* declaring variables to hold tree nodes */ tree tree1, tree2, tree3, tree4; /* declaring the starting and traveling pointers */ tree *p, *start; /* putting tree names into each node */ strcpy (tree1.tree_name, "Maple"); strcpy (tree2.tree_name, "Fir"); strcpy (tree3.tree_name, "Pine"); strcpy (tree4.tree_name, "Oak"); /* setting the start of the list at tree1 */ start = &tree1; /* linking the other trees together */ tree1.next = &tree2; tree2.next = &tree3; tree3.next = &tree4; /* sets tree4 as the last node of the list */ tree4.next = NULL; /* printing out the list by traveling though it with a loop */ p = start; while (p != NULL) { printf ("%s\n", p -> tree_name); p = p -> next; } printf ("\n"); /* printing out the list by using a recursive function */ print_trees (start); return (0); }
Hergestellt in Deutschland / Made in Germany
Maple Fir Pine Oak Maple Fir Pine Oak
COPYRIGHT © 2015-2024 IHY PRESS Frankfurt am Main 60329 Deutschland