0. Print list

mandatory

Write a function that prints all the elements of a list_t list.

  • Prototype: size_t print_list(const list_t *h);
  • Return: the number of nodes
  • Format: see example
  • If str is NULL, print [0] (nil)
  • You are allowed to use printf
julien@ubuntu:~/0x12. Singly linked lists$ cat 0-main.c
#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);
}
julien@ubuntu:~/0x12. Singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-print_list.c -o a
julien@ubuntu:~/0x12. Singly linked lists$ ./a 
[5] Hello
[5] World
-> 2 elements

[0] (nil)
[5] World
-> 2 elements
julien@ubuntu:~/0x12. Singly linked lists$ 

Repo:

  • GitHub repository: alx-low_level_programming
  • Directory: 0x12-singly_linked_lists
  • File: 0-print_list.c

 Done!HelpCheck your code

1. List length

mandatory

Write a function that returns the number of elements in a linked list_t list.

  • Prototype: size_t list_len(const list_t *h);
julien@ubuntu:~/0x12. Singly linked lists$ cat 1-main.c
#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 = list_len(head);
    printf("-> %lu elements\n", n);
    free(new->str);
    free(new);
    return (0);
}
julien@ubuntu:~/0x12. Singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-list_len.c -o b
julien@ubuntu:~/0x12. Singly linked lists$ ./b 
-> 2 elements
julien@ubuntu:~/0x12. Singly linked lists$ 

Repo:

  • GitHub repository: alx-low_level_programming
  • Directory: 0x12-singly_linked_lists
  • File: 1-list_len.c

 Done!HelpCheck your code

2. Add node

mandatory

Write a function that adds a new node at the beginning of a list_t list.

  • Prototype: list_t *add_node(list_t **head, const char *str);
  • Return: the address of the new element, or NULL if it failed
  • str needs to be duplicated
  • You are allowed to use strdup
julien@ubuntu:~/0x12. Singly linked lists$ cat 2-main.c
#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;

    head = NULL;
    add_node(&head, "Alexandro");
    add_node(&head, "Asaia");
    add_node(&head, "Augustin");
    add_node(&head, "Bennett");
    add_node(&head, "Bilal");
    add_node(&head, "Chandler");
    add_node(&head, "Damian");
    add_node(&head, "Daniel");
    add_node(&head, "Dora");
    add_node(&head, "Electra");
    add_node(&head, "Gloria");
    add_node(&head, "Joe");
    add_node(&head, "John");
    add_node(&head, "John");
    add_node(&head, "Josquin");
    add_node(&head, "Kris");
    add_node(&head, "Marine");
    add_node(&head, "Mason");
    add_node(&head, "Praylin");
    add_node(&head, "Rick");
    add_node(&head, "Rick");
    add_node(&head, "Rona");
    add_node(&head, "Siphan");
    add_node(&head, "Sravanthi");
    add_node(&head, "Steven");
    add_node(&head, "Tasneem");
    add_node(&head, "William");
    add_node(&head, "Zee");
    print_list(head);
    return (0);
}
julien@ubuntu:~/0x12. Singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-add_node.c 0-print_list.c -o c
julien@ubuntu:~/0x12. Singly linked lists$ ./c 
[3] Zee
[7] William
[7] Tasneem
[6] Steven
[9] Sravanthi
[6] Siphan
[4] Rona
[4] Rick
[4] Rick
[7] Praylin
[5] Mason
[6] Marine
[4] Kris
[7] Josquin
[4] John
[4] John
[3] Joe
[6] Gloria
[7] Electra
[4] Dora
[6] Daniel
[6] Damian
[8] Chandler
[5] Bilal
[7] Bennett
[8] Augustin
[5] Asaia
[9] Alexandro
julien@ubuntu:~/0x12. Singly linked lists$ 

Repo:

  • GitHub repository: alx-low_level_programming
  • Directory: 0x12-singly_linked_lists
  • File: 2-add_node.c

 Done!HelpCheck your code

3. Add node at the end

mandatory

Write a function that adds a new node at the end of a list_t list.

  • Prototype: list_t *add_node_end(list_t **head, const char *str);
  • Return: the address of the new element, or NULL if it failed
  • str needs to be duplicated
  • You are allowed to use strdup
