-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
203 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <stdio.h> | ||
#include "lists.h" | ||
|
||
/** | ||
* main - check the code | ||
* | ||
* Return: Always 0. | ||
*/ | ||
int main(void) | ||
{ | ||
list_t *head; | ||
list_t *new; | ||
list_t hello = {"World", 5, NULL}; | ||
size_t n; | ||
|
||
head = &hello; | ||
new = malloc(sizeof(list_t)); | ||
if (new == NULL) | ||
{ | ||
printf("Error\n"); | ||
return (1); | ||
} | ||
new->str = strdup("Hello"); | ||
new->len = 5; | ||
new->next = head; | ||
head = new; | ||
n = print_list(head); | ||
printf("-> %lu elements\n", n); | ||
|
||
printf("\n"); | ||
free(new->str); | ||
new->str = NULL; | ||
n = print_list(head); | ||
printf("-> %lu elements\n", n); | ||
|
||
free(new); | ||
return (0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <stdlib.h> | ||
#include "lists.h" | ||
|
||
/** | ||
* list_len - returns the number of elements in a linked list | ||
* @h: pointer to the list_t list | ||
* | ||
* Return: number of elements in h | ||
*/ | ||
size_t list_len(const list_t *h) | ||
{ | ||
size_t n = 0; | ||
|
||
while (h) | ||
{ | ||
n++; | ||
h = h->next; | ||
} | ||
return (n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <stdio.h> | ||
|
||
void first(void) __attribute__ ((constructor)); | ||
|
||
/** | ||
* first - prints a sentence before the main | ||
* function is executed | ||
*/ | ||
void first(void) | ||
{ | ||
printf("You're beat! and yet, you must allow,\n"); | ||
printf("I bore my house upon my back!\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
global main | ||
extern printf | ||
main: | ||
mov edi, format | ||
xor eax, eax | ||
call printf | ||
mov eax, 0 | ||
ret | ||
format: db `Hello, Holberton\n`,0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "lists.h" | ||
|
||
/** | ||
* add_node - adds a new node at the beginning of a linked list | ||
* @head: double pointer to the list_t list | ||
* @str: new string to add in the node | ||
* | ||
* Return: the address of the new element, or NULL if it fails | ||
*/ | ||
list_t *add_node(list_t **head, const char *str) | ||
{ | ||
list_t *new; | ||
unsigned int len = 0; | ||
|
||
while (str[len]) | ||
len++; | ||
|
||
new = malloc(sizeof(list_t)); | ||
if (!new) | ||
return (NULL); | ||
|
||
new->str = strdup(str); | ||
new->len = len; | ||
new->next = (*head); | ||
(*head) = new; | ||
|
||
return (*head); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "lists.h" | ||
|
||
/** | ||
* add_node_end - adds a new node at the end of a linked list | ||
* @head: double pointer to the list_t list | ||
* @str: string to put in the new node | ||
* | ||
* Return: address of the new element, or NULL if it failed | ||
*/ | ||
list_t *add_node_end(list_t **head, const char *str) | ||
{ | ||
list_t *new; | ||
list_t *temp = *head; | ||
unsigned int len = 0; | ||
|
||
while (str[len]) | ||
len++; | ||
|
||
new = malloc(sizeof(list_t)); | ||
if (!new) | ||
return (NULL); | ||
|
||
new->str = strdup(str); | ||
new->len = len; | ||
new->next = NULL; | ||
|
||
if (*head == NULL) | ||
{ | ||
*head = new; | ||
return (new); | ||
} | ||
|
||
while (temp->next) | ||
temp = temp->next; | ||
|
||
temp->next = new; | ||
|
||
return (new); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <stdlib.h> | ||
#include "lists.h" | ||
|
||
/** | ||
* free_list - frees a linked list | ||
* @head: list_t list to be freed | ||
*/ | ||
void free_list(list_t *head) | ||
{ | ||
list_t *temp; | ||
|
||
while (head) | ||
{ | ||
temp = head->next; | ||
free(head->str); | ||
free(head); | ||
head = temp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,15 @@ | ||
Linked list | ||
File 0-print_list.c is a function that prints all the elements of a list_t list. | ||
|
||
File 1-list_len.c is a a function that returns the number of elements in a linked list_t list. | ||
|
||
File 2-add_node.c is a function that adds a new node at the beginning of a list_t list. | ||
|
||
File 3-add_node_end.c is a function that adds a new node at the end of a list_t list. | ||
|
||
File 4-free_list.c is a function that frees a list_t list. | ||
|
||
File 100-first.c is a function that prints a sentence before the main function is executed. | ||
|
||
File 101-hello_holberton.asm is 64-bit program in assembly that prints "Hello, Holberton", followed by a new line. | ||
|
||
File lists.h is the header file containing the definition of the list_t struct and all the prototypes of the above functions. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters