【问题标题】:Creating linked list with multiple element types创建具有多种元素类型的链表
【发布时间】:2018-12-08 23:50:01
【问题描述】:

我的 C 课程上周有期末课,在学期结束前我们还有 1 周的课程,我的教授决定取消其余的课程。

我问他为什么不利用这段时间教我们链表,他说“决赛已经给出,所以我不需要教其他任何东西”。

我仍然想学习链表,现在由于我的教授没有任何用处,我想请各位好心人帮助我完成我正在考虑的应用程序。

简单地说,我有一个文本文件,其中包含如下行:

Apple     //name   
12        //quantity   
23.90     //price   
Bananas   //name  
4         //quantity  
12.90     //price

我了解如何将上述 txt 文件读入结构数组,但我不明白如何对链表执行相同操作。

我的结构示例:

typedef struct
{
char food_name[BUF];
int food_quantity;
float food_cost;
}FOOD;

我很乐意将我的其余代码作为示例发布,但由于我还有 2 年的大学毕业时间,并且在接下来的 3 个学期中还有上面提到的同一位教授,我不想冒学术不诚实和明年有潜力的学生回答我的家庭作业(如果明年这个班的任何人看到这个,请在​​ HW3 上找到)

【问题讨论】:

  • 你的结构不是链表中的链。链表有一个指向链表中下一个元素的指针。 struct food_s { .......; struct food_s *next; }

标签: c data-structures linked-list


【解决方案1】:

结构的链表有一个指向下一个结构的附加指针,用于将一个结构连接到下一个结构。一个简单的单链表如下所示:

typedef struct food_s {
  char  food_name[BUF];
  int   food_quantity;
  float food_cost;
  struct food_s *next;
}food_t;

这只是一个小小的开始,但是有几本书描述了链表、双链表、二叉树等排序列表,...

【讨论】:

    【解决方案2】:

    这里有一些粗略代码可以帮助您入门,我很懒惰并且忽略了释放分配的 FOOD 内存。

    通过示例进行跟踪时,我学得最好。我鼓励您密切注意指针显示的值(各种 printfs 中的 %p)。例如,name="Cherries" qty=8 cost=1.23 next=0xa22070 中的 next=0xa22070 来自哪里?

    随着您为各种数据结构(链表、树、队列)编写更高级的代码,指针和内存管理将变得越来越重要。

    祝你学习曲线顺利:-)

    输出

    $ gcc food.c 
    $ ./a.out
    --- directly calling print_food() ---
    FOOD addr=0xa22010  name="Apple" qty=12 cost=23.90 next=(nil)
    FOOD addr=0xa22070  name="Bananas" qty=4 cost=12.90 next=0xa22010
    FOOD addr=0xa220d0  name="Cherries" qty=8 cost=1.23 next=0xa22070
    --- calling print_food_list() ---
    food list #001: FOOD addr=0xa220d0  name="Cherries" qty=8 cost=1.23 next=0xa22070
    food list #002: FOOD addr=0xa22070  name="Bananas" qty=4 cost=12.90 next=0xa22010
    food list #003: FOOD addr=0xa22010  name="Apple" qty=12 cost=23.90 next=(nil)
    Done, n=3
    $ 
    

    food.c

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define BUF 64
    
    typedef struct food
    {
       char  name[BUF];
       int   quantity;
       float cost;
       struct food  *next; // ptr to next food, otherwise null.
    } FOOD;
    
    FOOD *make_food(char *name, int quantity, float cost, FOOD *next) {
        FOOD *p = malloc(sizeof(FOOD));
        strcpy( p->name, name);
        p->quantity = quantity;
        p->cost = cost;
        p->next = next;
        return p;
    }
    
    void print_food(FOOD *p) {
        printf("FOOD addr=%p  name=\"%s\" qty=%d cost=%.2f next=%p\n",
            p, p->name, p->quantity, p->cost, p->next);
    }
    
    int print_food_list(FOOD *p) {
        int length = 0;
        while( p ) {
            ++length;
            printf("food list #%03d: ", length); // note lack of \n
            print_food(p);
            p = p->next;
        }
        return length;
    }
    
    void main(int argc, char** argv) {
        FOOD *a = make_food( "Apple", 12, 23.90, (FOOD *)NULL );
        FOOD *b = make_food( "Bananas", 4, 12.90, a);
        FOOD *c = make_food( "Cherries", 8, 1.23, b);
        printf("--- directly calling print_food() ---\n");
        print_food(a);
        print_food(b);
        print_food(c);
        printf("--- calling print_food_list() ---\n");
        int n = print_food_list(c);
        // int n = print_food_list(a); // what would happen with a?
        printf("Done, n=%d\n", n);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-23
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-02
      • 1970-01-01
      相关资源
      最近更新 更多