数据结构习题集(C语言版)2.33 实例代码

    科技2025-01-29  6

    2.33习题实例代码

    #include<stdio.h> #include<stdlib.h> // / 测试数据 /// char sample[10] = {'1', 'a', ',', 't', '5', '@', '%', '.', ':'}; / 测试数据 /// // typedef struct LNode{ char data; struct LNode *next; }LNode, *LinkList; /// /// 测试函数, 对原数组L进行初始化, 可以不用看 / void init(LinkList L){ int i; LinkList q, temp; q = L; for(i=0; i<9; ++i){ temp = (LinkList)malloc(sizeof(LNode)); temp->data = sample[i]; q->next = temp; q = temp; } q->next = NULL; } /// 测试函数, 对原数组L进行初始化, 可以不用看 / /// // 这里是2.33 的答案 void LiskDivideInto3CL(LinkList L, LinkList s1, LinkList s2, LinkList s3) { LinkList p, q, pt1, pt2, pt3; p = L->next; pt1 = s1; pt2 = s2; pt3 = s3; while(p){ if(p->data >= '0' && p->data<='9') { q = p; p = p->next; q->next = pt1->next; pt1->next = q; pt1 = pt1->next; } else if((p->data>='A' && p->data<='Z') || (p->data>='a' && p->data<='z')) { q = p; p = p->next; q->next = pt2->next; pt2->next = q; pt2 = pt2->next; } else{ q = p; p = p->next; q->next = pt3->next; pt3->next = q; pt3 = pt3->next; } q=L; free(q); } } int main(){ LNode L, s1, s2, s3; L.next = NULL; s1.next = &s1; s2.next = &s2; s3.next = &s3; / / 测试用, 可以忽略 / init(&L); q = L.next; while(q!=&s1) { printf("%c\n", q->data); q=q->next; } / 测试用, 可以忽略 / / LiskDivideInto3CL(&L, &s1, &s2, &s3); // // 测试 /// LinkList q; q = s1.next; while(q!=&s1) { printf("%c\n", q->data); q=q->next; } q = s2.next; while(q!=&s2) { printf("%c\n", q->data); q=q->next; } q = s3.next; while(q!=&s3) { printf("%c\n", q->data); q=q->next; } // 测试 /// // return 0; }
    Processed: 0.008, SQL: 8