-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_lists.c
74 lines (63 loc) · 1.39 KB
/
linked_lists.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#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);
}
}