【问题标题】:Pointers Linked Lists C指针链表 C
【发布时间】:2013-11-25 03:32:22
【问题描述】:

我做对了吗?我在这里遇到了一个奇怪的错误......

linked.c:在函数“push”中: linked.c:50:19:警告:来自不兼容指针类型的赋值 [默认启用] linked.c:在函数“main”中: linked.c:146:9:错误:“push”的参数 1 的类型不兼容 linked.c:45:6:注意:预期的“结构节点 **”,但参数的类型是“结构节点” ~/swen250/CLinkedList$ gcc -o linked linked.c linked.c:在函数“push”中: linked.c:50:19:警告:来自不兼容指针类型的赋值 [默认启用] ~/swen250/CLinkedList$ gcc -o linked linked.c linked.c:在函数“pop”中: linked.c:63:19:错误:请求成员“数据”不是结构或联合 linked.c:64:20:错误:在不是结构或联合的东西中请求成员“下一个” linked.c:在函数“copyList”中: linked.c:106:9:警告:从不兼容的指针类型传递“appendNode”的参数 1 [默认启用] linked.c:75:6:注意:预期为“struct node **”,但参数的类型为“struct node *”
#include <stdio.h>
#include <stdlib.h>

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

static int length(struct node** head);
static void push(struct node** head, int data);
static int pop(struct node** head);
static void appendNode(struct node** head, int data);
static struct node *copyList(struct node** head);
static void printList(struct node** head);



/************************************************************
 length - return length of a list
 ************************************************************/
int length(struct node** head) {
    int count = 0;
    struct node* current = NULL;

    current = *head;
    while (current != NULL) {
        current = current->next;
        ++count;
    }

    return count;
}


/************************************************************
 push - add new node at beginning of list
 ************************************************************/
void push(struct node** head, int data) {
    struct node* new_ptr = NULL;

    new_ptr = (struct node*)malloc(sizeof(struct node));
    new_ptr->data = data;
    new_ptr->next = *head;

    *head = new_ptr;
}

/************************************************************
 pop - delete node at beginning of non-empty list and return its data
 ************************************************************/
int pop(struct node** head) {
    int val = 0;
    struct node* temp = NULL;

    if (*head != NULL) {
        val = head->data;
        temp = head->next;
        free(head);
        *head = temp;
    }

    return(val);
}

/************************************************************
 appendNode - add new node at end of list
 ************************************************************/
void appendNode(struct node** head, int data) {
    struct node* current = NULL;
    struct node* previous = NULL;
    struct node* new_ptr = NULL;

    current = *head;
    previous = current;
    while (current != NULL) {
        previous = current;
        current = current->next;
    }

    new_ptr = (struct node*)malloc(sizeof(struct node));
    new_ptr->data = data;
    new_ptr->next = NULL;

    previous = new_ptr;

}

/************************************************************
 copyList - return new copy of list
 ************************************************************/
struct node* copyList(struct node** head) {
    struct node* copy = NULL;
    struct node* current = NULL;
    struct node* new_ptr = NULL;

    /* Copy current head to copy */
    current = *head;
    while (current != NULL) {
        appendNode(copy, current->data);
        current = current->next;
    }

    return copy;
}


/************************************************************
 printList - print linked list as "List: < 2, 5, 6 >" (example)
 ************************************************************/
void printList(struct node** head) {
    struct node* current = NULL;

    printf("List: < ");

    current = *head;
    if (current == NULL)
        printf("none ");

    while (current != NULL) {
        printf("%d", current->data);
        current = current->next;
        if (current != NULL)
            printf(", ");
    }

    printf(" >\n");
}

void main() {
    int i;                      // index used for loops
    struct node *list_a;        // a new list
    struct node *list_a_copy;   // copy of list
    list_a = NULL;                // initialize empty list
    list_a_copy = NULL;           // initialize empy list


    // test push
    for (i = 0; i < 4; ++i)
        push(&list_a, i);

    // test length
    printf("Length of list = %d\n", length(&list_a));

    // test print head list
    printf("head:\n");
    printList(&list_a);

    // test append node
    for (i = 4; i < 8; ++i)
        appendNode(&list_a, i);

    // test print head list
    printf("head(append):\n");
    printList(&list_a);

    // make a copy of list
    list_a_copy = copyList(&list_a);

    // test pop head list
    for (i = 0; i < 4; ++i)
        printf("%d popped\n", pop(&list_a));

    // test print copy list
    printf("head copy:\n");
    printList(&list_a_copy);

    // test pop copy list
    for (i = 0; i < 4; ++i)
        printf("%d popped\n", pop(&list_a_copy));

}

