-
Notifications
You must be signed in to change notification settings - Fork 1
/
linked_lists.c
114 lines (100 loc) · 1.66 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "shell.h"
/**
* add_sep_node_end - adds a separator node
* to the end of the list
*
* @head: head of the linked list.
* @sep: separator to add.
* Return: NULL on failure and the address
* of the head on success.
*/
sep_t *add_sep_node_end(sep_t **head, char sep)
{
sep_t *new, *temp;
new = malloc(sizeof(sep_t));
if (new == NULL)
{
free(new);
return (NULL);
}
new->sep = sep;
new->next = NULL;
if (!(*head))
*head = new;
else
{
temp = *head;
while (temp->next != NULL)
temp = temp->next;
temp->next = new;
}
return (*head);
}
/**
* free_sep_list - frees a sep_list
* @head: head of the linked list.
*/
void free_sep_list(sep_t **head)
{
sep_t *temp;
sep_t *curr;
if (head != NULL)
{
curr = *head;
while ((temp = curr) != NULL)
{
curr = curr->next;
free(temp);
}
*head = NULL;
}
}
/**
* add_line_node_end - adds a command line at the end
* of a line_list.
* @head: head of the linked list.
* @line: command line.
* Return: address of the head.
*/
line_t *add_line_node_end(line_t **head, char *line)
{
line_t *new, *temp;
new = malloc(sizeof(line_t));
if (new == NULL)
{
free(new);
return (NULL);
}
new->line = line;
new->next = NULL;
if (!(*head))
*head = new;
else
{
temp = *head;
while (temp->next != NULL)
temp = temp->next;
temp->next = new;
}
return (*head);
}
/**
* free_line_list - frees a line_list
* @head: head of the linked list.
* Return: no return.
*/
void free_line_list(line_t **head)
{
line_t *temp;
line_t *curr;
if (head != NULL)
{
curr = *head;
while ((temp = curr) != NULL)
{
curr = curr->next;
free(temp);
}
*head = NULL;
}
}