-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlink.c
executable file
·87 lines (62 loc) · 1.44 KB
/
link.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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Linked List Basic Implementation :)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* link;
};
struct node* InsertFront(struct node* head)
{
struct node* newNode = malloc(sizeof(struct node )) ;
newNode->data = 5;
newNode->link=head;
head = newNode;
return head;
}
int LengthList(struct node* head)
{
int count=0;
struct node* cur ;
cur = head ;
while(cur!=NULL)
{
count++;
cur=cur->link;
}
return count;
}
struct node* LinkedList()
{
struct node* head= NULL;
struct node* second = NULL;
struct node* third = NULL;
head = malloc(sizeof(struct node));
second = malloc(sizeof(struct node));
third = malloc(sizeof(struct node));
head->data = 1;
head->link=second;
second->data=2;
second->link=third;
third->data=3;
third->link=NULL;
printf("\n Head !! %d -%p ------>!! ",head->data,head->link);
printf("\n second !! %d - %p !! --------> ",second->data,second->link);
printf("\n third !! %d - %p --------- >!! \n",third->data,third->link);
return head;
}
int main()
{
// Building the Linked List
struct node* res = LinkedList();
int length = LengthList(res);
// Length of the Linked List
printf("\n Length of the Linked List is %d\n",length);
// Inserting Node in the front
struct node* new = InsertFront(res);
length = LengthList(res);
printf("New Node data %d",new->data);
printf("\n New Node points to %p",new->link);
//printf("\n %p\n",res->link);
return 0;
}