-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
53 lines (46 loc) · 928 Bytes
/
Copy pathlinked_list.cpp
File metadata and controls
53 lines (46 loc) · 928 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
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct list {int data; struct list *next;} list;
int is_empty(const list *l) { return (l == NULL); }
list* create_list(int d)
{
list* head = (list *)malloc(sizeof(list));
head->data = d;
head->next = NULL;
return head;
}
list* add_to_front(int d, list *h)
{
list *head = create_list(d);
head->next = h;
return head;
}
list* array_to_list(int data[], int size)
{
list *head = create_list(data[0]);
int i = 1;
for (i = 1; i < size; i++)
{
head = add_to_front(data[i], head);
}
return head;
}
void print_list(list *h)
{
while (h != NULL)
{
printf("%d\t", h->data);
h = h->next;
}
printf("\n\n");
}
int main(void)
{
int SIZE = 5;
int data[SIZE] = {1,2,3,4,5};
list *head = NULL;
head = array_to_list(data, SIZE);
print_list(head);
return 0;
}