6 - 1 순환 단일 링크 구간 삭제 (15 분)

3973 단어 데이터 구조
이 문 제 는 선두 결점 의 순환 단일 체인 표 의 생 성과 단일 체인 표 의 구간 삭 제 를 실현 해 야 한다.L 은 앞장 서 는 노드 의 순환 싱글 체인 시트 입 니 다. 함수 ListCreateCL 은 순환 싱글 체인 시트, 함수 ListDelete 를 만 드 는 데 사 용 됩 니 다.CL 은 min 보다 작은 링크 요 소 를 삭제 하 는 데 사 용 됩 니 다.
함수 인터페이스 정의:
    Status ListCreate_CL(LinkList &CL); 
    void ListDelete_CL(LinkList &CL,ElemType min,ElemType max);

//        
#include
#include
#include

//       
#define TRUE        1
#define FALSE       0
#define OK          1
#define ERROR       0
#define INFEASIBLE -1
#define OVERFLOW   -2

typedef 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("        !!!
"); return -1; } scanf("%d%d",&min,&max); ListDelete_CL(CL,min,max); ListPrint_CL(CL); return 0; } /* */

: n, , n , 。 min max。

: , , 。

6
1 2 3 4 5 6
2 5


1 2 5 6

코드:
Status ListCreate_CL(LinkList &CL) {
    //     
    CL = (LNode*)malloc(sizeof(LNode));
    if(!CL)
        exit(OVERFLOW);
    // CL  rearPtr curPtr, n  ,curPtr next  CL
    LNode* curPtr;
    LNode* rearPtr;
    rearPtr = CL;
    curPtr = CL;
    int n;
    scanf("%d",&n);
    //  ,  n    ,     next     ,
    //rearPtr     
    for(int i = 0; i < n; i++) {
        curPtr = (LNode*)malloc(sizeof(LNode));
        if(!curPtr)
            exit(OVERFLOW);
        scanf("%d",&curPtr->data);
        rearPtr->next = curPtr;
        rearPtr = curPtr;
    }
    curPtr->next = CL;

    return OK;
}

void ListDelete_CL(LinkList &CL, ElemType min, ElemType max) {
    //rearPtr     ,curPtr       
    LNode * curPtr;
    LNode* rearPtr;
    int _min = min,_max = max;
    rearPtr = CL;
    curPtr = CL->next;
    //           
    while(curPtr != CL) {
        //              ,rearPtr next  curPtr      
        //  curPtr    ,curPtr       
        if(curPtr->data > _min && curPtr->data < _max) {
            rearPtr->next = curPtr->next;
            free(curPtr);
            curPtr = rearPtr->next;
        }
        //     ,               
        else {
            rearPtr = rearPtr->next;
            curPtr = curPtr->next;
        }
    }
}

좋은 웹페이지 즐겨찾기