荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: Version (Who makes history and why), 信区: Program
标  题:  一道算法题解法(4) (转载)
发信站: 荔园晨风BBS站 (Mon Mar 24 18:07:15 2003), 站内信件

使用链表很简单。

#include <stdio.h>
#include <stdlib.h>

struct LINEARTABLE {
        unsigned        nValue;
        struct  LINEARTABLE *pNext;
};

void    SearchAndInsert(struct LINEARTABLE *, int);

void    main(int argc, char *argv[])
{
        struct  LINEARTABLE *pHead, *p;
        int     i;
        unsigned        n;

        pHead=malloc(sizeof(pHead[0]));
        pHead->nValue=1;
        pHead->pNext=NULL;

        for(i=0;i<1500;i++) {
                n=pHead->nValue;
                printf("%d:\t%d\n", i+1, n);

                SearchAndInsert(pHead, n*2);
                SearchAndInsert(pHead, n*3);
                SearchAndInsert(pHead, n*5);

                p=pHead;
                pHead=pHead->pNext;
                free(p);
        }
}


void    SearchAndInsert(struct LINEARTABLE *pHead, int nNum)
{
        struct  LINEARTABLE *p, *q, *r;

        q=pHead, p=q->pNext;
        while(p!=NULL) {
                if(p->nValue>=nNum) break;

                q=p, p=q->pNext;
        }
        if(p==NULL || p->nValue>nNum) {
                r=malloc(sizeof(r[0]));
                r->nValue=nNum;
                r->pNext=p;
                q->pNext=r;
        }
}

--
                      *
          *                                  *
                          *             *
                      no more to say
                  ★     just wish you   ★
                            good luck

※ 修改:·Version 於 Mar 24 18:19:50 修改本文·[FROM: 192.168.1.50]
※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.1.50]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店