1th. 2nd Section 4 Deleting a specified interval node

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.