-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLINKEDLIST.c
More file actions
48 lines (40 loc) · 1.01 KB
/
LINKEDLIST.c
File metadata and controls
48 lines (40 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
//traversal for linked list
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
void traversal(struct node *ptr){ int i=0;
while(ptr!=NULL){
printf("%d ",i ); i++;
printf("%d\n",(*ptr).data);
ptr=(*ptr).next;
}
}
int main(){
struct node *head;
struct node *first;
struct node *second;
struct node *third;
struct node *fourth;
//dynamic allocation
head = (struct node*)malloc(sizeof(struct node));
first = (struct node*)malloc(sizeof(struct node));
second = (struct node*)malloc(sizeof(struct node));
third = (struct node*)malloc(sizeof(struct node));
fourth = (struct node*)malloc(sizeof(struct node));
//making chain inshort linking
(*head).data=1;
(*head).next=first;
(*first).data=2;
(*first).next=second;
(*second).data=3;
(*second).next=third;
(*third).data=4;
(*third).next=fourth;
(*fourth).data=5;
(*fourth).next=NULL;
traversal(head);
return 0;
}