선형 시계의 체인식 실현

2236 단어

include


include


define ERROR -1


define OK 1


typedef struct LNode{ int data; struct LNode *next; }LNode;
void Output(LNode *head){ while(head->next != NULL){ head = head->next; printf("%d ", head->data); } printf(""); } LNode createLink(LNode *head, int n) {LNode:::* int i, head = p = (LNode *head, int n) 말loc(LNode *p, *qq, int i;::::::(LNode *);; p:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::>next = q;p = q;}Output(head); return *head; } void FindLink(LNode *L, int num){ LNode *p; p = L; int i = 0; while(p->data != num && p->next != NULL){ p = p->next; i++; } if(p->data == num){ printf("find:%d,pos:%d", p->data, i); } else{ printf("nofind:%d", num); }
} int insertLink(LNode *L, int i, int num){ LNode *p, *q; p = L; int j = 0; while(p && j < i-1){ p = p->next; j++; } if(!p || j > i-1){ return ERROR; } q = (LNode *)malloc(sizeof(LNode)); q->data = num; q->next = p->next; p->next = q; Output(L); return OK; }
int DeleLink(LNode *L, int i){ LNode *p, *q; int j = 0; p = L; while(p->next && j < i-1){ p = p->next; j++; } if(!(p->next) || j > i-1){ return ERROR; } q = p->next; p->next = q->next; free(q); Output(L); return OK; }
void MergeList(LNode *La, LNode *Lb, LNode *Lc){ LNode *pa, *pb, *pc; pa = La->next; pb = Lb->next; Lc = pc = La; while(pa && pb){ if(pa->data <= pb->data){ pc->next = pa; pc = pa; pa = pa->next; } else{ pc->next = pb; pc = pb; pb = pb->next; } } pc->next = pa?pa:pb; free(Lb); Output(Lc); }
int main() { LNode la, lb, lc; int i, num; la = createLink(&la, 5);
printf(" :");
scanf("%d", &num);
FindLink(&la, num);

printf(" :");
scanf("%d", &i);
printf(" :");
scanf("%d", &num);
insertLink(&la, i, num);

printf(" :");
scanf("%d", &i);
DeleLink(&la, i);

lb = createLink(&lb, 6);
printf(" :");
MergeList(&la, &lb, &lc);
return 0;

}

좋은 웹페이지 즐겨찾기