C Language Learning (29)

Source: Internet
Author: User

1 //Study of linked list2#include <stdio.h>3#include <malloc.h>4 #defineLEN sizeof (struct student)5 structstudent{6     intnum;7     floatscore;8     structStudent *Next;9 };Ten intN//This is the number of linked list nodes One  A //Create a linked list - structStudent *Create () { -     structStudent *head,*p1,*P2; theP1=p2= (structStudent *)malloc(LEN); -printf"Please enter the student's numbers and score: \ n"); -scanf"%d%f",& (P1->num),& (p1->score)); -      while(p1->num!=0){//End Loop When the student's numbered 0 is entered +n++;//number of nodes plus 1 -         if(n==1){ +Head=P1; A}Else{ atp2->next=P1; -         } -P2=P1; -P1= (structStudent *)malloc(LEN); -printf"Please enter the student's numbers and score: \ n"); -scanf"%d%f",& (P1->num),& (p1->score)); in     } -p2->next=NULL; to      Free(p1); +     returnhead; - } the  * //Output Link List $ voidPrintstructStudent *head) {Panax Notoginseng     structStudent *p; -p=head; theprintf"\ n Here's the student's message: \ n \ t fraction \ n"); +      while(p!=NULL) { Aprintf"%d\t%f\n",p->num,p->score); theP=p->Next; +     } - } $  $ //Delete a linked list - structStudent *del (structStudent *head,intNUM) {//num is the numbered of the student to be deleted -     structStudent *p1,*P2; thep1=head; -      while(P1->num!=num && p1->next!=NULL) {WuyiP2=P1; theP1=p1->Next; -     } Wu     //If we find this, -     if(p1->num==num) { About         if(P1==head) {//If the node you want to delete is the first node $Head=head->Next; -}Else{ -P2->next=p1->Next; -         } A          Free(p1); +printf"the node with the numbered%d has been deleted \ n", num); then--;//number of nodes minus 1 -}Else{ $printf"This student does not exist. \ n"); the     } the     returnhead; the } the  -  in //inserting a linked list node the structStudent *insert (structStudent *head,structStudent *stu) {//The Second Stu parameter is the new linked list node to be inserted the     structStudent *p0,*p1,*P2; Aboutp1=head; thep0=Stu; the     if(Head==null) {//if the original linked list is empty, create a new linked list theHead=P0; +p0->next=NULL; -}Else{ the          while((P0->num>p1->num) && (p1->next!=NULL)) {BayiP2=P1; theP1=p1->Next; the         } -         if(P0->num <= p1->num) { -             if(HEAD==P1) {//P0 is the first node theHead=P0; the}Else{ thep2->next=p0;//P0 is the middle node the             } -p0->next=p1;//P0 refers to a node down the}Else{//P0 is the tail node thep1->next=P0; thep0->next=NULL;94         } the     } then--; the     returnhead;98 } About  - 101 intMain () {102     structStudent *head;103Head=Create ();104 print (head); theHead=del (Head,3);106 print (head);107     structStudent *Stu;108Stu= (structStudent *)malloc(LEN);109printf"Please enter the numbers and fractions of the newly added students: \ n"); thescanf"%d%f",&stu->num,&stu->score);111Head=Insert (head,stu); the print (head);113     return 0; the}

C Language Learning (29)

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.