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


struct node {
    int data;
    struct node *next;
};

int insert (struct node *head, int data);
int print (struct node *head);

int main()
{
    struct node *head;

    head = NULL;
    // printf("%d\n",head);
    insert(&head,5);
    insert(&head,4);
    insert(&head,6);
    print(&head);
    print(&head);
    print(&head);
}

int insert(struct node *head,int data) {
    if(head == NULL) {
        head = malloc(sizeof(struct node));
        head->next = NULL;
        head->data = data;
        // printf("%d\n",data);
    }
    else {
        struct node *tmp = head;
        if(tmp->next!=NULL) {
            tmp = tmp->next;
        }

        tmp->next  = malloc(sizeof(struct node));
        tmp->next->next = NULL;
        tmp->next->data = data;
        // printf("%d\n",data);
    }
}

int print (struct node *head) {
    printf("hello entered here\n");
    struct node *tmp = head;

    if (head == NULL) {
        printf("entered null\n");
        return;
    }

    while (tmp != NULL) {
        if (tmp->next == NULL) {
            printf("%0d", tmp->data);
        } else {
            printf("%0d -> ", tmp->data);
        }
        tmp = tmp->next;
    }
    printf("\n");
}

编译时收到以下警告

In function main:
insert.c:16: warning: passing argument 1 of insert from incompatible pointer type
insert.c:17: warning: passing argument 1 of insert from incompatible pointer type
insert.c:18: warning: passing argument 1 of insert from incompatible pointer type
insert.c:19: warning: passing argument 1 of print from incompatible pointer type
insert.c:20: warning: passing argument 1 of print from incompatible pointer type
insert.c:21: warning: passing argument 1 of print from incompatible pointer type

当我运行它时,我会得到以下输出

hello entered here
0 -> 5 -> 6
hello entered here
0 -> 5 -> 6
hello entered here
0 -> 5 -> 6

请帮我删除此警告。
你也可以帮我添加一个函数来删除C中的节点
我在做什么错误?
我应该传递**head给函数吗?

4

2 回答 2

0

你的insert()气味——太复杂了。这实际上是某种 OO。

这是我的方式,直接输入:

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

// class node_t
typedef struct __node_s *node_t; // Mind the pointer here.
struct __node_s {
    int data;
    node_t next;
};
node_t node_init(void); // Constructor.
void node_append(node_t, int);
void node_drop_last(node_t);
void node_print(node_t);
void node_fini(node_t); // Destructor.
// end class node_t

int main(void)
{
    node_t head = node_init();
    node_append(head, 5);
    node_append(head, 4);
    node_append(head, 6);
    node_print(head);
    node_drop_last(head);
    node_print(head);
    node_fini(head);
    head = NULL;
    return 0;
}

node_t node_init(void)
{
     node_t node = malloc(sizeof(struct __node_s));
     assert(node);
     memset(node, 0, sizeof(struct __node_s));
     return node;
}

void node_insert(node_t head, int data)
{
    node_t last = head, new = node_init();
    for (; last->next; last = last->next);
    new->data = data;
    last->next = new;
}

void node_drop_last(node_t head)
{
    node_t last = head;
    if (!head->next)
        return;
    for (; last->next->next; last - last->next);
    node_fini(last->next);
    last->next = NULL;
}

void node_print(node_t head)
{
    for (node_t this = head->next; this; this = this->next)
    {
        printf("%d", this->data);
        if (this->next)
            putchar(' '); // A lot faster!
    }
    putchar('\n');
}

void node_fini(node_t head)
{
    if (head->next)
    {
        node_fini(head->next);
        head->next = NULL;
    }
    free(head);
}
于 2013-06-06T10:37:51.313 回答
0

目前,函数 print() 和 insert() 期望在struct node*您传递struct node **. 如果要传递副本&,请在代码中放入函数调用。

如果要修改指针,请将指针head传递给指针并相应地修改参数:

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


    struct node {
      int data;
      struct node *next;
    };
    int insert (struct node **head, int data);
    int  print  (struct node **head);

    int main()
    {
     struct node *head;
     head = NULL;
    // printf("%d\n",head);
     insert(&head,5);
     insert(&head,4);
     insert(&head,6);
     print(&head);
     print(&head);
     print(&head);

    } 
    int  insert(struct node **head,int data){

      if(*head == NULL){
        *head = malloc(sizeof(struct node));
        (*head)->next = NULL;
        (*head)->data = data;
    //    printf("%d\n",data);
      }
    else {
      struct node *tmp = *head;
      if(tmp->next!=NULL){
        tmp = tmp->next;
      }
    tmp->next  = malloc(sizeof(struct node));
      tmp->next->next = NULL;
      tmp->next->data = data;
    //  printf("%d\n",data);
    }

    }


    int print (struct node **head) {
      printf("hello entered here\n");
        struct node *tmp = *head;
        if (*head == NULL) {
          printf("entered null\n");
            return;
        }
        while (tmp != NULL) {
            if (tmp->next == NULL) {
                printf("%0d", tmp->data);
            } else {
                printf("%0d -> ", tmp->data);
            }
            tmp = tmp->next;
        }
        printf("\n");
    }
于 2013-06-06T07:27:55.423 回答