C 指针链表

C 指针链表,c,pointers,linked-list,C,Pointers,Linked List,我做对了吗?我这里有个奇怪的错误 linked.c: In function ‘push’: linked.c:50:19: warning: assignment from incompatible pointer type [enabled by default] linked.c: In function ‘main’: linked.c:146:9: error: incompatible type for argument 1 of ‘push’ linked.c:45:6: note

我做对了吗?我这里有个奇怪的错误

linked.c: In function ‘push’: linked.c:50:19: warning: assignment from incompatible pointer type [enabled by default] linked.c: In function ‘main’: linked.c:146:9: error: incompatible type for argument 1 of ‘push’ linked.c:45:6: note: expected ‘struct node **’ but argument is of type ‘struct node’ ~/swen250/CLinkedList$ gcc -o linked linked.c linked.c: In function ‘push’: linked.c:50:19: warning: assignment from incompatible pointer type [enabled by default] ~/swen250/CLinkedList$ gcc -o linked linked.c linked.c: In function ‘pop’: linked.c:63:19: error: request for member ‘data’ in something not a structure or union linked.c:64:20: error: request for member ‘next’ in something not a structure or union linked.c: In function ‘copyList’: linked.c:106:9: warning: passing argument 1 of ‘appendNode’ from incompatible pointer type [enabled by default] linked.c:75:6: note: expected ‘struct node **’ but argument is of type ‘struct node *’ c:在函数“push”中: linked.c:50:19:警告:不兼容指针类型的赋值[默认启用] c:在函数“main”中: linked.c:146:9:错误:“推送”的参数1的类型不兼容 linked.c:45:6:注意:应为“struct node**”,但参数的类型为“struct node” ~/swen250/CLinkedList$gcc-o linked.c c:在函数“push”中: linked.c:50:19:警告:不兼容指针类型的赋值[默认启用] ~/swen250/CLinkedList$gcc-o linked.c c:在函数“pop”中: linked.c:63:19:错误:请求非结构或联合中的成员“数据” linked.c:64:20:错误:请求非结构或联合中的成员“next” c:在函数“copyList”中: linked.c:106:9:警告:从不兼容的指针类型传递'appendNode'的参数1[默认情况下已启用] linked.c:75:6:注意:应为“struct node**”,但参数的类型为“struct node*”
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
};
静态整数长度(结构节点**头部);
静态void push(结构节点**头部,int数据);
静态int-pop(结构节点**head);
静态节点(结构节点**head,int数据);
静态结构节点*复制列表(结构节点**头);
静态无效打印列表(结构节点**头);
/************************************************************
length—返回列表的长度
************************************************************/
整数长度(结构节点**头){
整数计数=0;
结构节点*current=NULL;
电流=*水头;
while(当前!=NULL){
当前=当前->下一步;
++计数;
}
返回计数;
}
/************************************************************
推送-在列表开头添加新节点
************************************************************/
无效推送(结构节点**头部,整数数据){
结构节点*new_ptr=NULL;
new_ptr=(结构节点*)malloc(sizeof(结构节点));
新建->数据=数据;
新建->下一步=*头部;
*压头=新压头;
}
/************************************************************
pop-删除非空列表开头的节点并返回其数据
************************************************************/
int pop(结构节点**头){
int-val=0;
结构节点*temp=NULL;
如果(*head!=NULL){
val=头部->数据;
温度=头部->下一步;
自由(头);
*压头=温度;
}
返回(val);
}
/************************************************************
appendNode-在列表末尾添加新节点
************************************************************/
void appendNode(结构节点**head,int数据){
结构节点*current=NULL;
结构节点*previous=NULL;
结构节点*new_ptr=NULL;
电流=*水头;
先前=当前;
while(当前!=NULL){
先前=当前;
当前=当前->下一步;
}
new_ptr=(结构节点*)malloc(sizeof(结构节点));
新建->数据=数据;
新建\u ptr->next=NULL;
先前=新的ptr;
}
/************************************************************
copyList-返回列表的新副本
************************************************************/
结构节点*复制列表(结构节点**头){
结构节点*copy=NULL;
结构节点*current=NULL;
结构节点*new_ptr=NULL;
/*复制当前磁头以进行复制*/
电流=*水头;
while(当前!=NULL){
追加节点(复制,当前->数据);
当前=当前->下一步;
}
返回副本;
}
/************************************************************
打印列表-将链接列表打印为“列表:<2,5,6>”(示例)
************************************************************/
无效打印列表(结构节点**头){
结构节点*current=NULL;
printf(“列表:<”);
电流=*水头;
如果(当前==NULL)
printf(“无”);
while(当前!=NULL){
printf(“%d”,当前->数据);
当前=当前->下一步;
如果(当前!=NULL)
printf(“,”);
}
printf(“>\n”);
}
void main(){
int i;//用于循环的索引
struct node*list_a;//一个新列表
struct node*list\u a\u copy;//列表的副本
list_a=NULL;//初始化空列表
list\u a\u copy=NULL;//初始化empy list
//试推
对于(i=0;i<4;++i)
推送(列表a和i);
//测试长度
printf(“列表长度=%d\n”,长度(&list_a));
//测试打印头列表
printf(“头:\n”);
打印列表(&list_a);
//测试附加节点
对于(i=4;i<8;++i)
附录节点(和列表_a,i);
//测试打印头列表
printf(“头(附加):\n”);
打印列表(&list_a);
//复印一份清单
list_a_copy=复制列表(&list_a);
//测试pop标题列表
对于(i=0;i<4;++i)
printf(“%d弹出\n”,弹出(&list_a));
//测试打印副本列表
printf(“头拷贝:\n”);
打印列表(列表和副本);
//测试pop副本列表
对于(i=0;i<4;++i)
printf(“%d弹出\n”,弹出(&列出副本));
}

从错误日志中,我注意到您的不兼容类型是时间的倍数。如果您也发布main.cpp,这会有所帮助

p、 正如迈克G所说,双指针真的太过分了。您可以使用单个指针实现链表。

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

val=head->data;
temp = head->next;
此外,您正在释放头部,然后重新分配

你应该这样做

val=(*head)->data;
temp=(*head)->next;
在释放内存的同时…释放temp并将磁头重新分配到下一个。

问题在于双指针的使用方式

以下是完整的工作代码: 我对双指针的使用方式做了一些改变。 您可以在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));
}
#i
#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));
}