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// https://github.com/davidemesso/LinkedListC for the full
2// implementation of every basic function
3
4typedef struct Node
5{
6 // Void pointer content of this node (to provide multityping).
7 void* data;
8
9 // Points to the next Node in the list.
10 struct Node* next;
11} Node;
12
13Node *llNewList(void* data, Node* next)
14{
15 Node* result =
16 (Node*)malloc(sizeof(Node));
17
18 result->data = data;
19 result->next = next;
20
21 return result;
22}
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <stdbool.h>
5
6struct node {
7 int data;
8 int key;
9 struct node *next;
10};
11
12struct node *head = NULL;
13struct node *current = NULL;
14
15//display the list
16void printList() {
17 struct node *ptr = head;
18 printf("\n[ ");
19
20 //start from the beginning
21 while(ptr != NULL) {
22 printf("(%d,%d) ",ptr->key,ptr->data);
23 ptr = ptr->next;
24 }
25
26 printf(" ]");
27}
28
29//insert link at the first location
30void insertFirst(int key, int data) {
31 //create a link
32 struct node *link = (struct node*) malloc(sizeof(struct node));
33
34 link->key = key;
35 link->data = data;
36
37 //point it to old first node
38 link->next = head;
39
40 //point first to new first node
41 head = link;
42}
43
44//delete first item
45struct node* deleteFirst() {
46
47 //save reference to first link
48 struct node *tempLink = head;
49
50 //mark next to first link as first
51 head = head->next;
52
53 //return the deleted link
54 return tempLink;
55}
56
57//is list empty
58bool isEmpty() {
59 return head == NULL;
60}
61
62int length() {
63 int length = 0;
64 struct node *current;
65
66 for(current = head; current != NULL; current = current->next) {
67 length++;
68 }
69
70 return length;
71}
72
73//find a link with given key
74struct node* find(int key) {
75
76 //start from the first link
77 struct node* current = head;
78
79 //if list is empty
80 if(head == NULL) {
81 return NULL;
82 }
83
84 //navigate through list
85 while(current->key != key) {
86
87 //if it is last node
88 if(current->next == NULL) {
89 return NULL;
90 } else {
91 //go to next link
92 current = current->next;
93 }
94 }
95
96 //if data found, return the current Link
97 return current;
98}
99
100//delete a link with given key
101struct node* delete(int key) {
102
103 //start from the first link
104 struct node* current = head;
105 struct node* previous = NULL;
106
107 //if list is empty
108 if(head == NULL) {
109 return NULL;
110 }
111
112 //navigate through list
113 while(current->key != key) {
114
115 //if it is last node
116 if(current->next == NULL) {
117 return NULL;
118 } else {
119 //store reference to current link
120 previous = current;
121 //move to next link
122 current = current->next;
123 }
124 }
125
126 //found a match, update the link
127 if(current == head) {
128 //change first to point to next link
129 head = head->next;
130 } else {
131 //bypass the current link
132 previous->next = current->next;
133 }
134
135 return current;
136}
137
138void sort() {
139
140 int i, j, k, tempKey, tempData;
141 struct node *current;
142 struct node *next;
143
144 int size = length();
145 k = size ;
146
147 for ( i = 0 ; i < size - 1 ; i++, k-- ) {
148 current = head;
149 next = head->next;
150
151 for ( j = 1 ; j < k ; j++ ) {
152
153 if ( current->data > next->data ) {
154 tempData = current->data;
155 current->data = next->data;
156 next->data = tempData;
157
158 tempKey = current->key;
159 current->key = next->key;
160 next->key = tempKey;
161 }
162
163 current = current->next;
164 next = next->next;
165 }
166 }
167}
168
169void reverse(struct node** head_ref) {
170 struct node* prev = NULL;
171 struct node* current = *head_ref;
172 struct node* next;
173
174 while (current != NULL) {
175 next = current->next;
176 current->next = prev;
177 prev = current;
178 current = next;
179 }
180
181 *head_ref = prev;
182}
183
184void main() {
185 insertFirst(1,10);
186 insertFirst(2,20);
187 insertFirst(3,30);
188 insertFirst(4,1);
189 insertFirst(5,40);
190 insertFirst(6,56);
191
192 printf("Original List: ");
193
194 //print list
195 printList();
196
197 while(!isEmpty()) {
198 struct node *temp = deleteFirst();
199 printf("\nDeleted value:");
200 printf("(%d,%d) ",temp->key,temp->data);
201 }
202
203 printf("\nList after deleting all items: ");
204 printList();
205 insertFirst(1,10);
206 insertFirst(2,20);
207 insertFirst(3,30);
208 insertFirst(4,1);
209 insertFirst(5,40);
210 insertFirst(6,56);
211
212 printf("\nRestored List: ");
213 printList();
214 printf("\n");
215
216 struct node *foundLink = find(4);
217
218 if(foundLink != NULL) {
219 printf("Element found: ");
220 printf("(%d,%d) ",foundLink->key,foundLink->data);
221 printf("\n");
222 } else {
223 printf("Element not found.");
224 }
225
226 delete(4);
227 printf("List after deleting an item: ");
228 printList();
229 printf("\n");
230 foundLink = find(4);
231
232 if(foundLink != NULL) {
233 printf("Element found: ");
234 printf("(%d,%d) ",foundLink->key,foundLink->data);
235 printf("\n");
236 } else {
237 printf("Element not found.");
238 }
239
240 printf("\n");
241 sort();
242
243 printf("List after sorting the data: ");
244 printList();
245
246 reverse(&head);
247 printf("\nList after reversing the data: ");
248 printList();
249}
1// Node of the list
2typedef struct node {
3 int val;
4 struct node * next;
5} node_t;
6
1#include <stdio.h>
2#include <stdlib.h>
3
4// Represents a node
5typedef struct node
6{
7 int number;
8 struct node *next;
9}
10node;
11
12int main(void)
13{
14 // List of size 0. We initialize the value to NULL explicitly, so there's
15 // no garbage value for our list variable
16 node *list = NULL;
17
18 // Allocate memory for a node, n
19 node *n = malloc(sizeof(node));
20 if (n == NULL)
21 {
22 return 1;
23 }
24
25 // Set the value and pointer in our node
26 n->number = 1;
27 n->next = NULL;
28
29 // Add node n by pointing list to it, since we only have one node so far
30 list = n;
31
32 // Allocate memory for another node, and we can reuse our variable n to
33 // point to it, since list points to the first node already
34 n = malloc(sizeof(node));
35 if (n == NULL)
36 {
37 free(list);
38 return 1;
39 }
40
41 // Set the values in our new node
42 n->number = 2;
43 n->next = NULL;
44
45 // Update the pointer in our first node to point to the second node
46 list->next = n;
47
48 // Allocate memory for a third node
49 n = malloc(sizeof(node));
50 if (n == NULL)
51 {
52 // Free both of our other nodes
53 free(list->next);
54 free(list);
55 return 1;
56 }
57 n->number = 3;
58 n->next = NULL;
59
60 // Follow the next pointer of the list to the second node, and update
61 // the next pointer there to point to n
62 list->next->next = n;
63
64 // Print list using a loop, by using a temporary variable, tmp, to point
65 // to list, the first node. Then, every time we go over the loop, we use
66 // tmp = tmp->next to update our temporary pointer to the next node. We
67 // keep going as long as tmp points to somewhere, stopping when we get to
68 // the last node and tmp->next is null.
69 for (node *tmp = list; tmp != NULL; tmp = tmp->next)
70 {
71 printf("%i\n", tmp->number);
72 }
73
74 // Free list, by using a while loop and a temporary variable to point
75 // to the next node before freeing the current one
76 while (list != NULL)
77 {
78 // We point to the next node first
79 node *tmp = list->next;
80 // Then, we can free the first node
81 free(list);
82 // Now we can set the list to point to the next node
83 list = tmp;
84 // If list is null, when there are no nodes left, our while loop will stop
85 }
86}
87