julien@ubuntu:~/0x12. Singly linked lists$ cat 3-main.c
#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;

    head = NULL;
    add_node_end(&head, "Anne");
    add_node_end(&head, "Colton");
    add_node_end(&head, "Corbin");
    add_node_end(&head, "Daniel");
    add_node_end(&head, "Danton");
    add_node_end(&head, "David");
    add_node_end(&head, "Gary");
    add_node_end(&head, "Holden");
    add_node_end(&head, "Ian");
    add_node_end(&head, "Ian");
    add_node_end(&head, "Jay");
    add_node_end(&head, "Jennie");
    add_node_end(&head, "Jimmy");
    add_node_end(&head, "Justin");
    add_node_end(&head, "Kalson");
    add_node_end(&head, "Kina");
    add_node_end(&head, "Matthew");
    add_node_end(&head, "Max");
    add_node_end(&head, "Michael");
    add_node_end(&head, "Ntuj");
    add_node_end(&head, "Philip");
    add_node_end(&head, "Richard");
    add_node_end(&head, "Samantha");
    add_node_end(&head, "Stuart");
    add_node_end(&head, "Swati");
    add_node_end(&head, "Timothy");
    add_node_end(&head, "Victor");
    add_node_end(&head, "Walton");
    print_list(head);
    return (0);
}
julien@ubuntu:~/0x12. Singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c 3-add_node_end.c 0-print_list.c -o d
julien@ubuntu:~/0x12. Singly linked lists$ ./d 
[4] Anne
[6] Colton
[6] Corbin
[6] Daniel
[6] Danton
[5] David
[4] Gary
[6] Holden
[3] Ian
[3] Ian
[3] Jay
[6] Jennie
[5] Jimmy
[6] Justin
[6] Kalson
[4] Kina
[7] Matthew
[3] Max
[7] Michael
[4] Ntuj
[6] Philip
[7] Richard
[8] Samantha
[6] Stuart
[5] Swati
[7] Timothy
[6] Victor
[6] Walton
julien@ubuntu:~/0x12. Singly linked lists$ 

Repo:

  • GitHub repository: alx-low_level_programming
  • Directory: 0x12-singly_linked_lists
  • File: 3-add_node_end.c

 Done!HelpCheck your code

4. Free list

mandatory

Write a function that frees a list_t list.

  • Prototype: void free_list(list_t *head);
julien@ubuntu:~/0x12. Singly linked lists$ cat 4-main.c
#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;

    head = NULL;
    add_node_end(&head, "Bob");
    add_node_end(&head, "&");
    add_node_end(&head, "Kris");
    add_node_end(&head, "love");
    add_node_end(&head, "asm");
    print_list(head);
    free_list(head);
    head = NULL;
    return (0);
}
julien@ubuntu:~/0x12. Singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-main.c 4-free_list.c 3-add_node_end.c 0-print_list.c -o e
julien@ubuntu:~/0x12. Singly linked lists$ valgrind ./e
==3598== Memcheck, a memory error detector
==3598== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==3598== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==3598== Command: ./e
==3598== 
[6] Bob
[1] &
[3] Kris
[4] love
[3] asm
==3598== 
==3598== HEAP SUMMARY:
==3598==     in use at exit: 0 bytes in 0 blocks
==3598==   total heap usage: 11 allocs, 11 frees, 1,166 bytes allocated
==3598== 
==3598== All heap blocks were freed -- no leaks are possible
==3598== 
==3598== For counts of detected and suppressed errors, rerun with: -v
==3598== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
julien@ubuntu:~/0x12. Singly linked lists$ 

Repo:

  • GitHub repository: alx-low_level_programming
  • Directory: 0x12-singly_linked_lists
  • File: 4-free_list.c

 Done!HelpCheck your code

5. The Hare and the Tortoise

#advanced

Write a 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.

  • You are allowed to use the printf function
julien@ubuntu:~/0x12. Singly linked lists$ cat 100-main.c
#include <stdio.h>

/**
 * main - check the code
 *
 * Return: Always 0.
 */
