Answers for "list c"

C
2

list in C

#include <stdio.h>

int main (int argc, char * argv[])
{
    int i = 0;
    int list[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int run = 1;
    int length_of_list; // the number of elements that the list contains
    length_of_list = sizeof(list) / sizeof(int); // getting the number
    
    while (run){ //printing the list
    printf("The list is: %d\n", list[i]);
    i = i + 1;
    if (i == length_of_list){ //check if the list has ended
        printf("The list has ended!\n");
        break; // exit the loop
    } 
    }

    return 0;
}
Posted by: Guest on July-02-2021
1

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\n", len(list));

    add(&list, 123);
    printf("Size %d\n", len(list));
    printf("LastNode %d\n\n", (getLastNode(list))->data);

    add(&list, 12);
    printf("Size %d\n", len(list));
    printf("LastNode %d\n\n", (getLastNode(list))->data);

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

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

    return 0;
}
Posted by: Guest on October-06-2021
17

how to make a linked list in c

typedef struct node{
    int value; //this is the value the node stores
    struct node *next; //this is the node the current node points to. this is how the nodes link
}node;

node *createNode(int val){
    node *newNode = malloc(sizeof(node));
    newNode->value = val;
    newNode->next = NULL;
    return newNode;
}
Posted by: Guest on June-21-2020
3

c list

// Node of the list
typedef struct node {
    int val;
    struct node * next;
} node_t;
Posted by: Guest on June-16-2020

Code answers related to "C"

Browse Popular Code Answers by Language