c - 移动指针以便以不同的顺序打印

标签 c pointers linked-list ansi-c

我必须完成这个计划。

我有一个这样的文件

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(结构列表*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;
}

有人可以帮助我吗?

最佳答案

按照数组中临时存储的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);
}

关于c - 移动指针以便以不同的顺序打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25582886/

相关文章:

c++ - 如何使用新运算符 C++ 为 void 指针分配内存?

c - 为什么在没有人阅读后继续写入命名管道?

c - 在 OSX 上使用 GDB 检测堆栈损坏(在金丝雀值上设置观察点)

c++ - 指向数组指针的双星指针;如何访问数组的指针? C++

c - 为什么不打印我的 char 数组指针?

java - 在Java中递归地替换链表中的字符串

java - 反向链表问题

c - 请有人帮我解释链表吗?

c - 错误: Expected identifier or '(' before 'int'

c++ - 二维数组访问时间比较