File tree 5 files changed +114
-2
lines changed
0x03-python-data_structures
5 files changed +114
-2
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ int check(listint_t *head, listint_t **current)
16
16
17
17
tail = check (head -> next , current );
18
18
19
- if (tail && (* current )-> data == head -> data )
19
+ if (tail && (* current )-> n == head -> n )
20
20
{
21
21
* current = (* current )-> next ;
22
22
return (1 );
@@ -36,5 +36,5 @@ int is_palindrome(listint_t **head)
36
36
if (* head == NULL || head == NULL )
37
37
return (1 );
38
38
39
- return (check (head , * head ));
39
+ return (check (* head , head ));
40
40
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
#ifndef LISTS_H
2
2
#define LISTS_H
3
3
4
+ #include <stdio.h>
5
+ #include <stdlib.h>
6
+
4
7
/**
5
8
* struct listint_s - singly linked list
6
9
* @n: integer
You can’t perform that action at this time.
0 commit comments