1#include <stdio.h>
2#include <stdlib.h>
3
4struct node
5{
6 int num; //Data of the node
7 struct node *nextptr; //Address of the next node
8}*stnode;
9
10void createNodeList(int n); // function to create the list
11void displayList(); // function to display the list
12
13int main()
14{
15 int n;
16 printf("\n\n Linked List : To create and display Singly Linked List :\n");
17 printf("-------------------------------------------------------------\n");
18
19 printf(" Input the number of nodes : ");
20 scanf("%d", &n);
21 createNodeList(n);
22 printf("\n Data entered in the list : \n");
23 displayList();
24 return 0;
25}
26void createNodeList(int n)
27{
28 struct node *fnNode, *tmp;
29 int num, i;
30 stnode = (struct node *)malloc(sizeof(struct node));
31
32 if(stnode == NULL) //check whether the fnnode is NULL and if so no memory allocation
33 {
34 printf(" Memory can not be allocated.");
35 }
36 else
37 {
38// reads data for the node through keyboard
39
40 printf(" Input data for node 1 : ");
41 scanf("%d", &num);
42 stnode->num = num;
43 stnode->nextptr = NULL; // links the address field to NULL
44 tmp = stnode;
45// Creating n nodes and adding to linked list
46 for(i=2; i<=n; i++)
47 {
48 fnNode = (struct node *)malloc(sizeof(struct node));
49 if(fnNode == NULL)
50 {
51 printf(" Memory can not be allocated.");
52 break;
53 }
54 else
55 {
56 printf(" Input data for node %d : ", i);
57 scanf(" %d", &num);
58
59 fnNode->num = num; // links the num field of fnNode with num
60 fnNode->nextptr = NULL; // links the address field of fnNode with NULL
61
62 tmp->nextptr = fnNode; // links previous node i.e. tmp to the fnNode
63 tmp = tmp->nextptr;
64 }
65 }
66 }
67}
68void displayList()
69{
70 struct node *tmp;
71 if(stnode == NULL)
72 {
73 printf(" List is empty.");
74 }
75 else
76 {
77 tmp = stnode;
78 while(tmp != NULL)
79 {
80 printf(" Data = %d\n", tmp->num); // prints the data of current node
81 tmp = tmp->nextptr; // advances the position of current node
82 }
83 }
84}
85
86
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