4
Chaining data
- #include <stdlib.h>
- #include <stdio.h>
- #include <malloc.h>
- struct node {
- struct node *next;
- int data;
- };
- int main() {
- int i, n;
- struct node *p = 0, *q = 0;
- char buf[ 128 ];
- printf( "Enter a series of integers and a ^D|^Z:\n" );
- for ( ;; ) {
- n = scanf( "%d", &i );
- if ( n == EOF )
- break;
- if ( n == 1 ) {
- p = (struct node *)malloc( sizeof( struct node ) );
- p->data = i;
- p->next = q;
- q = p;
- }
- }
- for ( ; p; p = p->next )
- printf( "%d -> ", p->data );
- printf( "*\n" );
- exit( 0 );
- }
$ 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