-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
165 lines (148 loc) · 4.48 KB
/
linked_list.cpp
File metadata and controls
165 lines (148 loc) · 4.48 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <vector>
using namespace std;
class Node{
public:
int data;
Node *next;
};
//IMP: if we need to assign this pointer to another, WAY 1
//we should use double pointer,which we are accepting as the parameter when defining
//we are passing the address of the pointer when calling
void delete_entire_linked_list(Node **head){
Node *next_node, *current_node;
current_node = *head;
while(current_node!=NULL){
next_node = current_node -> next;
free(current_node);
current_node = next_node;
}
free(next_node);
*head = NULL;
}
//IMP: if we need to assign this pointer to another, WAY 2
//function definition should accept pointer address, i.e., accepting address of pointer when defining
//when called we pass the pointer
int pairwise_swap_elements(Node &head){
//https://www.geeksforgeeks.org/pairwise-swap-elements-of-a-given-linked-list/
Node *first_in_pair, *second_in_pair;
first_in_pair = &head;
second_in_pair = first_in_pair -> next;
int temp;
while(second_in_pair!=NULL){
temp = first_in_pair -> data;
first_in_pair -> data = second_in_pair -> data;
second_in_pair -> data = temp;
if (second_in_pair -> next != NULL){
first_in_pair = second_in_pair -> next;
second_in_pair = first_in_pair -> next;
}else{
return 0;
}
}
return 0;
}
int count_repetition_of_node_data(Node *head, int key){
//https://www.geeksforgeeks.org/write-a-function-that-counts-the-number-of-times-a-given-int-occurs-in-a-linked-list/
if(head != NULL){
if(head -> data == key){
return 1 + count_repetition_of_node_data(head -> next, key);
}
else{
return count_repetition_of_node_data(head -> next, key);
}
}else{
return 0;
}
}
void find_middle_node_of_linked_list_using_2_pointers(Node **head){
Node *single_jump_ptr, *double_jump_ptr;
single_jump_ptr = *head, double_jump_ptr = *head;
while(double_jump_ptr -> next != NULL){
if(double_jump_ptr -> next -> next != NULL){
double_jump_ptr = double_jump_ptr -> next -> next;
}else{
double_jump_ptr = double_jump_ptr -> next;
}
single_jump_ptr = single_jump_ptr -> next;
}
cout << "The middle node of the linked list is = " <<single_jump_ptr -> data << endl;
}
bool search_linked_list_iteratively(Node *current_node, int search_val){
while(current_node!=NULL){
if(current_node->data == search_val){
cout << "FOUND VALUE ITERATIVELY= " << search_val << endl;
return true;
}
current_node = current_node -> next;
}
cout << "COULDN'T FIND VALUE ITERATIVELY= " << search_val << endl;
return false;
}
bool search_linked_list_recursively(Node *current_node, int search_val){
if(current_node == NULL){
cout << "COULDN'T FIND VALUE RECURSIVELY= " << search_val << endl;
return false;
}else if(current_node->data == search_val){
cout << "FOUND VALUE RECURSIVELY= " << search_val << endl;
return true;
}
return search_linked_list_recursively(current_node->next, search_val);
}
int main(){
vector<int> arr = {1};
Node *new_node, *current_node, *head=NULL;
// APPEND STARTS HERE
for(int i=0; i<arr.size(); i++){
new_node = new Node();
new_node -> data = arr[i];
new_node -> next = NULL;
if(head == NULL){
head = new_node;
current_node = new_node;
}
else{
current_node -> next = new_node;
current_node = new_node;
}
};
// APPEND ENDS HERE
// DELETE FOR KEY STARTS HERE
int delete_node_value = 31;
Node *prev_node;
current_node = head, prev_node = NULL;
while(current_node != NULL){
if(prev_node == NULL && current_node -> data == delete_node_value){
head = current_node -> next;
free(current_node);
}
else if(current_node -> data == delete_node_value){
prev_node -> next = current_node -> next;
free(current_node);
}
prev_node = current_node;
current_node = current_node -> next;
}
// DELETE FOR KEY ENDS HERE
// delete_entire_linked_list(&head);
search_linked_list_iteratively(head, 3);
search_linked_list_recursively(head, 3);
find_middle_node_of_linked_list_using_2_pointers(&head);
cout << "COUNT OF 1 = " <<count_repetition_of_node_data(head, 1) << endl;
cout << endl << "BEFORE PAIR WISE ELEMENT SWAPPING" << endl;
current_node = head;
while(current_node!=NULL){
cout << current_node -> data << " ";
current_node = current_node -> next;
}
pairwise_swap_elements(*head);
cout << endl << "AFTER PAIR WISE ELEMENT SWAPPING" << endl;
// TRAVERSAL STARTS HERE
current_node = head;
while(current_node!=NULL){
cout << current_node -> data << " ";
current_node = current_node -> next;
}
// TRAVERSAL ENDS HERE
return 0;
}