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 :: how to check if a string pointer is empty in c 
C :: c print multiple variables 
C :: char array to int c 
C :: sum average min max in c array 
C :: c print sizeof char 
C :: multiplication table in c using array 
C :: c pass int by reference 
C :: merge sort for strings in c 
C :: read a document from console in c 
C :: how to delete virtual hard disk virtualbox 
C :: casting an int to a char in c 
C :: array size in c 
C :: warning: function returns address of local variable [-Wreturn-local-addr] 
C :: to run Blazor project using CLI 
C :: bootstrap 4 forms 
C :: delete string function in c 
C :: print command for rust unit-test 
C :: c long to string 
C :: mongodb read 
C :: How to copy one string into another in C 
C :: pasar a binario recursivo 
C :: c program to find minimum of 5 numbers using conditional operator in c 
C :: c pause for 1 second 
C :: nested while loop in c 
C :: c defined value sum 
C :: typedef c 
C :: allintext:christie kiser filetype:log 
C :: vscode how to output in seperate consile 
C :: block quote in lua 
C :: diamond dataset in r 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =