-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked-list-array.c
More file actions
42 lines (41 loc) · 879 Bytes
/
linked-list-array.c
File metadata and controls
42 lines (41 loc) · 879 Bytes
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
#include<stdio.h>
#include<stdlib.h>
struct Node *createLinkedList(int arr[], int size);
struct Node {
int data;
struct Node *next;
};
int main()
{
int a[] = {5, 10, 15, 20};
struct Node *head;
head = createLinkedList(a, 4);
while(head != NULL)
{
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
return 0;
}
struct Node *createLinkedList(int arr[], int size)
{
struct Node *head = NULL, *temp = NULL, *current = NULL;
int i;
for(i = 0; i < size; i++)
{
temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = arr[i];
temp->next = NULL;
if(head == NULL)
{
head = temp;
current = temp;
}
else{
current->next = temp;
current = current->next;
}
}
return head;
}