-->
当前位置:首页 > 题库

PROGRAMMING:C language programming experiment 6-1 establishment of one way linked list

Luz5年前 (2021-05-10)题库456
This topic requires the realization of the following three functions to achieve the following functions:
Enter a number of positive integers, end with '- 1', and create a one-way linked list by adding nodes to the linked list.
Traverse and output this one-way list.
###Add a node function to the end of the linked list:
```
struct link *AppendNode(struct link *head,int data);
```
-'head': pointer to the chain header node. If 'head' is null, a new head node will be created
-'data ': the data value of the node to be added
-Function returns: the pointer of the chain header node after the node has been added
###Traversal display linked list function:
```
void DisplyNode(struct link *head);
```
-'head': pointer of chain header node
###Delete linked list memory function:
```
void DeleteMemory(struct link *head);
```
-'head': pointer of chain header node
###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);
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*/
DeleteMemory(head); /* Free all dynamically allocated memory*/
return 0;
}
/*Here, we implement struct link * appendnode (struct link * head, int data)*/
/*Here, void displynode (struct link * head) is implemented*/
/*In this way, void deletememory (struct link * head) is implemented*/
```
###Input format:
A series of positive integers ending with '- 1'
###Output format:
Output each item in the one-way linked list, and connect each number with '- >'
###Input example:
Here is a set of inputs. For example:
```in
1 3 5 7 9 -1
```
###Output example:
The corresponding output is given here. For example:
```out
1->3->5->7->9
```







answer:If there is no answer, please comment