4

Chaining data

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <malloc.h>
  4.  
  5. struct node {
  6.     struct node *next;
  7.     int data;
  8. };
  9.  
  10. int main() {
  11.     int i, n;
  12.     struct node *p = 0, *q = 0;
  13.     char buf[ 128 ];
  14.  
  15.     printf( "Enter a series of integers and a ^D|^Z:\n" );
  16.  
  17.     for ( ;; ) {
  18.         n = scanf( "%d", &i );
  19.         if ( n == EOF )
  20.             break;
  21.         if ( n == 1 ) {
  22.             p = (struct node *)malloc( sizeof( struct node ) );
  23.             p->data = i;
  24.             p->next = q;
  25.             q = p;
  26.         }
  27.     }
  28.  
  29.     for ( ; p; p = p->next )
  30.         printf( "%d -> ", p->data );
  31.     printf( "*\n" );
  32.  
  33.     exit( 0 );
  34. }
$ gcc -o cnode cnode.c
$ ./cnode
Enter a series of integers and a ^D|^Z:
-3 0 1 3 5 11
^D
11 -> 5 -> 3 -> 1 -> 0 -> -3 -> *

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].