Skip to content

Commit ed546b3

Browse files
committed
Fix 13-is_palindrome 2
1 parent dfea217 commit ed546b3

File tree

5 files changed

+114
-2
lines changed

5 files changed

+114
-2
lines changed

0x03-python-data_structures/13-is_palindrome.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ int check(listint_t *head, listint_t **current)
1616

1717
tail = check(head->next, current);
1818

19-
if (tail && (*current)->data == head->data)
19+
if (tail && (*current)->n == head->n)
2020
{
2121
*current = (*current)->next;
2222
return (1);
@@ -36,5 +36,5 @@ int is_palindrome(listint_t **head)
3636
if (*head == NULL || head == NULL)
3737
return (1);
3838

39-
return (check(head, *head));
39+
return (check(*head, head));
4040
}

0x03-python-data_structures/13-main.c

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include "lists.h"
4+
5+
/**
6+
* main - check the code for
7+
*
8+
* Return: Always 0.
9+
*/
10+
int main(void)
11+
{
12+
listint_t *head;
13+
14+
head = NULL;
15+
add_nodeint_end(&head, 1);
16+
add_nodeint_end(&head, 17);
17+
add_nodeint_end(&head, 972);
18+
add_nodeint_end(&head, 50);
19+
add_nodeint_end(&head, 98);
20+
add_nodeint_end(&head, 98);
21+
add_nodeint_end(&head, 50);
22+
add_nodeint_end(&head, 972);
23+
add_nodeint_end(&head, 17);
24+
add_nodeint_end(&head, 1);
25+
print_listint(head);
26+
27+
if (is_palindrome(&head) == 1)
28+
printf("Linked list is a palindrome\n");
29+
else
30+
printf("Linked list is not a palindrome\n");
31+
32+
free_listint(head);
33+
34+
return (0);
35+
}
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include "lists.h"
4+
5+
/**
6+
* print_listint - prints all elements of a listint_t list
7+
* @h: pointer to head of list
8+
* Return: number of nodes
9+
*/
10+
size_t print_listint(const listint_t *h)
11+
{
12+
const listint_t *current;
13+
unsigned int n; /* number of nodes */
14+
15+
current = h;
16+
n = 0;
17+
while (current != NULL)
18+
{
19+
printf("%i\n", current->n);
20+
current = current->next;
21+
n++;
22+
}
23+
24+
return (n);
25+
}
26+
27+
/**
28+
* add_nodeint_end - adds a new node at the end of a listint_t list
29+
* @head: pointer to pointer of first node of listint_t list
30+
* @n: integer to be included in new node
31+
* Return: address of the new element or NULL if it fails
32+
*/
33+
listint_t *add_nodeint_end(listint_t **head, const int n)
34+
{
35+
listint_t *new;
36+
listint_t *current;
37+
38+
current = *head;
39+
40+
new = malloc(sizeof(listint_t));
41+
if (new == NULL)
42+
return (NULL);
43+
44+
new->n = n;
45+
new->next = NULL;
46+
47+
if (*head == NULL)
48+
*head = new;
49+
else
50+
{
51+
while (current->next != NULL)
52+
current = current->next;
53+
current->next = new;
54+
}
55+
56+
return (new);
57+
}
58+
59+
/**
60+
* free_listint - frees a listint_t list
61+
* @head: pointer to list to be freed
62+
* Return: void
63+
*/
64+
void free_listint(listint_t *head)
65+
{
66+
listint_t *current;
67+
68+
while (head != NULL)
69+
{
70+
current = head;
71+
head = head->next;
72+
free(current);
73+
}
74+
}

0x03-python-data_structures/lists.h

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#ifndef LISTS_H
22
#define LISTS_H
33

4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
47
/**
58
* struct listint_s - singly linked list
69
* @n: integer
16.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)