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