forked from Romeo-Aryal/C-program-fest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicOperation.cpp
More file actions
201 lines (172 loc) · 3.37 KB
/
Copy pathbasicOperation.cpp
File metadata and controls
201 lines (172 loc) · 3.37 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node *next;
node(int d)
{
data = d;
next = NULL;
}
};
//insertion
void insertAtHead(node*&head,int data){
if(head == NULL){
head = new node(data);
return;
}
node *n = new node(data);
n->next = head;
head = n;
}
int length(node*head){
int cnt = 0;
while(head != NULL){
cnt++;
head= head->next;
}
return cnt;
}
void insertAtTail(node *&head,int data){
if(head == NULL){
head = new node(data);
return;
}
node *tail = head;
while(tail->next != NULL){
tail = tail->next;
}
node* temp = new node(data);
tail->next = temp;
temp->next = NULL;
return;
}
void InsertInMiddle(node*&head,int data,int p){
//corner case
if(head == NULL or p ==0){
insertAtHead(head,data);
}else if(p>length(head)){
insertAtTail(head,data);
}else{
//take p-1 jump
int jump = 1;
node* temp = head;
while(jump<= p-1){
temp = temp->next;
jump++;
}
node*n = new node(data);
n->next = temp->next;
temp->next = n;
}
}
// Deletion
void deleteHead(node*&head) {
if(head == NULL){
return;
}
node*temp = head->next;
delete head;
head = temp;
}
void deleteAtTail(node*&head){
if(head==NULL){
return;
}
if(head->next == NULL){
delete head;
head = NULL;
return;
}
node*temp = head;
while(temp->next->next != NULL){
temp = temp->next;
}
node *lastNode = temp->next;
delete(lastNode);
temp->next = NULL;
}
void deleteInMiddle(node*&head,int p){
if(head==NULL){
return;
}else if(p>length(head)){
deleteAtTail(head);
}else{
int jump = 1;
node*temp = head;
while(jump<= p-2){
temp = temp->next;
jump++;
}
node*targetNode = temp->next;
temp->next = temp->next->next;
delete(targetNode);
}
}
// searching in linkedList
//only linear search will be done in linkedList
bool search(node *head,int key){
node*temp = head;
while(head!=NULL){
if(head->data ==key){
return true;
}
head = head->next;
}
return false;
}
//search by recursive function
bool search_rec(node *head,int key){
if(head == NULL){
return false;
}
//rec case
if(head->data == key){
return true;
}else{
return search(head->next,key);
}
}
//Taking input
void print(node * head){
while(head != NULL){
cout<<head->data<<"->";
head = head->next;
}
putchar('\n');
}
int main()
{
node* head = NULL;
insertAtHead(head,4);
insertAtHead(head,2);
insertAtHead(head,1);
insertAtHead(head,0);
print(head);
InsertInMiddle(head,3,4);
print(head);
insertAtTail(head,5);
print(head);
cout<<length(head)<<endl;
deleteInMiddle(head,5);
print(head);
deleteAtTail(head);
print(head);
deleteHead(head);
print(head);
int key;
cin>>key;
if(search(head,key)){
cout<<"present"<<endl;
}else
{
cout<<"not present"<<endl;
}
if(search_rec(head,key)){
cout<<"present"<<endl;
}else
{
cout<<"not present"<<endl;
}
}