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