【问题标题】:Move pointers in order to print in a different order移动指针以便以不同的顺序打印
【发布时间】:2014-08-30 13:30:50
【问题描述】:

我必须完成这个程序。

我有一个这样的文件

Name iD Num_of_elements elem(1) elem(2), ... , elem(n)
james 1 3 AAA BBB CCC
arthur 2 2 EEE FFF
james 1 1 KKK
irine 3 4 EEE FFF DDD AAA
james 1 1 XXX

我需要创建一个列表,将文件加载到列表中并按如下顺序打印:

james 1 3 AAA BBB CCC
james 1 1 XXX
james 1 1 KKK
arthur 2 2 EEE FFF
irine 3 4 EEE FFF DDD AAA

(必须先打印具有相同 iD 的人,然后再打印其他人)。

我已经在 ANSI C 中创建了部分程序,但我无法按照要求完成“最终功能”。

void printListOrderedByiD(struct list *top) { }

这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define len 35

struct elements
{
    char name[len];
};

struct list
{
    char name[len];
    int id;
    int numOfElements;
    struct elements *pElements; /* pointer to the struct elements */
    struct list *next;
};


FILE *openFile(FILE *fp)
{
    fp=fopen("file.txt", "r");
        if (fp==NULL)
            {
                perror("");
                exit(1);
            }
    return (fp);
}


struct list *newNode(FILE *fp)
{
    int i=0;
    struct list *temp=(struct list *)malloc(sizeof(struct list));

    fscanf(fp, "%s\t%d\t%d\t", temp->name, &temp->id, &temp->numOfElements);

    temp->pElements=(struct elements *)malloc(temp->numOfElements*sizeof(struct elements));

        for (i=0; i<temp->numOfElements; i++)
        {
            fscanf(fp, "%s\t", temp->pElements[i].name);
        }

        temp->next=NULL;

    return temp;
}

struct list *insertAsLast(struct list *top, FILE *fp) /* this function will insert every node at the end of the list */
{
    if (top==NULL)
    {
        top=newNode(fp);
    }
    else
    {
        top->next=insertAsLast(top->next, fp);
    }

return top;
}

void printList(struct list *top) /* this procedure will stamp the list as loades from the file */
{
    int i=0;

    if (top==NULL)
    {
        printf("//\n");
    }
    else
    {
        printf("%s %d %d ", top->name, top->id, top->numOfElements);

            for (i=0; i<top->numOfElements; i++)
            {
                printf("%s ", top->pElements[i].name);
            }
            printf("\n");

        printList(top->next);
    }
}


int main()
{
    struct list *top=NULL;
    char firstLine[200];
    FILE *fp=NULL;
    fp=openFile(fp);

        fgets(firstLine, 200, fp); /* in order to jump the 1st line */

            while (!feof(fp))
            {
                top=insertAsLast(top, fp);
            }
    fclose (fp);

    printList(top);

    return 0;
}

谁能帮帮我?

【问题讨论】:

    标签: c pointers linked-list ansi


    【解决方案1】:

    按临时存储在数组中的ID排序。

    static int cmp(const void *a, const void *b){
        int x = (*(struct list**)a)->id;
        int y = (*(struct list**)b)->id;
        return x < y ? -1 : x > y;
    }
    void printListOrderdByID(struct list *top){
        struct list **table, *p;
        size_t size=16, n=0, i;
        table = malloc(size*sizeof(struct list*));
        //Can be secured in the malloc simply if you use it the number of elements already known from a reading from a file.
        for(p=top; p ; p=p->next){
            table[n++] = p;
            if(n == size)
                table = realloc(table, (size+=16)*sizeof(struct list*));
        }
        qsort(table, n, sizeof *table, cmp);
        for(i = 0; i < n; ++i){
            int j;
            p = table[i];
            printf("%s %d %d ", p->name, p->id, p->numOfElements);
            for (j=0; j<p->numOfElements; j++){
                printf("%s ", p->pElements[j].name);
            }
            printf("\n");
        }
        free(table);
    }
    

    【讨论】:

    • 请你解释一下什么是 static int cmp(const void <i>a, const void *b){ int x = (*(struct list*</i>)a) -&gt;身份证; int y = (<i>(struct list*</i>)b)-&gt;id;返回 x y; } 谢谢
    • @Mariadegregorio 数组的元素将指向该元素的指针作为void * 传递给qsort 中使用的比较函数。所以(struct list**)aconst void *a --> struct list ***struct list ** --> struct list *
    • @Mariadegregorio 如果第一个参数分别被认为小于、等于或大于第二个,则比较函数应返回一个小于、等于或大于零的整数。
    • 对不起,如果我想订购列表然后使用普通的“printList”?
    • 当然......就像我在我的代码中所做的那样......我在问是否容易 - 通过 ID 对列表进行排序 - 使用 printList(top);由于我真的是编程新手,我觉得你的代码对我来说有点难......我稍后会尝试学习它,现在我不在家:(谢谢你的支持和你的回答. 周末愉快。:D
    猜你喜欢
    • 1970-01-01
    • 2020-06-13
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    相关资源
    最近更新 更多