# include
# include
# define the ERROR 0
# define OK 1
//the definition of a singly linked list
Typedef int elemtype;
Typedef struct Node
{
Elemtype data;
Struct Node * next;
} Node, * linklist;
//singly linked lists of initialization
Void init_linklist (linklist * l)
{
* l=(linklist) malloc (sizeof (Node));
(* l) - & gt; Next=NULL;
}
//head interpolation based singly linked list
Void createfromhead linklist (l)
{
Node * s;
char c;
Int flag=1;
While (flag)
{
c=getchar();
fflush(stdin);
If (c!='0')
{
S=(*) malloc (sizeof (Node));
S - & gt; data=https://bbs.csdn.net/topics/c;
S - & gt; Next=l - & gt; Next;
L - & gt; Next=s;
}
The else
Flag=0;
}
}
//singly linked lists of insert
Int inslist (int I linklist l, elemtype e)
//in the lead the singly linked list of nodes l the position I insert the value of e node
{
Pre Node * and * s;
int k;
If (i<=0)
Return the ERROR;
The pre=l; k=0;//from the beginning to find, find the first I - 1 node
While (pre!=NULL& & k{
The pre=pre - & gt; Next;
K=k + 1;
}//find the first I - 1 node
If (pre==NULL) pre//if the current location to find an empty table said, into account but also to the ith, insert position unreasonable
{
Printf (" insert position is not reasonable ");
Return the ERROR;
}
S=(*) malloc (sizeof (Node));//apply for a new node
S - & gt; data=https://bbs.csdn.net/topics/e;//value data is put in the s and e
S - & gt; Next=pre - & gt; Next;//modify a pointer, the complete insert
The pre - & gt; Next=s;
Return OK;
}
Void main ()
{
Linklist l;
Node * p;
Int I, r, j.
//establish a singly linked list
Init_linklist (& amp; L);
Printf (" head method to establish a singly linked list, please enter the list data \ n ");
Createfromhead (l);
P=l - & gt; Next;
while(p!=NULL)
{
Printf (" % c \ n ", * p).
P=p - & gt; Next;
}
//singly linked lists of insert
Printf (" please enter your insert location: \ n ");
The scanf (" % d ", & amp; I);
Printf (" please input the number which you want to insert: \ n ");
The scanf (" % d ", & amp; R);
Inslist (l, I, r);
Printf (" results for: \ n ");
P=l - & gt; Next;
while(p!=NULL)
{
Printf (" % c \ n ", * p).
P=p - & gt; Next;
}
}
Wrong, the result of that results in no insert that number
CodePudding user response:
# include
# include
# include
# define the ERROR 0
# define OK 1
//the definition of a singly linked list
Typedef int elemtype;
Typedef struct Node
{
Elemtype data;
Struct Node * next;
} Node, * linklist;
//singly linked lists of initialization
Void init_linklist (linklist * l)
{
* l=(linklist) malloc (sizeof (Node));
(* l) - & gt; Next=NULL;
}
//head interpolation based singly linked list
Void createfromhead linklist (l)
{
Node * s;
char c;
Int flag=1;
While (flag)
{
C=getchar ();
//fflush (stdin);
getchar();
If (c!='0')
{
S=(*) malloc (sizeof (Node));
S - & gt; data=https://bbs.csdn.net/topics/c;
S - & gt; Next=l - & gt; Next;
L - & gt; Next=s;
}
The else
Flag=0;
}
}
//singly linked lists of insert
Int inslist (int I linklist l, elemtype e)
//in the lead the singly linked list of nodes l the position I insert the value of e node
{
Pre Node * and * s;
int k;
If (i<=0)
Return the ERROR;
The pre=l; k=0;//from the beginning to find, find the first I - 1 node
While (pre!=NULL& & k{
The pre=pre - & gt; Next;
K=k + 1;
}//find the first I - 1 node
If (pre==NULL) pre//if the current location to find an empty table said, into account but also to the ith, insert position unreasonable
{
Printf (" insert position is not reasonable ");
Return the ERROR;
}
S=(*) malloc (sizeof (Node));//apply for a new node
S - & gt; data=https://bbs.csdn.net/topics/e;//value data is put in the s and e
S - & gt; Next=pre - & gt; Next;//modify a pointer, the complete insert
The pre - & gt; Next=s;
Return OK;
}
//void main ()
Int main ()
{
Linklist l;
Node * p;
Int I, r, j.
//establish a singly linked list
Init_linklist (& amp; L);
Printf (" head method to establish a singly linked list, please enter the list data \ n ");
Createfromhead (l);
P=l - & gt; Next;
while(p!=NULL)
{
//printf (" % c \ n ", * p).
Printf (" % c \ n ", p - & gt; The data);
P=p - & gt; Next;
}
//singly linked lists of insert
Printf (" please enter your insert location: \ n ");
The scanf (" % d ", & amp; I);
getchar();
Printf (" please input the number which you want to insert: \ n ");
//the scanf (" % d ", & amp; R);
The scanf (" % c ", & amp; R);
If (inslist (l, I, r)==ERROR) {
Printf (" Insert error! \n");
return -1;
}
Printf (" results for: \ n ");
P=l - & gt; Next;
while(p!=NULL)
{
//printf (" % c \ n ", * p).
Printf (" % c \ n ", p - & gt; The data);
P=p - & gt; Next;
}
}
For your reference ~
Insert algorithm will be a problem, the problem is that the main function of r is % d format, pay attention to the '1' and 1 are not equal,
If according to the writing of the building Lord, 51, can consider to enter this insert is' 3 ',