Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
805 B
1
Indexable
Never
#include <stdio.h>

typedef struct Node
{
    struct Node *next;
    int data;
} node;

void print(node *root);
void add(node *root, int newData);
// void del(node n);
// void delRoot(node n);

int main()
{
    node root;
    root.data = 15;
    root.next = NULL;
    print(&root);
    add(&root, 20);
    print(&root);
    add(&root, 30);
    print(&root);
    return 0;
}

void add(node *root, int newData)
{
    node *tmp = root;
    while (tmp->next != NULL)
    {
        tmp = tmp->next;
    }
    node *newNode = malloc(sizeof(node));

    newNode->next = NULL;
    newNode->data = newData;
    
    tmp->next = newNode;
}

void print(node *root)
{
    node *tmp = root;
    do 
    {
        printf("%d\n", tmp->data);
        tmp = tmp->next;
    } while (tmp != NULL);
    printf("-------\n");
}