-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathd42_linked_list.c
More file actions
70 lines (61 loc) · 1.01 KB
/
d42_linked_list.c
File metadata and controls
70 lines (61 loc) · 1.01 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Implementation of linked list
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct node
{
int data;
struct node *link;
};
typedef struct node node;
struct list
{
node *head;
int size;
};
typedef struct list list;
bool is_empty(list *l)
{
return l->size == 0;
}
list * create_list()
{
list *l = (list *)malloc(sizeof(list));
l->head = NULL;
l->size = 0;
return l;
}
// front operations
void insert_front(list *l,int val)
{
node *n = (node *)malloc(sizeof(node));
n->data = val;
n->link = l->head;
l->head = n;
l->size++;
}
int delete_front(list *l)
{
if(is_empty(l))
{
printf("List Empty\n");
return 0;
}
int val = l->head->data;
node *temp = l->head;
l->head = l->head->link;
free(temp);
l->size--;
return val;
}
int main(void)
{
list *l = create_list();
insert_front(l,10);
insert_front(l,20);
while(!is_empty(l))
{
printf("%d\n",delete_front(l));
}
return 0;
}