PROGRAMMING:C language programming experiment 6-3 insertion of one way linked list
Based on the one-way linked list code in experiment 6-2, the function of inserting nodes is added to realize that the specified value data is inserted into the corresponding nodes * * in ascending order * * in the linked list. That is, insert data before the first node larger than data.
###Insert node function interface:
```
struct link *InsertNode(struct link *head, int data)
```
-'head' is the pointer of the chain header node
-'data 'is the value of the node to be inserted.
-Function returns the pointer to the header node after the corresponding node is inserted.
###Main function example:
```
#include
#include
struct link
{
int data;
struct link *next;
};
struct link *AppendNode(struct link *head,int data);
void DisplyNode(struct link *head);
void DeleteMemory(struct link *head);
struct link * DeleteNode(struct link *head,int data);
struct link *InsertNode(struct link *head, int nodeData);
int main()
{
char c;
int data = 0;
struct link *head = NULL; /* Chain header pointer*/
while (1)
{
scanf("%d",&data);
if (data==-1)
break;
head = AppendNode(head,data);/* Add a node to the end of the linked list with head as the head pointer*/
}
DisplyNode(head); /* Display the information of each node in the current linked list*/
scanf("%d",&data);
head = InsertNode(head,data);
DisplyNode(head); /* Display the information of each node in the current linked list*/
DeleteMemory(head); /* Free all dynamically allocated memory*/
return 0;
}
```
###Input format:
First line:
A series of values ending with '- 1' to construct a one-way linked list.
Second line:
Inserts the value before the first node in the list that is greater than the specified value.
###Output format:
After the one-way linked list is constructed, the whole linked list is displayed.
After the specified value is inserted, the whole linked list is displayed again.
###Input example:
Here is a set of inputs. For example:
```in
1 3 5 7 9 -1
six
```
###Output example:
The corresponding output is given here. For example:
```out
1->3->5->7->9
1->3->5->6->7->9
```
###Input example:
Here is a set of inputs. For example:
```in
1 3 5 3 1 -1
four
```
###Output example:
The corresponding output is given here. For example:
```out
1->3->5->3->1
1->3->4->5->3->1
```
answer:If there is no answer, please comment
###Insert node function interface:
```
struct link *InsertNode(struct link *head, int data)
```
-'head' is the pointer of the chain header node
-'data 'is the value of the node to be inserted.
-Function returns the pointer to the header node after the corresponding node is inserted.
###Main function example:
```
#include
#include
struct link
{
int data;
struct link *next;
};
struct link *AppendNode(struct link *head,int data);
void DisplyNode(struct link *head);
void DeleteMemory(struct link *head);
struct link * DeleteNode(struct link *head,int data);
struct link *InsertNode(struct link *head, int nodeData);
int main()
{
char c;
int data = 0;
struct link *head = NULL; /* Chain header pointer*/
while (1)
{
scanf("%d",&data);
if (data==-1)
break;
head = AppendNode(head,data);/* Add a node to the end of the linked list with head as the head pointer*/
}
DisplyNode(head); /* Display the information of each node in the current linked list*/
scanf("%d",&data);
head = InsertNode(head,data);
DisplyNode(head); /* Display the information of each node in the current linked list*/
DeleteMemory(head); /* Free all dynamically allocated memory*/
return 0;
}
```
###Input format:
First line:
A series of values ending with '- 1' to construct a one-way linked list.
Second line:
Inserts the value before the first node in the list that is greater than the specified value.
###Output format:
After the one-way linked list is constructed, the whole linked list is displayed.
After the specified value is inserted, the whole linked list is displayed again.
###Input example:
Here is a set of inputs. For example:
```in
1 3 5 7 9 -1
six
```
###Output example:
The corresponding output is given here. For example:
```out
1->3->5->7->9
1->3->5->6->7->9
```
###Input example:
Here is a set of inputs. For example:
```in
1 3 5 3 1 -1
four
```
###Output example:
The corresponding output is given here. For example:
```out
1->3->5->3->1
1->3->4->5->3->1
```
answer:If there is no answer, please comment