-
Notifications
You must be signed in to change notification settings - Fork 256
/
01 Display LL.c
49 lines (40 loc) · 1.61 KB
/
01 Display LL.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
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
}*first = NULL; //Global Pointer //Can be directly accessed and can pass as parameter
void create(int A[], int n)
{
int i;
struct Node *t; //temporary node help in creating new node
struct Node *last; //pointer to point at last node //helps in adding new node in end of linked list
first = (struct Node *)malloc(sizeof(struct Node)); //creates space for first node in heap
first->data = A[0]; //data part is assigned as first element of the array
first->next = NULL; //next pointer points to NULL only
last = first; //last pointer made to point at first node //helps in linking nodes [V. Imp]
for(i=1; i<n; i++) //starting from 1 as already 0th element is created
{
t = (struct Node *)malloc(sizeof(struct Node)); //everytime a new space is created in heap for a new node represented by t
t->data = A[i]; //data part is assigned according to array element
t->next = NULL; //everytime the node is created its next points to NULL as there is no node after that
last->next = t; //helps in linking the previous node with the newly created node
last = t; //last refreshes its pointer to the newly created node that will help again in linking a newly created node in next iteration
}
}
void display(struct Node *p)
{
while(p != NULL)
{
printf("%d->", p->data);
p = p->next;
}
}
int main()
{
int A[] = {3,5,7,10,15};
create(A,sizeof(A)/sizeof(int));
display(first);
return 0;
}