How to delete substrings in a string

Source: Internet
Author: User
This article is mainly for you to share an article on how to delete strings in the string problem, has a good reference value, I hope to be helpful to everyone. Follow the small series together to see it.

Words don't say much, directly on the code.

Algorithm idea: Find the first node to be deleted, delete it individually.

#include <stdio.h> #include <stdlib.h>typedef char datatype;typedef struct node{datatype x; struct node *next;}    seqlist;//Create a single-linked list of lead nodes Seqlist *creat () {seqlist *p,*s,*head=null;    DataType ch;    Head= (Seqlist *) malloc (sizeof (seqlist));    P=head;    head->next=null;        while ((Ch=getchar ()) = ' \ n ') {s= (seqlist *) malloc (sizeof (seqlist));        s->x=ch;        p->next=s;    P=s;    } p->next=null; return head;}    Single-linked list traversal 1seqlist * DISPLAY1 (seqlist *head) {seqlist *p;    p=head->next;        while (p) {printf ("%c", p->x);    p=p->next;    } printf ("\ n"); return head;} The traversal of a single-linked list 2 creates this because 1 causes an error when the return value is null.    Seqlist * Display2 (seqlist *head) {seqlist *p;    P=head;        while (p) {printf ("%c", p->x);    p=p->next;    } printf ("\ n"); return head;}    Deletion of string substring seqlist * Del (seqlist * head,int i,int len) {seqlist *p,*q,*r;    P,q,r for moving, to replace the dead, to record the previous seat.    int k=1;    P=r=head; WhIle (P && k<=i) {r=p;        p=p->next;    k++;        } if (!p) {printf ("error1\t position out of range \ n");    return (NULL);        } else {k=1;                while (P && k<=len)//There is a need to pay special attention to export conditions {if (p==r) {p=p->next;                Q=p;                p=q->next;                r->next=q->next;                k++;            Free (q);                } else {q=p;                p=q->next;                r->next=q->next;                k++;            Free (q);            }} if (K<len) {printf ("Error 2\t length out of range \ n");        return (NULL);    } else return head;    }}int Main () {int i,len;    Seqlist *head;    Head=creat ();    Display1 (head);    scanf ("%d", &i);    scanf ("%d", &len);    Head=del (Head,i,len);    Display2 (head); return 0;}

Related recommendations:

Delete substrings in the specified string

To delete a substring in a string

Delete substrings in a string

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.