-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertatbegining.c
43 lines (36 loc) · 948 Bytes
/
insertatbegining.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
// Function to insert a node at the beginning of the linked list
void insertAtBeginning(struct Node** head, int newData) {
// Create a new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = newData;
newNode->next = *head;
*head = newNode;
}
// Function to print the linked list
void printList(struct Node* node) {
while (node != NULL) {
printf(" %d ", node->data);
node = node->next;
}
printf("\n");
}
// Main function
int main() {
struct Node* head = NULL;
// Insert some elements at the beginning of the list
insertAtBeginning(&head, 6);
insertAtBeginning(&head, 7);
insertAtBeginning(&head, 1);
insertAtBeginning(&head, 4);
// Print the linked list
printf("Linked list: ");
printList(head);
return 0;
}