-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_in_linked_list_after_particular_value.cpp
More file actions
76 lines (65 loc) · 1.83 KB
/
insertion_in_linked_list_after_particular_value.cpp
File metadata and controls
76 lines (65 loc) · 1.83 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
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
#include<iostream>
using namespace std ;
class Node{
public:
int data;
class Node * next ;
};
void linked_list_traversal (class Node * head)
{
while (head != NULL)
{
cout<<"|"<<head->data<<"|";
head = head->next ;
}
}
class Node * create_node (class Node * head ,int data)
{
class Node * new_node = new Node ;
new_node->data = data ;
new_node->next = NULL ;
head->next = new_node ;
head = new_node ;
return head ;
}
void insertionInBetween( class Node * head ,int information_P , int data_to_be_inserted)
{ class Node * new_node = new Node ;
new_node->data = data_to_be_inserted ;
while( head != NULL )
{ if(head->data == information_P)
break;
head = head->next ;
}
new_node->next = head->next;
head->next = new_node;
}
int main()
{
// Creating an array of integers (1-10)
int array_of_integer_1[10] ;
for(int i = 0 ; i < 10 ; i++)
{
array_of_integer_1[i] = i+1 ;
}
//Creating a head Node for your linked list 1.
class Node * head_1 = new Node ;
head_1->data = array_of_integer_1[0] ;
head_1->next = NULL ;
// Connecting more elements to linked list 1
class Node * new_node_1 ;
new_node_1 = create_node(head_1 , array_of_integer_1[1]);
for(int i = 2 ; i < 10 ; i++)
{
new_node_1 = create_node(new_node_1 , array_of_integer_1[i]);
}
//Traveral of Linked List 1
cout<<"List 1 --->";
linked_list_traversal(head_1);
//------------------------------------------------------------------------//
int information_P = 78 ;
int element_to_be_inserted_after = 5;
insertionInBetween(head_1 , element_to_be_inserted_after , information_P );
cout<<endl<<"After Insertion --->";
linked_list_traversal(head_1);
return 0 ;
}