C. Create, output, delete, and insert a Dynamic Linked List

Source: Internet
Author: User
Tags define null

Dynamic Linked List creation, output, deletion, and insertion


# Include <stdio. h>

# Include <malloc. h>

# Include <stdlib. h>

# Define null 0

# Define Len sizeof (struct student)

Struct student

{

Long num;

Float score;

Struct student * next;

};

Int N;/* n is the global variable */

 

Struct student * creat ()

{

Struct student * P1, * P2, * head;

N = 0;

P1 = P2 = (structstudent *) malloc (LEN );

Scanf ("% lD, % F", & P1-> num, & P1-> score );

Head = NULL;

While (P1-> num! = 0)

{

N = n + 1;

If (n = 1)

Head = p1;

Elsep2-> next = p1;

P2 = p1;

P1 = (structstudent *) malloc (LEN );

Scanf ("% lD, % F", & P1-> num, & P1-> score );

}

P2-> next = NULL;

Return (head );

}

 

 

Void print (struct student * head)

{

Struct student * P;

Printf ("\ nthese records are: \ n ");

P = head;

If (Head! = NULL)

Do

{

Printf ("% lD, % F \ n", p-> num, p-> score );

P = p-> next;

} While (P! = NULL );

}

 

 

Struct student * del (struct student * head, long num)

{

Struct student * P1, * P2;

 

If (Head = NULL)

{Printf ("\ NLIST null! \ N "); goto end ;}

 

P1 = head;

While (num! = P1-> num & P1-> next! = NULL)

{

P2 = p1; P1 = p1-> next;

}

If (num = p1-> num)

{

If (p1 = head) Head = p1-> next;

Else P2-> next = p1-> next;

Printf ("delete: % LD \ n", num );

N = n-1;

}

Else printf ("% d not beenfound! \ N ", num );

End :;

Return (head );

}

 

Struct student * insert (struct student * head, struct student * stud)

{

Struct student * P0, * P1, * P2;

P1 = head;

P0 = stud;

If (Head = NULL)

{

Head = P0; P0-> next = NULL;

}

Else

{

While (P0-> num> P1-> num) & (P1-> next! = NULL ))

{

P2 = p1;

P1 = p1-> next;

}

 

If (P0-> num <= p1-> num) // conflicts with the preceding if, incorrect

{

If (Head = p1) Head = P0;

Else P2-> next = P0;

P0-> next = p1;

}

Else

{P1-> next = P0; P0-> next = NULL ;}

}

N = n + 1;

Return (head );

}

 

Void main ()

{

Struct student * head, Stu;

Long del_num;

Printf ("inputrecords: \ n ");

Head = creat ();

Print (head );

 

Printf ("\ ninputthe deleted number :");

Scanf ("% lD", & del_num );

Head = del (Head, del_num );

Print (head );

 

Printf ("\ ninputthe inserted record :");

Scanf ("% d, % F", & Stu. Num, & Stu. Score );

Head = insert (Head, & Stu );

Print (head );

}


Related Article

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.