C Language List Operation example sharing _c language

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.