C语言 链表插入和反向输出



我有这段代码,我正在处理它,在链表中添加一些字符串以反转列表。但是我遇到段故障错误。

segmentation fault: 11是我在编译器上看到的。

可能涉及内存分配问题,但这在此时并不重要。

还有一个警告,即字符常量对于其类型来说太长。我不确定这意味着什么。

如何解决这个问题?

#include <stdio.h> 
#include <stdlib.h> 
struct Node { 
char *data[100]; 
struct Node *next; 
}; 
static void reverse(struct Node **head_ref) { 
struct Node *prev = NULL; 
struct Node *current = *head_ref; 
struct Node *next = NULL; 
while (current != NULL) { 
// Store next 
next = current->next; 
// Reverse current node's pointer 
current->next = prev; 
// Move pointers one position ahead. 
prev = current; 
current = next; 
} 
*head_ref = prev; 
} 
void push(struct Node **head_ref, char new_data) { 
struct Node *new_node = (struct Node *)malloc(sizeof(struct Node)); 
new_node->data = new_data; 
new_node->next = (*head_ref); 
(*head_ref) = new_node; 
} 
void printList(struct Node *head) { 
struct Node *temp = head; 
while (temp != NULL) { 
printf("%s ", temp->data); 
temp = temp->next; 
}
}
int main() { 
/* Start with the empty list */
struct Node *head = NULL; 
push(&head, "hi"); 
push(&head, "hello");
push(&head, "mello"); 
printf("Given linked listn"); 
printList(head); 
reverse(&head); 
printf("nReversed Linked list n"); 
printList(head); 
getchar(); 
} 

在您的推送函数中,当您打算传递char *时,您似乎正在传递char

此外,您正在将new_data分配给同一函数中的数组。

如果将data变量更改为仅键入char *,则可以进行此分配。这意味着您需要根据之前每个字符串的大小分配必要的内存,但正如您提到的,现在这并不重要。

这是包含这 2 个更改的代码,并经过测试可以正常工作;

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

struct Node {
char * data;
struct Node* next;
};

static void reverse(struct Node** head_ref)
{
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next = NULL;
while (current != NULL) {
// Store next
next = current->next;
// Reverse current node's pointer
current->next = prev;
// Move pointers one position ahead.
prev = current;
current = next;
}
*head_ref = prev;
}

void push(struct Node** head_ref, char * new_data)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

void printList(struct Node* head)
{
struct Node* temp = head;
while (temp != NULL) {
printf("%s ", temp->data);
temp = temp->next;
}
}

int main(){
/* Start with the empty list */
struct Node* head = NULL;
push(&head, "hi");
push(&head, "hello");
push(&head, "mello");

printf("Given linked listn");
printList(head);
reverse(&head);
printf("nReversed Linked list n");
printList(head);
getchar();
}

希望这有帮助!

发布的代码无法编译!

编译时,请始终启用警告,然后修复这些警告。

下面显示了如何使用gcc和结果编译代码。

gcc -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled1.c" -o "untitled1.o" 
untitled1.c: In function ‘push’:
untitled1.c:29:20: error: assignment to expression with array type
new_node->data = new_data;
^
untitled1.c: In function ‘printList’:
untitled1.c:37:18: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat=]
printf("%s ", temp->data);
~^    ~~~~
untitled1.c: In function ‘main’:
untitled1.c:46:17: warning: passing argument 2 of ‘push’ makes integer from pointer without a cast [-Wint-conversion]
push(&head, "hi");
^~~~
untitled1.c:27:6: note: expected ‘char’ but argument is of type ‘char *’
void push(struct Node **head_ref, char new_data) {
^~~~
untitled1.c:47:17: warning: passing argument 2 of ‘push’ makes integer from pointer without a cast [-Wint-conversion]
push(&head, "hello");
^~~~~~~
untitled1.c:27:6: note: expected ‘char’ but argument is of type ‘char *’
void push(struct Node **head_ref, char new_data) {
^~~~
untitled1.c:48:17: warning: passing argument 2 of ‘push’ makes integer from pointer without a cast [-Wint-conversion]
push(&head, "mello");
^~~~~~~
untitled1.c:27:6: note: expected ‘char’ but argument is of type ‘char *’
void push(struct Node **head_ref, char new_data) {
^~~~
Compilation failed.

最新更新