Search
 
SCRIPT & CODE EXAMPLE
 

C

list c

#include <stdio.h>
#include <stdlib.h>
struct Node
{
    int data;
    struct Node *nextPtr;
};

//create a node

struct Node *newNode(int data)
{
    struct Node *out;
    //malloc allocates dinamically what's inside the round brackets
    out = (struct Node *)malloc(sizeof(struct Node));
    //-> its the equivalent of (*out).data = data
    //all'inerno della variabile data della struttura out metti data
    out->data = data;
    out->nextPtr = NULL;

    return out;
}
////////////////////////////////////////////////////////////////
//get the list length
int len(struct Node *list)
{
    int out = 0;
    while (list != NULL)
    {
        out++;
        list = list->nextPtr;
    }
    return out;
}
////////////////////////////////////////////////////////////////
//get last node
struct Node *getLastNode(struct Node *list)
{
    struct Node *currentNode;
    if (list == NULL)
        currentNode = NULL;
    else
    {
        currentNode = list;
        while (currentNode->nextPtr != NULL)
        {
            currentNode = currentNode->nextPtr;
        }
    }
    return currentNode;
}
////////////////////////////////////////////////////////////////

//pop cut the Node from the list, but the node exist anyway, if you have to put that Node in another list then it should be useful
struct Node *pop(struct Node **listPtr)
{
    struct Node *out, *box;
    int n = len(*listPtr);
    int idx = 0;
    switch (n)
    {
    case 0:
        out = NULL;
        break;
    case 1:
        out = *listPtr;
        *listPtr = NULL;
        break;
    default:
        idx = 0;
        box = *listPtr;
        //i have to go at the last but one node and cut that
        while (idx < n - 2)
        {
            box = box->nextPtr;
        }
        out = box->nextPtr;
        box->nextPtr = NULL;
    }
    return out;
}
////////////////////////////////////////////////////////////////
struct Node *add(struct Node **listPtr, int data)
{
    //create the node to add
    struct Node *toAdd = newNode(data);
    if (*listPtr == NULL)
    {
        *listPtr = toAdd;
    }
    //add the node TO THE END OF THE LIST
    else
    {
        (getLastNode(*listPtr))->nextPtr = toAdd;
    }

    return toAdd;
}
int main()
{
    struct Node *list = NULL;
    printf("Size %d
", len(list));

    add(&list, 123);
    printf("Size %d
", len(list));
    printf("LastNode %d

", (getLastNode(list))->data);

    add(&list, 12);
    printf("Size %d
", len(list));
    printf("LastNode %d

", (getLastNode(list))->data);

    // VISIT the list
    struct Node *currNodePtr;
    currNodePtr = list;
    while (currNodePtr != NULL)
    {
        printf("data = %d
", currNodePtr->data);
        currNodePtr = currNodePtr->nextPtr;
    }

    pop(&list);
    printf("Size %d
", len(list));
    printf("LastNode %d

", (getLastNode(list))->data);
    pop(&list);
    printf("Size %d
", len(list));

    return 0;
}
Comment

c list

// Node of the list
typedef struct node {
    int val;
    struct node * next;
} node_t;
Comment

PREVIOUS NEXT
Code Example
C :: print in c 
C :: c defined 
C :: c add char to char array 
C :: rust set toolchain 
C :: threads in c 
C :: get boolean from localstorage 
C :: c comment 
C :: round c 
C :: majuscule en c 
C :: node in c 
C :: realloc in c 
C :: C How to define a union? 
C :: hostbuilder add environment variables 
C :: git add -u flag 
C :: solutionadda 
C :: how to belu-roll peoples in c 
C :: amazon kinesis disaster recovery 
C :: insertNode 
C :: typecating in c 
C :: Print Characters 
C :: ssl_get_servername return null 
C :: Uri/beecrowd problem no - 1099 solution in C 
C :: redis endpoint 
C :: how we can strore a nested structure values in arrays 
C :: %d and %i 
C :: reap zombie process in c 
C :: + ********************* 
C :: C temporary files 
C :: c if statement 
C :: #include <sys/time.h int main() { timespec ts; // clock_gettime(CLOCK_MONOTONIC, &ts); // Works on FreeBSD clock_gettime(CLOCK_REALTIME, &ts); // Works on Linux } 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =