-
Notifications
You must be signed in to change notification settings - Fork 0
/
manual_creation_of_linkedlist.c
47 lines (40 loc) · 1.11 KB
/
manual_creation_of_linkedlist.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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node * next;
};
void linkedlistprint(struct node * ptr)
{
while(ptr!=NULL)
{
printf("ELEMENT: %d \n",ptr->data);
ptr=ptr->next; //counter for pointer
}
}
int main()
{
//creating pointers which will point to struct node and then using it we can see the data and address inside of it
struct node* head;
struct node* second;
struct node* third;
struct node* fourth;
//creating linkedlist
head=(struct node*)malloc(sizeof(struct node)); //malloc returns int so we typecast it into struct node*
second=(struct node*)malloc(sizeof(struct node));
third=(struct node*)malloc(sizeof(struct node));
fourth=(struct node*)malloc(sizeof(struct node));
//linking second to head
head->data=11;
head->next=second;
//linking third to second
second->data=23;
second->next=third;
//entering fourth to third
third->data=71;
third->next=fourth;
//Terminating fourth
fourth->data=66;
fourth->next=NULL;
linkedlistprint(head);
}