gpt4 book ai didi

c - 指针 链表 C 编程

转载 作者:行者123 更新时间:2023-11-30 17:43:42 26 4
gpt4 key购买 nike

我不明白为什么这不起作用......例如我有这个。

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

static int length(struct node* head) {
Does Stuff

};

void main() (
int i;
struct node* head;
i = length(head);
);

但是代码不想工作...我得到了错误的输出。我试图将指针发送到我的函数,以便它们可以访问我 malloc 的数据。我将在下面发布完整的代码:

#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 指针,而且我知道我已经很接近了。

干杯

最佳答案

我研究了函数push():

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

您分配head = new_ptr;的方式是错误的。只有这样做,head 才会在函数内部生效,head 不会指向您在调用push() 后分配的内存。所以你需要修复你的push()函数:

void push(struct node **head, int data) {
if ((*head) == null)
(*head) = (struct node*)malloc(sizeof(struct node));
(*head)->data = data;
(*head)->next = head;
}

关于c - 指针 链表 C 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20183949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com