PTA 循环单链表区间删除

来源:互联网 发布:哪些手机支持4g十网络 编辑:程序博客网 时间:2024/05/29 14:42
#include <iostream>using namespace std;//库函数头文件包含#include<stdio.h>#include<malloc.h>#include<stdlib.h>//函数状态码定义#define TRUE        1#define FALSE       0#define OK          1#define ERROR       0#define INFEASIBLE -1#define OVERFLOW   -2typedef int  Status;typedef int  ElemType; //假设线性表中的元素均为整型typedef struct LNode{    ElemType data;    struct LNode *next;}LNode,*LinkList; //循环单链表类型定义与单链表定义相同,区别在尾节点next取值Status ListCreate_CL(LinkList &CL);void ListDelete_CL(LinkList &CL, ElemType min, ElemType max);void ListPrint_CL(LinkList &CL){   //输出单链表,空表时输出Empty List。    LNode *p=CL->next;  //p指向第一个元素结点    if(p==CL){      printf("Empty List");      return;    }    while(p!=CL)    {        if(p->next!=CL)            printf("%d ",p->data);        else            printf("%d",p->data);        p=p->next;    }}int main(){    LinkList CL;    ElemType min,max;    if(ListCreate_CL(CL)!= OK)    {       printf("循环链表创建失败!!!\n");       return -1;    }    scanf("%d%d",&min,&max);    ListDelete_CL(CL,min,max);    ListPrint_CL(CL);    return 0;}/* 请在这里填写答案 */Status ListCreate_CL(LinkList &CL){      LNode *curPtr,*rearPtr;      int n;      cin>>n;      //开辟新的头结点,初始化它的值;      CL = new LNode;      CL->next = NULL;      rearPtr = CL;      //循环赋值,赋值后把新结点拼接在尾结点后,更新尾结点      for(int i = 0;i < n ;i++)      {            curPtr = new LNode;            if(!curPtr)  exit(OVERFLOW);                  cin>>curPtr->data;            rearPtr->next = curPtr;            rearPtr = curPtr;      }      //循环结束后,让尾结点的Next成员指向头指针      rearPtr->next = CL;      return OK;}void ListDelete_CL(LinkList &CL, ElemType min, ElemType max){      LNode *p;      LNode *curPtr;      p=CL;      curPtr = CL->next;      while(curPtr->next != CL)      {           if(curPtr->data > min && curPtr->data < max)           {                       p->next = curPtr->next;                       curPtr =curPtr ->next;           }           else           {                 p = p->next;                 curPtr = curPtr->next;           }      }}