链表的删除

来源:互联网 发布:淘宝摄影师价格 编辑:程序博客网 时间:2024/06/06 00:59
#include <stdio.h>#include <stdlib.h>#include <malloc.h>using namespace std;#define TRUE        1#define FALSE       0#define OK          1#define ERROR       0#define INFEASIBLE  -1#define OVERFLOW    -2typedef int ElemType;typedef int Status;#define LIST_INIT_SIZE 100#define LISTINCREMENT 10;typedef struct LNode{   ElemType data;   struct LNode *next;}*LinkList,LNode;Status InitList(LinkList &L,ElemType n){    LNode *rea,*tmp;    L = (LNode *)malloc(sizeof(LNode));    L->next=NULL;    rea=L;    for(int i=0;i<n;i++){        ElemType m;        scanf("%d",&m);        tmp=(LNode*)malloc(sizeof(LNode));        tmp->data=m;        if(!tmp) exit(OVERFLOW);        rea->next=tmp;        rea=tmp;    }    rea->next=L;    return OK;}void ListDelete_L(LinkList &L,ElemType min,ElemType max){    LNode *p,*sub;    p=L;    while(p->next!=L){        sub=p->next;        if(sub->data>min&&sub->data<max){            p->next=sub->next;        }        else p=p->next;    }}void print_LinkList(LinkList &L){    LNode *p=L->next;    if(p==L){        printf("Empty List!");        return;    }    while(p!=L){        if(p->next==L)            printf("%d\n",*p);        else            printf("%d ",*p);        p=p->next;    }}int main(){    int n;    LinkList L;    scanf("%d",&n);    if(InitList(L,n)){        printf("创建成功!\n");    }    else{        printf("创建失败!\n");    }    int m1,m2;    scanf("%d %d",&m1,&m2);    ListDelete_L(L,m1,m2);    print_LinkList(L);    return 0;}