Problem description
Set the data values of all the elements in a single linked list with a header node unordered, and try to write a function that removes all the nodes whose values in the table are between the given value S and t (containing S and T, which require s<t)
Algorithmic thinking
Because the list is logically unordered, deleting the specified interval nodes is a prerequisite for locating these specified interval nodes. Therefore, starting from the beginning of the entire list of nodes to traverse, if found to meet the test instructions node, delete.
Algorithm description
void Delts(LNode* head, ElemType t, ElemType s){ LNode *pre=head; LNode *p=head->next; while(p){ if(p->data>s&&p->data<t){ pre->next=p->next; free(p); p=pre->next; }else{ pre=p; p=p->next; } }}
See the attached code for details.
Annex 1
#include <stdio.h>#include <stdlib.h>typedef intElemtype;typedef structlnode{elemtype data;structLnode *next;} Lnode, *linklist; Linklist Creatlnode (lnode*);voidDelts (lnode*, Elemtype, elemtype);voidPrint (lnode*);intMainintargcChar* argv[]) {Lnode *head; Head= (lnode*)malloc(sizeof(Lnode)); head->next=null; Head=creatlnode (head); Print (head); Elemtype t=7; Elemtype s=3; Delts (Head, T, s); Print (head);return 0;}//head interpolation to create a single linked listLinklist Creatlnode (lnode* head) {Lnode *l; Elemtype x;scanf("%d", &x); while(x!=999) {l= (lnode*)malloc(sizeof(Lnode)); l->data=x; l->next=head->next; head->next=l;scanf("%d", &x); }returnHead;}//Find and delete specified interval nodesvoidDelts (lnode* head, Elemtype T, Elemtype s) {Lnode *pre=head; Lnode *p=head->next; while(p) {if(p->data>s&&p->data<t) {pre->next=p->next; Free(p); p=pre->next; }Else{pre=p; p=p->next; } }}//Print all nodesvoidPrint (Lnode *head) {Lnode *p=head->next; while(p) {printf("%4D", P->data); p=p->next; }printf("\ n");}
1th. 2nd Section 4 Deleting a specified interval node