Skip to content

Commit

Permalink
skip
Browse files Browse the repository at this point in the history
  • Loading branch information
SHEFOO10 committed Mar 31, 2023
2 parents 847c962 + ed77a97 commit 3eb9dd1
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 2 deletions.
40 changes: 40 additions & 0 deletions 0x12-singly_linked_lists/0-main.c
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);
}
8 changes: 8 additions & 0 deletions 0x12-singly_linked_lists/0-print_list.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#include <stdio.h>
#include "lists.h"

/**
<<<<<<< HEAD
* print_list - print singly linked list
*
* @h: linked list
*
* Return: size of list
=======
* print_list - prints all the elements of a linked list
* @h: pointer to the list_t list to print
*
* Return: the number of nodes printed
>>>>>>> ed77a97053f3d469ec8736233f566c1560b6263a
*/
size_t print_list(const list_t *h)
{
Expand Down
20 changes: 20 additions & 0 deletions 0x12-singly_linked_lists/1-list_len.c
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);
}
13 changes: 13 additions & 0 deletions 0x12-singly_linked_lists/100-first.c
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");
}
9 changes: 9 additions & 0 deletions 0x12-singly_linked_lists/101-hello_holberton.asm
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
30 changes: 30 additions & 0 deletions 0x12-singly_linked_lists/2-add_node.c
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);
}
41 changes: 41 additions & 0 deletions 0x12-singly_linked_lists/3-add_node_end.c
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);
}
19 changes: 19 additions & 0 deletions 0x12-singly_linked_lists/4-free_list.c
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;
}
}
16 changes: 15 additions & 1 deletion 0x12-singly_linked_lists/README.md
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 added 0x12-singly_linked_lists/a.exe
Binary file not shown.
9 changes: 8 additions & 1 deletion 0x12-singly_linked_lists/lists.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef LISTS_H
#define LISTS_H
<<<<<<< HEAD
#include <stdlib.h>
=======
>>>>>>> ed77a97053f3d469ec8736233f566c1560b6263a

/**
* struct list_s - singly linked list
Expand All @@ -9,6 +12,7 @@
* @next: points to the next node
*
* Description: singly linked list node structure
* for Holberton project
*/
typedef struct list_s
{
Expand All @@ -17,7 +21,10 @@ typedef struct list_s
struct list_s *next;
} list_t;


size_t print_list(const list_t *h);
size_t list_len(const list_t *h);
list_t *add_node(list_t **head, const char *str);
list_t *add_node_end(list_t **head, const char *str);
void free_list(list_t *head);

#endif

0 comments on commit 3eb9dd1

Please sign in to comment.