【问题标题】:Checking and Returning 1 or 0 if Linked List is Palindrome?如果链表是回文,检查并返回 1 或 0?
【发布时间】:2020-08-29 17:41:58
【问题描述】:

我想用 C 编写一个函数来检查单向链表是否是回文。

我按以下顺序分隔了我的代码: 这是 lists.h 文件:

#ifndef LISTS_H
#define LISTS_H
#include <stdio.h>
#include <stdlib.h>
/**
 * struct listint_s - singly linked list
 * @n: integer
 * @next: points to the next node
 *
 * Description: singly linked list node structure
 * for Holberton project
 */
typedef struct listint_s {
    int n;
    struct listint_s *next;
} listint_t;

size_t print_listint(const listint_t *h);
listint_t *add_nodeint_end(listint_t **head, const int n);
void free_listint(listint_t *head);

int is_palindrome(listint_t **head);

listint_t *reverse(listint_t *head);

#endif /* LISTS_H */

0-main.c 文件:

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

/**
 * main - check the code for Holberton School students.
 *
 * Return: Always 0.
 */
int main(void)
{
    listint_t *head;

    head = NULL;
    add_nodeint_end(&head, 1);
    add_nodeint_end(&head, 17);
    add_nodeint_end(&head, 972);
    add_nodeint_end(&head, 50);
    add_nodeint_end(&head, 97);
    add_nodeint_end(&head, 98);
    add_nodeint_end(&head, 50);
    add_nodeint_end(&head, 972);
    add_nodeint_end(&head, 17);
    add_nodeint_end(&head, 1);
    print_listint(head);

    if (is_palindrome(&head) == 1)
        printf("Linked list is a palindrome\n");
    else
        printf("Linked list is not a palindrome\n");

    free_listint(head);

    return (0);
}

linked_lists.c 文件:

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

/**
 * print_listint - prints all elements of a listint_t list
 * @h: pointer to head of list
 * Return: number of nodes
 */
size_t print_listint(const listint_t *h)
{
    const listint_t *current;
    unsigned int n; /* number of nodes */

    current = h;
    n = 0;
    while (current != NULL)
    {
        printf("%i\n", current->n);
        current = current->next;
        n++;
    }

    return (n);
}

/**
 * add_nodeint_end - adds a new node at the end of a listint_t list
 * @head: pointer to pointer of first node of listint_t list
 * @n: integer to be included in new node
 * Return: address of the new element or NULL if it fails
 */
listint_t *add_nodeint_end(listint_t **head, const int n)
{
    listint_t *new;
    listint_t *current;

    current = *head;

    new = malloc(sizeof(listint_t));
    if (new == NULL)
        return (NULL);

    new->n = n;
    new->next = NULL;

    if (*head == NULL)
        *head = new;
    else
    {
        while (current->next != NULL)
            current = current->next;
        current->next = new;
    }

    return (new);
}

/**
 * free_listint - frees a listint_t list
 * @head: pointer to list to be freed
 * Return: void
 */
void free_listint(listint_t *head)
{
    listint_t *current;

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

0-is_palindrome.c文件(有问题的文件):

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

/**
 * is_palindrome - check the code for Holberton School students.
 * @head: the head of the linked list
 * Return: Always 0.
 */
int is_palindrome(listint_t **head)
{
    if (head == NULL) {
        return (1);
    }
    listint_t *mainnode;
    listint_t *reversedlist;
    mainnode = *head;
    reversedlist = *head;
    reversedlist = reverse(reversedlist);
    while (reversedlist != NULL)
    {
        if (mainnode->n != reversedlist->n)
        {
            return (0);
        }
        reversedlist = reversedlist->next;
        mainnode = mainnode->next;
    }
    return (1);
}

/**
 * reverse - check the code for Holberton School students.
 * @head: the head of the linked list
 * Return: Always 0.
 */
listint_t *reverse(listint_t *head)
{
    listint_t *prev = NULL;
    listint_t *current = head; 
    listint_t *next = NULL;

    while (current != NULL)
    {
        next = current->next; 
        current->next = prev; 
        prev = current; 
        current = next; 
    }

    return (prev); 
}

编译命令:

gcc -Wall -Werror -Wextra -pedantic 0-main.c linked_lists.c 0-is_palindrome.c -o palindrome

我遇到了分段错误,在我调整它以开始获得分段错误之前,即使链接列表不是回文,它也总是返回 1。 我只想修改 0-is_palindrome.c 文件,另一个应该保持不变,我的想法是列表被反转并在返回 1 或 0 之前与原始列表进行比较,就像你可以在代码中看到当反向列表的值与原始列表不同时它会中断。 我可以做些什么来解决这个问题?

【问题讨论】:

  • 使用调试符号编译您的应用程序并使用 gdb 调试器定位错误源。
  • listint_t *reverse(listint_t *head) 这个函数是做什么的,准确地说?是你自己写的吗?
  • 是的,我正在尝试反转第二个列表,我认为它实际上并没有反转它,这就是我理解它的方式

标签: c palindrome


【解决方案1】:

您的实现不起作用,因为reverse 没有构造新列表,而是修改了参数列表。

另外,is_palindrome没有理由修改列表,也没有修改它的初始节点,所以原型应该是:

int is_palindrome(const listint_t *head);

对于蛮力方法,您可以计算列表的长度,并在 i &lt; length / 2 循环中比较偏移量 ilength - i - 1 处的元素。如果所有比较都成功,则该列表是一个回文,您可以在一个比较失败时立即返回0

【讨论】:

    猜你喜欢
    • 2021-11-13
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 2022-06-30
    相关资源
    最近更新 更多