/* = ========================= */
/* Program Name: createlist. C */
/* Program Objective: To design a program that creates input data into a linked list */
/* Written by Kuo-Yu Huang. (want studio .)*/
/* Input: # go to dinner tomorrow */
/* = ========================= */
# Include <stdio. h>
# Include <malloc. h>
# Define max 10
Struct list/* node Structure Declaration */
{
Int number;
Char name [Max];
Struct list * next;
};
Typedef struct list node;
Typedef node * link;
/* Release the linked list */
Void free_list (link head)
{
Link pointer;/* node Declaration */
While (Head! = NULL)/* When the node is null, end the loop */
{
Pointer = head;
Head = head-> next;/* point to the next node */
Free (pointer );
}
}
/* Output linked list */
Void print_list (link head)
{
Link pointer;/* node Declaration */
Pointer = head;/* set the pointer to the first node */
While (pointer! = NULL)/* When the node is null, the end loop */
{
Printf ("# input data ##/ N ");
Printf ("Data number: % d/N", pointer-> number );
Printf ("Data name: % s/n", pointer-> name );
Pointer = pointer-> next;/* point to the next node */
}
}
/* Create a linked list */
link create_list (link head)
{< br> int datanum; /* Data ID */
char dataname [Max];/* Data name */
link new;/* node Declaration */
link pointer; /* node Declaration */
int I;
head = (Link) malloc (sizeof (node )); /* allocate memory */
If (Head = NULL)
printf ("memory allocate failure! /N ");/* Memory Allocation failure */
else
{
datanum = 1; /* Initial Data number */
printf ("Please input the data name:");
scanf ("% s", dataname );
head-> Number = datanum;/* define the first node data number */
for (I = 0; I <= max; I ++)
head-> name [I] = dataname [I];/* assign the value of dataname to name */
head-> next = NULL;
pointer = head;/* set the pointer to the first node */
while (1)
{< br> datanum ++;/* increasing data numbers */
New = (Link) malloc (sizeof (node);/* allocate memory */
printf ("Please input the data name:");
scanf ("% s ", dataname);
If (dataname [0] = '0')/* enter 0 to end */
break;
New-> Number = datanum;
for (I = 0; I {< br> New-> name [I] = dataname [I];
}< br> New-> next = NULL;
pointer-> next = new;/* connect the new node to the end of the original list */
pointer = new; /* The End Node of the list is a new node */
}< BR >}< br> return head;
}
/* Main Program */
void main ()
{
link head = NULL; /* node Declaration */
head = create_list (head);/* call the create linked list function */
If (Head! = NULL)
{< br> print_list (head);/* call the output linked list data function */
free_list (head ); /* call the release linked list function */
}< BR >}