summaryrefslogtreecommitdiff
path: root/reference/C/EXAMPLES/linklst2.c
blob: d019454cf77ea857f092b205e1d3ac26e54661d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

/************************************************************************ 
 *
 * Purpose: To demonstrate 'linked lists' This program will build a
 *	    linked list and place data into it. When the data is exausted
 *	    the contents of the list are O/P.
 *
 *          This is a "First in First out" (FIFO) list.
 *
 * Author:  M. J. Leslie
 *
 * Date:    11-May-95
 *
 ************************************************************************/

#include <stdlib.h>		/* malloc				*/

/************************************************************************/

struct x {			/* Declare a structure			*/
  char name[20];
  int age;
  struct x *next_rec;
};

/************************************************************************/

main()
{
  struct x *start_pointer;	/* Define pointers to the structure	*/
  struct x *next_pointer;

				/* Create some data to be placed in the
				 * Linked list.				*/
  char *names[]=
  {
    "Martin",
    "John  ",
    "Alex  ",
    ""
  };

  int ages[]={32, 43, 29, 0};

  int count=0;			/* General purpose counter.		*/

   /*===================================================================*
    =									=
    = 	Build a LINKED LIST and place data into it.			=
    =									=
    *===================================================================*/
       
				/* Initalise 'start_pointer' by reserving 
				 * memory and pointing to it		*/

  start_pointer=(struct x *) malloc (sizeof (struct x));

				/* Initalise 'next_pointer' to point
				 * to the same location.		*/
  next_pointer=start_pointer;

				/* Put some data into the reserved 
				 * memory.				*/

  strcpy(next_pointer->name, names[count]);
  next_pointer->age = ages[count];


				/* Loop until all data has been read	*/

  while ( ages[++count] != 0 )
  {
				/* Reserve more memory and point to it	*/

    next_pointer->next_rec=(struct x *) malloc (sizeof (struct x));

    next_pointer=next_pointer->next_rec;


    strcpy(next_pointer->name, names[count]);
    next_pointer->age = ages[count];
  }

  next_pointer->next_rec=NULL;

   /*===================================================================*
    =									=
    = 	Traverse the linked list and O/P all the data within it.	=
    =									=
    *===================================================================*/


  next_pointer=start_pointer;

  while (next_pointer != NULL)
  {
    printf("%s   ", next_pointer->name);
    printf("%d \n", next_pointer->age);
    next_pointer=next_pointer->next_rec;
  }
}

/************************************************************************ 
 *
 *	Program results.
 *
 * Martin   32 
 * John     43 
 * Alex     29 
 *
 ************************************************************************/