Task description
Write a simple one-way linked list to manage a group of ordered integers with variable length (query, insert, modify and delete).
Programming requirements
(1) Create a one-way linked list, whose node contains two integer data fields: number and xuhao (the serial number increases from 1, and the serial numbers of two adjacent nodes differ by 1, the same below), and output the linked list (the values of nodes are output from the head in order, and the values of adjacent nodes are separated by a space, and the value of the last node does not contain a space, the same below);
(2) Input the integer n and output the value of the node with sequence number n;
(3) Enter the integer x, find the node with the first value of X, and output its serial number and value;
(4) Insert node: enter integer n and integer x, insert a new node with value x after the node with serial number N in the original linked list, and then output the linked list;
(5) Delete node: enter the integer n, delete the node with sequence number n, and output the linked list.
(6) Any modification of the linked list must ensure that after the operation, the serial number in the linked list starts from the first node, the serial number always increases from 1, and the serial number values of adjacent nodes differ by 1
(7) Ensure that the memory is released in time (for example, when deleting a node, the memory of the deleted node shall be released in time) to avoid memory leakage.
(8) Make full use of the function to reasonably divide and design the program function, and improve the maintainability of the program.
Test description
Test input
21 3 15 27 11 18 0 3 15 3 33 4
Expected output:
21 3 15 27 11 18 15 3 15 21 3 15 33 27 11 18 21 3 15 27 11 18
The above explanation of input and output is as follows:
Enter the first line: enter a string of data separated by a space. When the input is 0, the input ends.
Output the first row: output each element in the linked list.
Enter the second line: enter the sequence number 3 to find
Output the second line: Data 15 corresponding to No. 3
Enter the third line: the element to find 15
Output the third line: position 3 corresponding to element 15, and the element 15
Enter the fourth line: the first element is the position to be inserted, the previous element is position 3, and the second element is the data to be inserted 33
Enter the fifth line: position 4 of the element to be deleted
Output line 5: the linked list elements after deleting the elements are output again.
code implementation
// Programming completes the following tasks //(1) Establish a single linked list 21 3 15 27 11 18 and output the linked list; //(2) Enter the sequence number n, find the node with sequence number n, and output it; //(3) Input the value x, find the node with the value x, and output it; //(4) Insert node: enter the sequence number n and the value x. insert x after the node with sequence number n and output the linked list; //(5) Delete node: enter the sequence number n, delete the node with sequence number n, and output the linked list. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #define LEN sizeof(struct Nmb) //Create node typedef struct Nmb { int number; int xuhao; struct Nmb* next; }Link; //Initialize linked list (input data) Link* Creat(const int n,const int *arr) { Link* head = NULL, * node = NULL, * end = NULL; head = (Link*)malloc(LEN); if (head == NULL) { printf("%s", strerror(errno)); return 0; } end = head; for (int i = 1; i <= n; i++) { node = (Link*)malloc(LEN); if (node == NULL) { printf("%s", strerror(errno)); return 0; } node->number = arr[i - 1]; node->xuhao = i; end->next = node; end = node; } end->next = NULL; return head; } //Output linked list void Output(const Link* head) { int i=0; while (head->next != NULL) { head = head->next; printf("%d", head->number); if(head->next != NULL) printf(" "); } printf("\n"); } //Find sequence number int SreachS(const Link* head, const int n) { int det = 0; while (head->next != NULL) { if (head->xuhao == n) { det = head->number; break; } head = head->next; } return det; } //Find element void SearchN(const Link* head,const int n) { int detS = 0; while (head->next != NULL) { if (head->number == n) { detS = head->xuhao; break; } head = head->next; } printf("%d %d\n", detS, n); } //Tail insert element void Insert(Link* head,const int s,const int n) { Link* p = NULL; p = (Link*)malloc(LEN); if (p == NULL) { return; } while (head->next != NULL) { if (head->xuhao == s) { p->xuhao = s + 1; p->number = n; p->next = head->next; head->next = p; while (p->next != NULL) { p->xuhao += 1; p = p->next; } break; } head = head->next; } } //Delete element void Delete(Link* head,const int n) { Link* det = NULL, * tmp = head; int i = 0; while (i < n && tmp->next != NULL) { det = tmp; tmp = tmp->next; i++; } if (tmp != NULL) { det->next = tmp->next; free(tmp); } } int main() { int n = 0; scanf("%d", &n); int arr[10] = { 0 }; int i = 0; while (n) { arr[i] = n; i++; scanf("%d", &n); } Link* head = Creat(i, arr); Output(head); int S = 0; scanf("%d", &S); S = SreachS(head,S); printf("%d\n", S); int N = 0; scanf("%d", &N); SearchN(head, N); int N1 = 0, S1 = 0; scanf("%d %d", &S1, &N1); Insert(head, S1, N1); Output(head); int S2 = 0; scanf("%d", &S2); Delete(head, S2); Output(head); return 0; }