"Ctrl AC! Together AC!"
catalogue
Definition and node of single linked list:
Definition of single linked list:
Structure of single linked list node:
Establishment of single linked list:
Insert a new node with data x into the single linked list:
Create linked list by tail interpolation:
Traversal of single linked list:
Find and delete nodes in the single linked list:
Find and delete node functions:
preface:
Before talking about single linked lists, let's talk about arrays:
Array is a linear data structure, which can realize random access to data elements, but a large number of elements need to be moved when inserting or deleting data in an ordered array and maintaining order; At the same time, the array occupies continuous storage space in the memory, and the size is fixed at the time of definition. When the amount of data exceeds the capacity of the array, the new storage space cannot be dynamically expanded.
The linked list with linear data structure can solve the above problems. Linked list is another effective storage structure of linear structure. It independently applies for storage space for each element in linear structure, and adds pointers in each element to record the position of its predecessor or successor elements. Therefore, it can improve the utilization of memory space and dynamic expansion.
Definition and node of single linked list:
Definition of single linked list:
A single linked list is composed of a series of structures (called nodes), in which each node contains not only all the attributes of the stored object, but also a pointer to the next node in the linked list.
The last node in the linked list contains a null pointer, and the position of the first node is remembered through an additional pointer.
Structure of single linked list node:
//First (recommended) struct node { int data; struct node* next; }; typedef struct node linknode; typedef linknode* linklist; linklist p;//Define linked list node pointer //Second struct node { int data; stuct node* next; }linknode; linknode* p;//Define linked list node pointer
In this way, the next node can be found through the pointer.
Establishment of single linked list:
Insert a new node with data x into the single linked list:
Prepare a pointer first:
linklist q; q=(linklist)malloc(sizeof(linknode)) //Request memory q->data = x //Store data
Insert the first case (insert to the front of the linked list or to an empty linked list)
q->next = head; head = q;
Insert the second case (after the node pointed to by p (p! = tail pointer))
q->next = p->next; p->next = q;
Insert the third case (insert to the end, tail is the tail pointer)
tail->next = q; tail = q; tail = NULL
Create linked list by tail interpolation:
linklist creatLink() { linklist head = NULL, tail == NULL, q; //Initial empty linked list, q is the temporary node int x; printf("Please enter an integer sequence to-1 end\n"); scanf("%d", &x); while (x != -1) { q = (linklist)malloc(sizeof(linknode)); q->data = x; if (head == NULL)//The linked list is empty head = tail = q; else//Linked list is not empty { tail->next = q; tail = q; } scanf("%d", &x); if (tail != NULL) tail->next = NULL;//End of list ending return head; }
Traversal of single linked list:
Traverse to free memory:
During the establishment of the linked list, the applied space needs to be released after it is used up, which can be released through traversal.
Such as code:
void freeLinklist(linklist head) {//Pass in a linked list linklist p; while (head != NULL) {//Traversal linked list p = head;//Remember the nodes to be released with p head = head->next;//head node movement free(p);//release } }
Traverse output data:
Such as code:
void print(linklist head) { linklist p = head; while (p != NULL) { printf("%d ", p->data);//output p = p->next;//move } }
Find and delete nodes in the single linked list:
Find node core code:
while (p != NULL && p->data != x) { p=p->next //Continue traversal without ending or not looking for x }
Delete node core code:
//First, delete the first node: head = p->next; free(p); //The second is non first node (pre is the precursor node of p) pre->next = p->next; free(p);
Find and delete node functions:
linklist deleteList(linklist head, int x) {//x is the data value of the node to be deleted linklist pre = NULL, p = head; while (p != NULL && p->data != x) { pre = p;//Record the precursor nodes of p in time with pre p = p->next; } if (p) { if (pre == NULL) {//If the first node is deleted head = head->next; } else { pre->next = p->next; } free(p); } return head; }
Example:
Such as:
Analysis:
Directly establish the function through the single linked list, delete the function with the maximum and minimum value, and output the answer function to solve the problem:
Such as code:
#include <stdio.h> #include <stdlib.h> int maxx = -1000, minn = 1000;//Record the highest and lowest scores struct node { int data; struct node* next; }; typedef struct node linknode; typedef linknode* linklist; linklist scan()/*Create linked list*/ { linklist head = NULL, tail = NULL, p; int x; printf("Input the scoring output of each judge-1 end\n"); scanf("%d", &x); if (x != -1) {//Record the highest and lowest points at all times maxx = x > maxx ? x : maxx; minn = x < minn ? x : minn; } while (x != -1) { p = (linklist)malloc(sizeof(linknode)); p->data = x; if (head == NULL) head = tail = p; else { tail->next = p; tail = p; } scanf("%d", &x); if (x != -1) {//Record the highest and lowest points at all times maxx = x > maxx ? x : maxx; minn = x < minn ? x : minn; } } if (tail != NULL) tail->next = NULL; return head;//Returns the created linked list } linklist deletemax(linklist head) {/*Delete highest score*/ linklist pre, p; pre = NULL; p = head; while (p != NULL && p->data != maxx) {//Find the node with the highest score pre = p; p = p->next; } if (pre == NULL) head = head->next; else pre->next = p->next; free(p); return head;//Return to the modified linked list } linklist deletemin(linklist head) {/*Delete lowest score*/ linklist pre, p; pre = NULL; p = head; while (p != NULL && p->data != minn) {//Find the node with the lowest score pre = p; p = p->next; } if (pre == NULL) head = head->next; else pre->next = p->next; free(p); return head;//Return to the modified linked list } void print(linklist head) {/*Output answer*/ linklist p = head; double sum = 0; double geshu = 0; printf("New list:\n"); while (p != NULL) { printf("%d ", p->data); sum += p->data; geshu++; p = p->next; } printf("\n"); printf("The final score is:%.2lf\n", (sum / geshu)); } void freeLinklist(linklist head) {/*Free memory*/ linklist p; while (head != NULL) { p = head; head = head->next; free(p); } } int main() { linklist head = scan();//Create a single linked list head = deletemax(head);//Delete highest score head = deletemin(head);//Delete lowest score print(head);//Output answer freeLinklist(head);//Free memory return 0; }
Output:
Thanks for reading!!!
"Ctrl AC! Together AC!"