1#include <stdio.h>
2#include <stdlib.h>
3struct Node
4{
5 int data;
6 struct Node *nextPtr;
7};
8
9//create a node
10
11struct Node *newNode(int data)
12{
13 struct Node *out;
14 //malloc allocates dinamically what's inside the round brackets
15 out = (struct Node *)malloc(sizeof(struct Node));
16 //-> its the equivalent of (*out).data = data
17 //all'inerno della variabile data della struttura out metti data
18 out->data = data;
19 out->nextPtr = NULL;
20
21 return out;
22}
23////////////////////////////////////////////////////////////////
24//get the list length
25int len(struct Node *list)
26{
27 int out = 0;
28 while (list != NULL)
29 {
30 out++;
31 list = list->nextPtr;
32 }
33 return out;
34}
35////////////////////////////////////////////////////////////////
36//get last node
37struct Node *getLastNode(struct Node *list)
38{
39 struct Node *currentNode;
40 if (list == NULL)
41 currentNode = NULL;
42 else
43 {
44 currentNode = list;
45 while (currentNode->nextPtr != NULL)
46 {
47 currentNode = currentNode->nextPtr;
48 }
49 }
50 return currentNode;
51}
52////////////////////////////////////////////////////////////////
53
54//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
55struct Node *pop(struct Node **listPtr)
56{
57 struct Node *out, *box;
58 int n = len(*listPtr);
59 int idx = 0;
60 switch (n)
61 {
62 case 0:
63 out = NULL;
64 break;
65 case 1:
66 out = *listPtr;
67 *listPtr = NULL;
68 break;
69 default:
70 idx = 0;
71 box = *listPtr;
72 //i have to go at the last but one node and cut that
73 while (idx < n - 2)
74 {
75 box = box->nextPtr;
76 }
77 out = box->nextPtr;
78 box->nextPtr = NULL;
79 }
80 return out;
81}
82////////////////////////////////////////////////////////////////
83struct Node *add(struct Node **listPtr, int data)
84{
85 //create the node to add
86 struct Node *toAdd = newNode(data);
87 if (*listPtr == NULL)
88 {
89 *listPtr = toAdd;
90 }
91 //add the node TO THE END OF THE LIST
92 else
93 {
94 (getLastNode(*listPtr))->nextPtr = toAdd;
95 }
96
97 return toAdd;
98}
99int main()
100{
101 struct Node *list = NULL;
102 printf("Size %d\n", len(list));
103
104 add(&list, 123);
105 printf("Size %d\n", len(list));
106 printf("LastNode %d\n\n", (getLastNode(list))->data);
107
108 add(&list, 12);
109 printf("Size %d\n", len(list));
110 printf("LastNode %d\n\n", (getLastNode(list))->data);
111
112 // VISIT the list
113 struct Node *currNodePtr;
114 currNodePtr = list;
115 while (currNodePtr != NULL)
116 {
117 printf("data = %d\n", currNodePtr->data);
118 currNodePtr = currNodePtr->nextPtr;
119 }
120
121 pop(&list);
122 printf("Size %d\n", len(list));
123 printf("LastNode %d\n\n", (getLastNode(list))->data);
124 pop(&list);
125 printf("Size %d\n", len(list));
126
127 return 0;
128}
1#include <stdio.h>
2
3int main (int argc, char * argv[])
4{
5 int i = 0;
6 int list[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
7 int run = 1;
8 int length_of_list; // the number of elements that the list contains
9 length_of_list = sizeof(list) / sizeof(int); // getting the number
10
11 while (run){ //printing the list
12 printf("The list is: %d\n", list[i]);
13 i = i + 1;
14 if (i == length_of_list){ //check if the list has ended
15 printf("The list has ended!\n");
16 break; // exit the loop
17 }
18 }
19
20 return 0;
21}
1typedef struct node{
2 int value; //this is the value the node stores
3 struct node *next; //this is the node the current node points to. this is how the nodes link
4}node;
5
6node *createNode(int val){
7 node *newNode = malloc(sizeof(node));
8 newNode->value = val;
9 newNode->next = NULL;
10 return newNode;
11}
1// Node of the list
2typedef struct node {
3 int val;
4 struct node * next;
5} node_t;
6