int main(void)
{
    printf("(A tortoise, having pretty good sense of a hare's nature, challenges one to a race.)\n");
    return (0);
}
julien@ubuntu:~/$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 100-main.c 100-first.c -o first
julien@ubuntu:~/$ ./first 
You're beat! and yet, you must allow,
I bore my house upon my back!
(A tortoise, having pretty good sense of a hare's nature, challenges one to a race.)
julien@ubuntu:~/$ 

Repo:

  • GitHub repository: alx-low_level_programming
  • Directory: 0x12-singly_linked_lists
  • File: 100-first.c

 Done!HelpCheck your code

6. Real programmers can write assembly code in any language

#advanced

Write a 64-bit program in assembly that prints Hello, Holberton, followed by a new line.

  • You are only allowed to use the printf function
  • You are not allowed to use interrupts
  • Your program will be compiled using nasm and gcc:
julien@ubuntu:~/$ nasm -f elf64 101-hello_holberton.asm && gcc -no-pie -std=gnu89 101-hello_holberton.o -o hello
julien@ubuntu:~/$ ./hello 
Hello, Holberton
julien@ubuntu:~/$ 

Repo:

  • GitHub repository: alx-low_level_programming
  • Directory: 0x12-singly_linked_lists
  • File: 101-hello_holberton.asm

πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯

Solutions to the Projects

πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯

0-print_list.c

include “lists.h”

include

/**

  • print_list – prints all the elements of a list_t list
  • @h: pointer to struct
    *
  • Return: number of nodes
    */

size_t print_list(const list_t *h)
{
size_t nbr_nodes = 0;

while (h != NULL)
{
    if (h->str == NULL)
        printf("[%d] %s\n", 0, "(nil)");
    else
        printf("[%d] %s\n", h->len, h->str);
    h = h->next;
    nbr_nodes++;
}

return (nbr_nodes);

}

1-list_len.c

include “lists.h”

/**

  • list_len – returns then number of elements in a list
  • @h: stringly linked list
  • Return: number of elements in the list.
    */

size_t list_len(const list_t *h)
{
size_t nbr_nodes = 0;

while (h != NULL)
{
    h = h->next;
    nbr_nodes++;
}

return (nbr_nodes);

}

2-add_node.c

include “lists.h”

/**

  • add_node – adds a new node at the beginning
  • of a linked list
  • @head: head of the linked list
  • @str: string to store in the list
    *
  • Return: address of the head
    */

list_t *add_node(list_t **head, const char *str)
{
list_t *new;
size_t nbr_node;

new = malloc(sizeof(list_t));
if (new == NULL)
    return (NULL);

new->str = strdup(str);

for (nbr_node = 0; str[nbr_node]; nbr_node++)
    ;

new->len = nbr_node;
new->next = *head;
*head = new;

return (*head);

}

3-add_node_end.c

include “lists.h”

/**

  • add_node_end – adds a new node at the end
  • of a linked list
  • @head: head of the linked list
  • @str: string to store in the list
    *
  • Return: address of the head
    */

list_t *add_node_end(list_t **head, const char *str)
{
list_t *new, *temp;
size_t nbr_node;

new = malloc(sizeof(list_t));
if (new == NULL)
    return (NULL);

new->str = strdup(str);

for (nbr_node = 0; str[nbr_node]; nbr_node++)
    ;

new->len = nbr_node;
new->next = NULL;
temp = *head;

if (temp == NULL)
{
    *head = new;
}
else
{
    while (temp->next != NULL)
        temp = temp->next;
    temp->next = new;
}

return (*head);

}

===========

4-free_list.c

===========

include “lists.h”

/**

  • free_list – frees a list
  • @head: head of the linked list.
    *
  • Return: No return
    */

void free_list(list_t *head)
{
list_t *current;

while ((current = head) != NULL)
{
    head = head->next;
    free(current->str);
    free(current);
}

}

Advanced TasksπŸš€

include

/**bmain – function executed before main
*Return: No return
*/
void attribute ((constructor)) bmain()
{
printf(“You’re beat! and yet, you must allow”);
printf(“,\nI bore my house upon my back!\n”);
}

================

101-hello_holberton.asm

=================

SECTION .data

msg: db “Hello, Holberton”, 0
fmt: db “%s”, 10, 0

SECTION .text
extern printf
global main

main:
mov esi, msg
mov edi, fmt
mov eax, 0
call printf

mov eax, 0
ret