Skip to content

Commit cab8ce5

Browse files
committed
0x12-singly_linked_lists
1 parent c16db67 commit cab8ce5

9 files changed

+222
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdio.h>
2+
#include "lists.h"
3+
4+
/**
5+
* * print_list - prints all the elements of a linked list
6+
* * @h: pointer to the list_t list to print
7+
* *
8+
* * Return: the number of nodes printed
9+
* */
10+
size_t print_list(const list_t *h)
11+
{
12+
size_t s = 0;
13+
14+
while (h)
15+
{
16+
if (!h->str)
17+
printf("[0] (nil)\n");
18+
else
19+
printf("[%u] %s\n", h->len, h->str);
20+
h = h->next;
21+
s++;
22+
}
23+
24+
return (s);
25+
}

0x12-singly_linked_lists/1-list_len.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <stdlib.h>
2+
#include "lists.h"
3+
4+
/**
5+
* * list_len - returns the number of elements in a linked list
6+
* * @h: pointer to the list_t list
7+
* *
8+
* * Return: number of elements in h
9+
* */
10+
size_t list_len(const list_t *h)
11+
{
12+
size_t n = 0;
13+
14+
while (h)
15+
{
16+
n++;
17+
h = h->next;
18+
}
19+
return (n);
20+
}

0x12-singly_linked_lists/100-first.c

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
nclude <stdio.h>
2+
3+
void first(void) __attribute__ ((constructor));
4+
5+
/**
6+
* * first - prints a sentence before the main
7+
* * function is executed
8+
* */
9+
void first(void)
10+
{
11+
printf("You're beat! and yet, you must allow,\n");
12+
printf("I bore my house upon my back!\n");
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
global main
2+
extern printf
3+
main:
4+
mov edi, format
5+
xor eax, eax
6+
call printf
7+
mov eax, 0
8+
ret
9+
format: db `Hello, Holberton\n`,0

0x12-singly_linked_lists/2-add_node.c

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include "lists.h"
4+
5+
/**
6+
* * add_node - adds a new node at the beginning of a linked list
7+
* * @head: double pointer to the list_t list
8+
* * @str: new string to add in the node
9+
* *
10+
* * Return: the address of the new element, or NULL if it fails
11+
* */
12+
list_t *add_node(list_t **head, const char *str)
13+
{
14+
list_t *new;
15+
unsigned int len = 0;
16+
17+
while (str[len])
18+
len++;
19+
20+
new = malloc(sizeof(list_t));
21+
if (!new)
22+
return (NULL);
23+
24+
new->str = strdup(str);
25+
new->len = len;
26+
new->next = (*head);
27+
(*head) = new;
28+
29+
return (*head);
30+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include "lists.h"
4+
5+
/**
6+
* * add_node_end - adds a new node at the end of a linked list
7+
* * @head: double pointer to the list_t list
8+
* * @str: string to put in the new node
9+
* *
10+
* * Return: address of the new element, or NULL if it failed
11+
* */
12+
list_t *add_node_end(list_t **head, const char *str)
13+
{
14+
list_t *new;
15+
list_t *temp = *head;
16+
unsigned int len = 0;
17+
18+
while (str[len])
19+
len++;
20+
21+
new = malloc(sizeof(list_t));
22+
if (!new)
23+
return (NULL);
24+
25+
new->str = strdup(str);
26+
new->len = len;
27+
new->next = NULL;
28+
29+
if (*head == NULL)
30+
{
31+
*head = new;
32+
return (new);
33+
}
34+
35+
while (temp->next)
36+
temp = temp->next;
37+
38+
temp->next = new;
39+
40+
return (new);
41+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stdlib.h>
2+
#include "lists.h"
3+
4+
/**
5+
* * free_list - frees a linked list
6+
* * @head: list_t list to be freed
7+
* */
8+
void free_list(list_t *head)
9+
{
10+
list_t *temp;
11+
12+
while (head)
13+
{
14+
temp = head->next;
15+
free(head->str);
16+
free(head);
17+
head = temp;
18+
}
19+
}

0x12-singly_linked_lists/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
- Singly linked lists
2+
3+
Tests ✔️
4+
* Tests: This Directory is for tests purposes.
5+
6+
Header File 📁
7+
* lists.h: Header file containing definitions and prototypes for all types and functions written for the project.
8+
Type/File Definition/Prototype
9+
* struct list_s • char *str
10+
* unsigned int len
11+
* struct list_s *next
12+
typedef list_t struct list_s
13+
0-print_list.c size_t print_list(const list_t *h);
14+
1-list_len.c size_t list_len(const list_t *h);
15+
2-add_node.c list_t *add_node(list_t **head, const char *str);
16+
3-add_node_end.c list_t *add_node_end(list_t **head, const char *str);
17+
4-free_list.c void free_list(list_t *head)
18+
19+
Tasks 📃
20+
* 0. Print list
21+
* 0-print_list.c: C function that prints all the elements of a list_t list.
22+
* If str is NULL, the function prints [0] (nil).
23+
* 1. List length
24+
* 1-list_len.c: C function that returns the number of elements in a linked list_t list.
25+
* 2. Add node
26+
* 2-add_node.c: C function that adds a new node at the beginning a of a list_t list.
27+
* If the function fails - returns NULL.
28+
* Otherwise - returns the address of the new element.
29+
* 3. Add node at the end
30+
* 3-add_node_end.c: C function that adds a new node at the end of a linked list_t list.
31+
* If the function fails - returns NULL.
32+
* Otherwise - returns the address of the new element.
33+
* 4. Free list
34+
* 4-free_list.c: C function that frees a list_t list.
35+
* 5. The Hare and the Tortoise
36+
* 100-first.c: C function that prints You're beat! and yet, you must allow,\nI bore my house upon my back!\n before the main function is executed.
37+
* 6. Real programmers can write assembly code in any language
38+
* 101-hello_holberton.asm: 64-but assembly program that prints Hello, Holberton followed by a new line using only the printf function witout interrupts.
39+

0x12-singly_linked_lists/lists.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef LISTS_H
2+
#define LISTS_H
3+
4+
/**
5+
* * struct list_s - singly linked list
6+
* * @str: string - (malloc'ed string)
7+
* * @len: length of the string
8+
* * @next: points to the next node
9+
* *
10+
* * Description: singly linked list node structure
11+
* * for Holberton project
12+
* */
13+
typedef struct list_s
14+
{
15+
char *str;
16+
unsigned int len;
17+
struct list_s *next;
18+
} list_t;
19+
20+
size_t print_list(const list_t *h);
21+
size_t list_len(const list_t *h);
22+
list_t *add_node(list_t **head, const char *str);
23+
list_t *add_node_end(list_t **head, const char *str);
24+
void free_list(list_t *head);
25+
26+
#endif

0 commit comments

Comments
 (0)