【问题标题】:Linked list storing prime numbers from 1 to 1000存储从 1 到 1000 的素数的链表
【发布时间】:2012-04-27 12:36:02
【问题描述】:

正如您将在以下程序的 cmets 中看到的,我应该创建一个列表来存储从 1 到 1000 的所有素数并释放节点。

其中只有两个功能是我的工作。但是,我已经很久没有弄清楚为什么这个程序不能编译了。各位看错了吗?这是一篇已经交上来的作业,仅供个人参考。

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

/* given data structure declaration */
struct record {
  int             data;
  struct record * next;
};

typedef   struct record   RecordType;



/* DO NOT MODIFY */
/* print a list */
void print_list(RecordType * list)
{
  RecordType * visitor = list;
  int count = 0;

  while (visitor != NULL)
  {
    printf("%d ", visitor->data);
    visitor = visitor->next;
    count++;
  }
  printf("\n");
  printf("There are %d items in the list.\n", count);
}



/* MY WORK HERE */
/* free every node in the list */
void free_list(RecordType * list)
{
    while (list->data != 2){
        free(list->next);
        list->next = list;
    }
}

/* MY WORK HERE */
/* this function may call other functions created by students */
/* create a list storing all prime numbers in [1, 1000] in ascending order */
/* return a pointer to the starting point of the list */
RecordType * create_list_prime_in_1_to_1000()
{
    RecordType * begin, *tail, *temp;
    int i = 0;
    begin = malloc(sizeof(RecordType));
    begin->data = 0;
    begin->next = NULL;
    tail = begin;
    while(i<1000){
        temp = malloc(sizeof(RecordType));
        temp -> data = ++i;
        tail -> next = temp;
        tail -> temp;
        tail -> next = NULL;
    }
}

int isPrime(int n){
    int d;

    for (d = 2; d < n; d = d + 1)
        if (n % d == 0)
            return 0;

    return 1;
}





/* DO NOT MODIFY */
/* main program */
int main(void)
{
  RecordType * start;

  /* create a linked list to store all the prime numbers in 1 - 10 */
  /* this is a naive way by hard-coding */

  start = malloc(sizeof(RecordType));
  start->data = 2;
  start->next = malloc(sizeof(RecordType));
  start->next->data = 3;
  start->next->next = malloc(sizeof(RecordType));
  start->next->next->data = 5;
  start->next->next->next = malloc(sizeof(RecordType));
  start->next->next->next->data = 7;
  start->next->next->next->next = NULL;

  print_list(start);

  free_list(start);


  /* i am expected to expected to build a list iteratively rather than hard-code */

  start = create_list_prime_in_1_to_1000();

  print_list(start);

  free_list(start);

  return 0;
}

【问题讨论】:

  • 什么是编译错误?请在问题中也发布它。
  • isPrime 的 for 循环中的条件可以是 d * d &lt;= n(想想为什么!:))将使测试更有效率。 (另外,d = d + 1 == d++
  • @dbaupp 或者更好的++d
  • 你们真是太棒了,你们是怎么在这么短的时间内读完这段代码的?哇..非常感谢你们!在提出任何后续问题之前,我将先吸收您的提示
  • error C2039: 'temp' : is not a member of 'record' 这是错误

标签: c linked-list


【解决方案1】:

您已将tail 声明为:

RecordType * begin, *tail, *temp;

RecordType 为:

struct record {
        int             data;
        struct record * next;
};    
typedef   struct record   RecordType;

接下来是:

tail -> temp;

因为RecordType 没有名为 temp 的成员,所以它不起作用。

我认为应该是:

tail = temp;

运行时错误的原因似乎是因为:

void free_list(RecordType * list)
{
        while (list->data != 2){
                free(list->next);
                list->next = list;
        }
}

这是不正确的。你需要这样的东西:

void free_list(RecordType * list)
{
        // keep going till there are nodes.
        while (list){
                // save the link to the rest of the nodes.
                RecordType *temp = list->next;

                // free the current node.
                free(list);

                // repeat the process starting at the next node.
                list = temp;
        }
}

【讨论】:

  • 非常感谢,我认为这现在更有意义了。谢谢!
猜你喜欢
  • 1970-01-01
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多