-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlinked list.cpp
More file actions
207 lines (191 loc) · 3.67 KB
/
linked list.cpp
File metadata and controls
207 lines (191 loc) · 3.67 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
202
203
204
205
206
207
#include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node *next;
node(int val){
data=val;
next=NULL;
}
};
void insertattail(node *&head,int value){
node*newnode=new node(value);
if(head==NULL){
head=newnode;
}
node* curr=head;
while(curr->next!=NULL){
curr=curr->next;
}
curr->next=newnode;
newnode->next=NULL;
}
void display(node *head){
node *curr=head;
while(curr!=NULL){
cout<<curr->data<<"->";
curr=curr->next;
}
cout<<"NULL";
cout<<endl;
}
void deleteatposition(node *&head,int pos){
int count=1;
node *curr=head;
node *prev=NULL;
if(pos==1){
head=head->next;
}
while(count<=pos-1 && curr!=NULL){
prev=curr;
curr=curr->next;
count++;
}
prev->next=curr->next;
curr->next=NULL;
}
void evenafterodd(node * &head){ //1->2->3->4->5->6->null
node*odd=head;
node*even=head->next;
node* evenstart=even;
while(odd->next!=NULL && even->next!=NULL){
odd->next=even->next;
odd=odd->next;
even->next=odd->next;
even=even->next;
}
odd->next=evenstart;
if(odd->next==NULL){//1->2->3->4->5->6->7->null
even->next=NULL;
}
}
void reversek(node*s,node*t){
node* curr=s;
node* next=s->next;
node* prev=NULL;
while(prev!=t ){
curr->next=prev;
prev=curr;
curr=next;
if(next!=NULL)next=next->next;
}
}
node * reversegroupk(node *head,int k){
if(head==NULL ||head->next==NULL||k==1)return head;
node* tail=head;
node*start=head;
int inc=k-1;
while(inc--){
tail=tail->next;
if(tail==NULL){
return head;
}
}
node *nexthead=reversegroupk(tail->next,k);
reversek(start,tail);
start->next=nexthead;
return tail;
}
node * even1(node *&head){
int i=0,j=0;
int l1[100];
int l2[100];
node *temp=head;
while(temp!=NULL){
if(temp->data%2==0){
l1[i++]=temp->data;
}else{
l2[j++]=temp->data;
}
temp=temp->next;
}
int l=0,m=0;
while(temp!=NULL){
if(l<i){
temp->data=l1[l++];
}else{
temp->data=l2[m++];
}
temp=temp->next;
}
return head;
}
node * remove_dup_unsorted(node *&head){
node *curr=head;
node*prev=NULL;
unordered_set<int>s;
while(curr!=NULL){
if(s.find(curr->data)==s.end()){
s.insert(curr->data);
prev=curr;
curr=curr->next;
}else{
prev->next=curr->next;
curr=curr->next;
}
}
return head;
}
node *reverse(node * &head){
int count=0;
node* curr=head;
node* next=NULL;
node* prev=NULL;
while(curr!=NULL ){
next=curr->next;
curr->next=prev;
prev=curr;
curr=next;
count++;
}
cout<<"the no of nodes are ...:"<<count<<endl;
return prev;
}
node * del_node_greateronright(node*&head){
head=reverse(head);
node *curr=head;
node*curnext=curr->next;
while(curnext!=NULL){
if(curr->data>curnext->data){
curr->next=curnext->next;
curnext=curr->next;
}else{
curr=curnext;
curnext=curnext->next;
}
}
node *curr1=reverse(head);
return curr1;
}
void insertdata(node* &head,int data1,int data2){
node *newnode=new node(data2);
node *temp=head;
while(temp->data!=data1 &&temp!=NULL){
temp=temp->next;
}
node* nextnode=temp->next;
temp->next=newnode;
newnode->next=nextnode;
}
int main(){
node*head=NULL;
insertattail(head,12);
insertattail(head,15);
insertattail(head,10);
insertattail(head,11);
insertattail(head,5);
insertattail(head,6);
display(head);
deleteatposition(head,3);
//insertattail(head,7);
//node *newnode=reverse(head);
//insertdata(head,5,7);
display(head);
//evenafterodd(head);
//display(head);
//node *newhead=reversegroupk(head,2);
//display(newhead);
//display(newhead);
return 0;
}