-1

我想知道我的程序设计是否正确,以及了解我的评论区域是否正在做它应该做的事情。我得到这些可能与我的代码注释段相关的编译错误,我会撒谎以获得一些帮助。谢谢!

  part1.c:15:6: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'insert'
  part1.c: In function 'main':
  part1.c:43:14: error: incompatible types when assigning to type 'struct point' from               type 'int'
  part1.c:49:44: error: invalid type argument of '->' (have 'struct point')
  part1.c:49:59: error: invalid type argument of '->' (have 'struct point')
  part1.c:55:5: error: incompatible type for argument 1 of 'free'
 /usr/include/stdlib.h:488:13: note: expected 'void *' but argument is of type 'struct point'






char *chars[3]= {"a","b","c"};

int nums[3]= {5,8,9};

struct point {char *letter;
                int number;
           struct point *next;};

struct point* insert(struct point list[],char *rqdLetters, int rqdNums) 
{  
     struct point *new;

     new = (struct point*)malloc(sizeof(struct point));
     if(new == NULL)
     fprintf(stderr,"error!");

     new->letter = rqdLetters;
     new->number = rqdNums; 

     new->next = head;
     head = new; 

     //not sure if i'm returning the a pointer to the start of new list
     return head;
}

int main(int argc, char **argv)                                             
{
   //not sure if i need to declare these here or in the insert 
    struct point list[3];          
    struct point *head = NULL;
    struct point *next; 
    struct point *new;

    int i;
    for (i = 0; i < 3; i++) 
    {
       //return result put back into the pointer to the start of the list
       head[i] = insert(list[i], chars[i], nums[i]);
    }

    int j;
    for(j = 0; j < 3; j++)
    {
       printf("letter %s and number %d\n", list[j]->letter, list[j]->number);
    }

    int z;
    for(z = 0; z < 3; z++)
    {
       free(list[z]);
    }

    return 0;
  }
4

1 回答 1

1

乍一看,您的代码存在几个问题。首先,您没有正确声明变量。

new = list;

应该:

struct point* new;

您的函数签名看起来也有点可疑。如果你返回一个指向你的数据结构的指针,它应该是这样的:

struct point* insert(...) { ... }

在更一般的层面上,我确实似乎您对链表的想法可能有点偏离。要表示一个列表,您应该只需要保留列表的headtail,而不是保留一个点数组。

如果您创建一个数据结构来保存这些指针,通常会有所帮助。然后,您可以将此结构传递给对列表进行操作的函数,例如insert()函数。

作为一个简单的例子(未经测试):

struct node {
  struct node *next;
  char letter;
  int number;
}

struct list {
  struct node *head;
  struct node *tail;
}

/* create a new list */
struct list* list_new(void) {
  struct list *L = malloc(sizeof(struct list));
  L->head = NULL;
  L->tail = NULL;
}

/* add a new node to the list */
void list_insert(struct list *list, char in_letter, int in_number) {
  struct node *node = malloc(sizeof(struct node));
  node->letter = in_letter;
  node->number = in_number;
  node->next = NULL;

  if (list->head == NULL) {  /* empty list */
    list->head = node;
    list->tail = node;
  } else { /* append to list */
    list->tail->next = node;
    list->tail = node;
  }
}

然后,您可以这样使用它:

int i;
char chars[3]= {"a","b","c"};
int nums[3]= {5,8,9};

struct list *mylist = list_new();

for (i = 0; i < 3; i++) 
{
   list_insert(mylist, chars[i], nums[i]);
}

回应:

...而且我不确定我是否应该在 insert 或 main 中声明它,但是我在 main 中声明了

这取决于您打算在何处使用变量以及这些变量的预期寿命。如上面评论中所述,您可能希望加深对范围规则的理解。

于 2012-08-01T16:40:38.147 回答