2013-06-06 131 views
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函数?

+0

你尝试打印(头);代替? –

+0

printf(“%d”,head)in main打印0 –

回答

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"); 
    } 
+0

我只想通过struct node * head我不想用**头 –

+0

嘿嘿嘿*头和(*头)之间的区别是什么。 –

+0

你真的想要使用**头,因为你似乎想修改指针头。 ' - >'绑定比*更紧密,因此'* head-> next'是'*(head-> next)',而您想使用'(* head) - > next''。 这就是为什么有必要有括号。 –

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); 
}