【问题讨论】:

  • 我看到的一个问题是,您在单指针的上下文中使用双指针。

标签: c pointers linked-list


【解决方案1】:

问题在于如何使用双指针。

这是完整的工作代码: 我对双指针的使用方式做了一些改变。 可以看到pop函数和copyList函数的变化。

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

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

static int length(struct node** head);
static void push(struct node** head, int data);
static int pop(struct node** head);
static void appendNode(struct node** head, int data);
static struct node *copyList(struct node** head);
static void printList(struct node** head);



/************************************************************
 length - return length of a list
************************************************************/
int length(struct node** head) {
  int count = 0;
  struct node* current = NULL;

  current = *head;
  while (current != NULL) {
    current = current->next;
    ++count;
  }

  return count;
}


/************************************************************
 push - add new node at beginning of list
************************************************************/
void push(struct node** head, int data) {
  struct node* new_ptr = NULL;

  new_ptr = (struct node*)malloc(sizeof(struct node));
  new_ptr->data = data;
  new_ptr->next = *head;

  *head = new_ptr;
}

/************************************************************
 pop - delete node at beginning of non-empty list and return its data
************************************************************/
int pop(struct node** head) {
  int val = 0;
  struct node* temp = NULL;

  if (*head != NULL) {
    val = (*head)->data;
    temp = (*head)->next;
    free(*head);
    *head = temp;
  }

  return(val);
}

/************************************************************
 appendNode - add new node at end of list
************************************************************/
void appendNode(struct node** head, int data) {
  struct node* current = NULL;
  struct node* previous = NULL;
  struct node* new_ptr = NULL;

  current = *head;
  previous = current;
  while (current != NULL) {
    previous = current;
    current = current->next;
  }

  new_ptr = (struct node*)malloc(sizeof(struct node));
  new_ptr->data = data;
  new_ptr->next = NULL;

  previous = new_ptr;

}

/************************************************************
 copyList - return new copy of list
************************************************************/
struct node* copyList(struct node** head) {
  struct node* copy = NULL;
  struct node* current = NULL;
  struct node* new_ptr = NULL;

  /* Copy current head to copy */
  current = *head;
  while (current != NULL) {
    appendNode(&copy, current->data);
    current = current->next;
  }

  return copy;
}


/************************************************************
 printList - print linked list as "List: < 2, 5, 6 >" (example)
************************************************************/
void printList(struct node** head) {
  struct node* current = NULL;

  printf("List: < ");

  current = *head;
  if (current == NULL)
    printf("none ");

  while (current != NULL) {
    printf("%d", current->data);
    current = current->next;
    if (current != NULL)
      printf(", ");
  }

  printf(" >\n");
}

void main() {
  int i;                      // index used for loops
  struct node *list_a;        // a new list
  struct node *list_a_copy;   // copy of list
  list_a = NULL;                // initialize empty list
  list_a_copy = NULL;           // initialize empy list


  // test push
  for (i = 0; i < 4; ++i)
    push(&list_a, i);

  // test length
  printf("Length of list = %d\n", length(&list_a));

  // test print head list
  printf("head:\n");
  printList(&list_a);

  // test append node
  for (i = 4; i < 8; ++i)
    appendNode(&list_a, i);

  // test print head list
  printf("head(append):\n");
  printList(&list_a);

  // make a copy of list
  list_a_copy = copyList(&list_a);

  // test pop head list
  for (i = 0; i < 4; ++i)
    printf("%d popped\n", pop(&list_a));

  // test print copy list
  printf("head copy:\n");
  printList(&list_a_copy);

  // test pop copy list
  for (i = 0; i < 4; ++i)
    printf("%d popped\n", pop(&list_a_copy));
}

【讨论】:

    【解决方案2】:

    从错误日志中,我注意到您多次使用不兼容的类型。如果您也发布您的 main.cpp 会有所帮助。

    p.s 就像 Mike G 说的,双指针实在是太过分了。您可以使用单个指针来实现链表。

    【讨论】:

      【解决方案3】:

      问题出在您的 pop() 函数中……看看。 你正在分配

      val=head->data;
      temp = head->next;
      

      你也正在释放头部然后重新分配。

      你应该这样做

      val=(*head)->data;
      temp=(*head)->next;
      

      在释放内存的同时...释放 temp 并将 head 重新分配给下一个。

      【讨论】:

        猜你喜欢
        • 2012-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-28
        • 2013-08-07
        相关资源
        最近更新 更多