-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linklist-travesal.c
94 lines (73 loc) · 2.17 KB
/
Linklist-travesal.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
88
89
90
91
92
93
94
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
struct parent
{
int data;
struct parent *next;
};
void search(struct node *head, int search)
{
struct node *p;
p=head;
while(p!=NULL)
{
if(p->data==search)
{
printf("found sucess\n");
break;
}
p=p->next;
}
}
struct parent * beginsert(struct parent *head, int data)
{
struct parent *newnode=(struct parent *)malloc(sizeof(struct parent));
newnode->next=head;
newnode->data=data;
}
void linkedListTraversal(struct parent *i)
{
while (i != NULL)
{
printf("Element: %d\n", i->data);
i = i->next;
}
}
int main()
{
struct parent *head;
struct parent *second;
struct parent *third;
struct parent *fourth;
struct parent *five;
// Allocate memory for nodes in the linked list in Heap
head = (struct parent *)malloc(sizeof(struct parent));
second = (struct parent *)malloc(sizeof(struct parent));
third = (struct parent *)malloc(sizeof(struct parent));
fourth = (struct parent *)malloc(sizeof(struct parent));
five = (struct parent *)malloc(sizeof(struct parent));
// Link first and second nodes
head->data = 100;
head->next = second;
// Link second and third nodes
second->data = 11;
second->next = third;
// Link third and fourth nodes
third->data = 41;
third->next = fourth;
// Link four and five nodes
fourth->data = 66;
fourth->next = five;
// Terminate the list at the third node
five->data = 90;
five->next = NULL;
printf("\nBefore insertiohn luink oist element :");
linkedListTraversal(head);
head=beginsert(head, 200);
printf("\nAfter insertionj link list elementP:\n");
linkedListTraversal(head);
search(head,90);
getch();
return 0;
}