Beispiel #1
0
//查看列表
void print_list(list L)
{
    int page=1;
    char ch;
    int max_page=len_list(L)/NPAGE+(len_list(L)%NPAGE==0?0:1);
    for(;;)
    {
        CLS;
        //头列标记
        print_list_one_page(L,page);
        printf("\n==第 %d 页",page);
        if(page==max_page)printf(",尾页");
        printf("==\n");
        printf("使用“-”和“=”翻页,“s”排序,“0”回到主菜单\n");
        ch=getch();
        switch(ch)
        {
        case '-':
            if(page>1)
                --page;
            break;
        case '=':
            if(page<max_page)
                ++page;
            break;
        case '0':
            return;
            break;
        case 's':
            sort_list(L);
            page=1;
            break;
        }
    }
}
Beispiel #2
0
list *upto_last(const list *l)
{
    if (!l || len_list(l) < 2)
        return NULL;

    return copy_list(l, 0, len_list(l) - 1);
}
void main()
{
    int len, pos, *del;
    plist pl = NULL;
    del = (int*)malloc(sizeof(int));
    pl = init_list();
    isEmpty_list(pl);
    insert_item(pl, 1, 1);
    insert_item(pl, 2, 3);
    insert_item(pl, 3, 5);
    insert_item(pl, 4, 7);
    insert_item(pl, 5, 9);
    insert_item(pl, 6, 11);
    display(pl);
    len = len_list(pl);
    printf("link list len: %d\n", len);
    pos = locate_item(pl, 7);
    printf("num 7 pos: %d\n", pos);
    delete_item(pl, 3, del);
    printf("delete pos 3 num: %d\n", *del);
    display(pl);
    printf("link list traverse...\n");
    pl = traverse_list(pl);
    display(pl);
    destroy_list(pl);
    getch();
}
Beispiel #4
0
list *chunk_words(list *words, size_t chunk_size)
{
    size_t num_words = len_list(words);
    list *chunks = NULL;

    if (num_words <= chunk_size)
        return NULL;

    size_t num_chunks = num_words - chunk_size;
    if (num_chunks > num_words)
        return NULL;

    for (size_t i = 0; i < num_chunks; i++) {
        list *chunk = copy_list(words, i, i + chunk_size + 1);
        if (!chunk)
            error(EALLOC);
        append_list(&chunks, chunk, 1);
    }

    return chunks;
}
Beispiel #5
0
//排序
void sort_list(list L)
{
    int i,rule,flag;
    int len=len_list(L);
    printf("输入两个数字选择排序方式和正倒序,两个数字用空格隔开\n1-学号, 2-姓名, 3-数学, 4-语文, 5-英语  ,6-总分\n1-正序, 2-倒序\n");
    scanf("%d %d",&rule,&flag);
    rst_stdin();
    L=L->next;
    //冒泡排序
    for(i=len-1; i>0; i--)
    {
        list m=L;
        int j=0;
        for(; j<i; j++,m=m->next)
        {
            char *tmp1,*tmp2;
            switch(rule)
            {
            case 1:
                tmp1=m->num;
                tmp2=m->next->num;
                break;
            case 2:
                tmp1=m->name;
                tmp2=m->next->name;
                break;
            case 3:
                tmp1=m->Math;
                tmp2=m->next->Math;
                break;
            case 4:
                tmp1=m->Chinese;
                tmp2=m->next->Chinese;
                break;
            case 5:
                tmp1=m->English;
                tmp2=m->next->English;
                break;
            case 6:
            {
                char a_s[40],b_s[40];
                double a=(atof(m->Math)+atof(m->Chinese)+atof(m->English));
                double b=(atof(m->next->Math)+atof(m->next->Chinese)+atof(m->next->English));
                sprintf(a_s,"%f",a);
                sprintf(b_s,"%f",b);
                tmp1=a_s;
                tmp2=b_s;
            }
            }
            if(rule==3||rule==4||rule==5||rule==6)
            {
                if((flag==1 && atof(tmp1)>atof(tmp2)) || (flag==2 && atof(tmp1)<atof(tmp2)))
                {
                    swap_node(m,m->next);
                }
            }
            else
            {
                if((flag==1 && strcmp(tmp1,tmp2)>0) || (flag==2 && strcmp(tmp1,tmp2)<0))
                {
                    swap_node(m,m->next);
                }
            }
        }
    }
}