6 - 1 순환 단일 링크 구간 삭제 (15 분)
3973 단어 데이터 구조
함수 인터페이스 정의:
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;
}
}
}