单链表连续删除

来源:互联网 发布:网络用语arp是什么意思 编辑:程序博客网 时间:2024/05/17 09:32
#include<iostream>using namespace std;typedef struct Node {int val;Node *next;}LST;LST *mycreat(int n){LST *p=NULL,*sec=NULL;LST *head=new LST;p=head;while(n--){int v;scanf("%d",&v);LST *sec=new LST;sec->val=v;p->next=sec;p=sec;}p->next=NULL;return head;}void myprint(LST *head){int f=0;LST *p;p=head->next;while(p!=NULL){if (f==0)printf("%d",p->val);elseprintf(" %d",p->val);f++;p=p->next;}printf("\n");return ;}LST *mydel(LST *head,int i,int l){LST *p=NULL,*q=NULL;p=head;int k=0;for (k=0;k<i-1;k++){p=p->next;}if(p->next)q=p->next;elsereturn head;l--;while(l--){q=q->next;}if (q->next==NULL)p->next=NULL;elsep->next=q->next;delete q;return head;}void del(LST *p){LST *q;while(p!=NULL){q=p;p=p->next;delete q;}}int main(){int n,i,l;while(~scanf("%d",&n)){LST *head;head=mycreat(n);scanf("%d%d",&i,&l);if(i+l-1>n||i==0||l<0||i>n) printf("data error\n");else if(l==0)myprint(head);else{head=mydel(head,i,l);if(head->next)myprint(head);}del(head);}return 0;}

原创粉丝点击