# include & lt; Stdlib. H>
# include "linklist. H"
Linklist Create_List (int n) {//create n the length of the list
int i=0;
Linklist head, L, prep;
If (n<=0) {
Puts (" invalid element input!" );
The exit (0);
}
Head=prep=(linklist) malloc (sizeof (listnode));
If (head==NULL) {
Puts (" malloc failed!" );
Return the head;
}
for(i=1; I<=n; I++) {
If ((L=(linklist) malloc (sizeof (listnode)))==NULL) {
Puts (" failure to apply new space!" );
Return the head;
}
The else {
L-> Data=https://bbs.csdn.net/topics/i;
Prep - & gt; Next=L;
Prep=L;
}
}
Prep - & gt; Next=NULL;
Free (L);
Return the head;
}
Int Insert_List (linklist head, int pos) {//insert the first pos position of a linked list
Linklist p;
P=the head;
Linklist=L (linklist) malloc (sizeof (listnode));
L - & gt; Data=https://bbs.csdn.net/topics/99;
L-> Next=NULL;
int i=0;
If (head==NULL) {
Puts (" this list is null ");
return -1;
}
If (L==NULL) {
Puts (" new list is NULL ");
return -1;
}
If (pos<=0) {
Puts (" an error occurred in the location of the new list ");
return -1;
}
The else {
While (p - & gt; Next!=NULL) {
P=p - & gt; next;
i++;
If (I==pos) {
Printf (" yes \ n ");
L-> Next=p - & gt; next;
P - & gt; Next=L;
Free (L);
return 0;
}
}
Puts (" cross - border access ");
return -1;
}
}
Int Is_Empty_List (linklist head) {//is empty, I use this function
If (the head - & gt; Next==NULL) {
Puts (" this list is empty ");
return -1;
}
The else {
return 1;
}
}
Void Show_list (linklist head) {//print list
If (head==NULL) {
Puts (" the head is null!" );
return;
}
int i=0;
Linklist L;
L=the head;
While (L - & gt; Next!=NULL) {
i++;
Printf (" the number % d - & gt; The data is % d \ n ", I, L - & gt; The data);
L=L - & gt; next;
}
return;
}
CodePudding user response:
# include & lt; Stdio. H>
# include & lt; Stdlib. H>
/*
Typedef struct link {
The int data;
Struct the link * next;
} * linklist, listnode;
//*/# include "linklist. H"
Linklist Create_List (int n)
{//create n the length of the list
int i=0;
Linklist head, L, prep;
If (n<=0) {
Puts (" invalid element input!" );
The exit (0);
}
Head=prep=(linklist) malloc (sizeof (listnode));
If (head==NULL) {
Puts (" malloc failed!" );
Return the head;
}
for(i=1; I<=n; I++) {
If ((L=(linklist) malloc (sizeof (listnode)))==NULL) {
Puts (" failure to apply new space!" );
Return the head;
}
The else {
L-> Data=https://bbs.csdn.net/topics/i;
Prep - & gt; Next=L;
Prep=L;
}
}
Prep - & gt; Next=NULL;
//free (L);//why to release L, L have been added to the head the list
Return the head;
}
Int Insert_List (linklist head, int pos) {//insert the first pos position of a linked list
Linklist p;
P=the head;
Linklist=L (linklist) malloc (sizeof (listnode));
L - & gt; Data=https://bbs.csdn.net/topics/99;
L-> Next=NULL;
int i=0;
If (head==NULL) {
Puts (" this list is null ");
return -1;
}
If (L==NULL) {
Puts (" new list is NULL ");
return -1;
}
If (pos<=0) {
Puts (" an error occurred in the location of the new list ");
return -1;
}
//else {
While (p - & gt; Next!=NULL) {
P=p - & gt; next;
i++;
If (I==pos) {
Printf (" yes \ n ");
L-> Next=p - & gt; next;
P - & gt; Next=L;
//free (L);//why release?
return 0;
}
}
Free (L);//why not release?
Puts (" cross - border access ");
return -1;
//}
}
Int Is_Empty_List (linklist head) {//is empty, I use this function
If (the head - & gt; Next==NULL) {
Puts (" this list is empty ");
return -1;
}
The else {
return 1;
}
}
Void Show_list (linklist head) {//print list
If (head==NULL) {
Puts (" the head is null!" );
return;
}
int i=0;
Linklist L;
L=head - & gt; next;
//while (L - & gt; Next!=NULL) {
While (L!=NULL) {
i++;
Printf (" the number % d - & gt; The data is % d \ n ", I, L - & gt; The data);
L=L - & gt; next;
}
return;
}
/*
Int main (void)
{
Linklist phead;
Phead=Create_List (10);
Insert_List (phead, 5);
Show_list (phead);
} */
For your reference ~
Shouldn't the release of the release, the release of no release ~