-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path53.LinkedList_Operations_P1_Till_insert.cpp
207 lines (192 loc) · 4.16 KB
/
53.LinkedList_Operations_P1_Till_insert.cpp
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
202
203
204
205
206
207
/*
DISPLAY - done
R-DISPLAY - done
COUNTINF NODES - done
SUM OF ELEMENTS + RECUSRSIVE APPROACH - done
MAX OF LL + RECURSIVE APPROACH - done (slight confusion in rmax)
SEARCHING + RECURSIVE APPROACH - done
IMPROVED SEARCHING - move to head - done
*/
#include<iostream>
using namespace std;
struct node{
int data;
node* next;
}*first = NULL;
node* create(int A[], int num){
first = new node;
first->data = A[0];
node *temp,*last;
last = first;
for(int i=1;i<num;i++){
temp = new node;
temp->data = A[i];
temp->next = NULL;
last->next = temp;
last = temp;
}
return first;
}
void display(node *first){
node* temp = first;
while(temp!=NULL){
cout<<temp->data<<" ";
temp = temp->next;
}
}
void rdisplay(node *first){
if(first->next!=NULL){
cout<<first->data<<" ";
rdisplay(first->next);
}
else
cout<<first->data;
}
int count_nodes(node* first){
node* temp = first;
int i=0;
while(temp!=NULL){
i++;
temp = temp->next;
}
return i;
}
int sum(node* first){
node* temp = first;
int sum=0;
while(temp!=NULL){
sum+=temp->data;
temp = temp->next;
}
return sum;
}
int rsum(node* first){
if(first->next==NULL)
return first->data;
else
return first->data + rsum(first->next);
}
int max(node *first){
int maximum=-1;
node *temp = first;
while(temp!=NULL){
if(maximum<temp->data){
maximum = temp->data;
}
temp = temp->next;
}
return maximum;
}
int rmax(node *first){
int x = 0;
if(first==0)
return 0;
x = rmax(first->next);
if(x<first->data)
return first->data;
else
return x;
}
void search(node* first, int x){
node* temp = first;
while(temp!=NULL){
if(temp->data == x){
cout<<"found "<<x;
return;
}
temp = temp->next;
}
cout<<"Not present";
}
void rsearch(node *n, int x){
if(n == NULL){
cout<<"not present";
return;
}
if(x == n->data){
cout<<"found "<<x;
return;
}
else{
rsearch(n->next,x);
}
}
void move_to_head_search(node* &first, int x){
node *temp = first;
node *back=NULL;
while(temp!=NULL){
if(temp->data!=x){
//increment both
temp = temp->next;
if(back == NULL){
back = first;
} else {
back = back->next;
}
}
else{
//data is found, remove that node and make it point at first, make back point at next node prior to this
back->next = temp->next;
temp->next = first; // first becomes second
first = temp; // temp becomes first
//stop
break;
}
}
if(temp==NULL)
cout<<x<<" not found.";
else{
cout<<x<<" found.";
cout<<"\nhere: ";
display(first);
}
}
void insert(node* &first, int x, int pos){
node* t = new node;
t->data = x;
if(pos==0){
//at first node
t->next = first;
first = t;
}
else{
//we should be at pos -1 node
node* p = first;
for(int i=0;i<pos -1;i++){
p = p->next;
}
t->next = p->next;
p->next = t;
}
}
int main(){
int A[] = {1,2,3,4,5,6,7,8,9};
first = create(A,9);
cout<<"displaying: "<<endl;
display(first);
cout<<"\ndisplaying using recursive function: "<<endl;
rdisplay(first);
cout<<"\nCount Nodes:\n"<<count_nodes(first);
cout<<"\nSum:\n";
cout<<sum(first);
cout<<"\nRecursive sum:\n";
cout<<rsum(first);
cout<<"\nmaximum:\n";
cout<<max(first);
cout<<"\nrmax:\n";
cout<<rmax(first);
cout<<"\nSearch:\n";
search(first,7);
cout<<"\nrecusrsive search:\n";
rsearch(first,90);
cout<<"\nmove to head: \n";
move_to_head_search(first,7);
cout<<endl;
cout<<"ojbfkjbwf"<<first->data;
insert(first,100,0);
display(first);
cout<<endl;
insert(first,999,5);
cout<<endl;
display(first);
}