Copy Code code as follows:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
/* The following is a structure defined to construct a linear list.
typedef struct chaink{
char c;
struct Chaink * NEXT;
}ck;
CK * Chain (CK *,int);
int print (CK *,int);
/* The following is the main function * *
int main (void) {
printf ("This is a linear list of test procedures." \ n ");
CK * HEAD=NULL;
int k;
K=sizeof (CK);
do{
Head=chain (HEAD,K);
printf ("Do you want to end the program?) If the end, please press y/y, press the other key to continue typing. \ n ");
if (getch () = = ' Y ' && getch () = = ' Y ') {
printf ("End of program.") \ n ");
Getch ();
Break
}
}while (1);
return 0;
}
/* Below is to demonstrate the linear list and try to write the program module * *
CK * CHAIN (CK * Head,int k) {
CK * next=null;/* Temporary local variables to be used when scanning the linked list * *
CK * temp=null;/* Temporary local variables to be used when inserting new nodes.
int i=0;/* A parameter of the print function dependent on the next door * *
if (head==null) {
Head= (ck*) malloc (k);/* Create an empty data node, the first Data node * *
if (head==null) {
printf ("Malloc memory Error!");
Getch ();
Exit (1);
}/* Verify that the data node has just been successfully created.
printf ("Header node was successfully created with the address%p.") \ n ", head);
head->next=null;
head->c= ' 0 '; /* Initialization head Node * *
}
do{/* receives input data in the form of inserting new data nodes from the head node.
printf ("Do you want to input new data?") If input, please press y/y, press the other key to end the entry. \ n ");
if (Getch ()!= ' y ' && getch ()!= ' Y ') {
printf ("entry ends.") \ n ");
Getch ();
Break
}
temp=head->next;/* Storage head node pointer field data * *
head->next= (ck*) malloc (k);/* Create a new node/*
if (head->next==null) {
printf ("Malloc memory Error!");
Getch ();
Exit (1);
}/* Verify that the data node has just been successfully created.
next=head->next;/* Scan to newly created node * *
next->next=temp;/* assign value to the new node's pointer field * *
printf ("Please input new data ... \ n");
Next->c=getch ()/* Assign value to the new node's data field * *
if (next->c==-1) {
printf ("System input end Error!") ");
Getch ();
Exit (1);
}
printf ("New data entry succeeded.") The new data entered is%c and its data node address is%p. \ n ", next->c,next);/* Feedback */
}while (1)/* To receive input data (end) in the form of inserting new data nodes from the head node.
if (head->next==null) {/* Data printing link * *
printf ("There is no data in the data link now.") \ n ");
Getch ();
}
else{
printf ("Do you want to display all the data in the list and its address?") To display, press y/y, and then press the other key to skip. \ n ");
if (Getch ()!= ' y ' && getch ()!= ' Y ') {
printf ("Skip.") \ n ");
Getch ();
return head;
}
printf ("Now output the contents of the list ... \ n serial Number data pointers");
for (Next=head->next;next!=null;next=next->next) {
I=print (next,i);//* Call function for Print List * *
}
}/* Data Printing link (end) * *
return head;
}
/* Below is the function of the print list.
int print (CK * Next,int i) {
printf ("%d%c%p\n", i,next->c,next);
i++;
return i;
}