-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbCLLswap.cpp
More file actions
157 lines (148 loc) · 2.62 KB
/
dbCLLswap.cpp
File metadata and controls
157 lines (148 loc) · 2.62 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
#include<iostream>
using namespace std;
struct node
{
int data;
node*prev,*next;
};
node *first,*temp,*ttemp,*p;
void addnode()
{
ttemp=new node;
ttemp->next=ttemp->prev=nullptr;
cin>>ttemp->data;
if(first==nullptr)
{first=ttemp;
ttemp->next=ttemp->prev=first;}
else{
temp->next=ttemp;
ttemp->prev=temp;
ttemp->next=first;
first->prev=ttemp;
}
temp=ttemp;
}
void swapFS()
{
temp=first->prev;
ttemp=first->next;
p=ttemp->next;
ttemp->prev=temp;
ttemp->next=first;
first->prev=ttemp;
first->next=p;
p->prev=first;
temp->next=ttemp;
first=ttemp;
}
void swapL2L()
{
temp=first->prev;
ttemp=temp->prev;
p=ttemp->prev;
p->next=temp;
temp->prev=p;
temp->next=ttemp;
ttemp->prev=temp;
ttemp->next=first;
first->prev=ttemp;
}
void swapFL()
{
temp=first->prev;
ttemp=temp->prev;
p=first->next;
ttemp->next=first;
first->prev=ttemp;
first->next=temp;
temp->prev=first;
temp->next=p;
p->prev=temp;
first=temp;
}
void swapMN(node*&head,int m,int n)
{
if(!head||(head->next==head&&head->prev==head))
return;
if(m==n)
return;
if(m>n)swap(m,n);
node*mnode=head;
node*nnode=head;
int count = 1;
node* temp = head;
while (temp->next != head)
{
count++;
temp = temp->next;
}
if (m > count || n > count)
{
cout << "Invalid positions: m or n out of bounds\n";
return;
}
//find mth and nth node
for(int i=1;i<m;i++)mnode=mnode->next;
for(int i=1;i<n;i++)nnode=nnode->next;
//adjacent cases
if(mnode->next==nnode)
{
node*mbefore=mnode->prev;
node*nafter=nnode->next;
mbefore->next=nnode;
nnode->prev=mbefore;
nnode->next-mnode;
mnode->prev=nnode;
mnode->next=nafter;
nafter->prev=mnode;
if(mnode==head)head=nnode;
}
else
{
node* mbefore = mnode->prev;
node* mafter = mnode->next;
node* nbefore = nnode->prev;
node* nafter = nnode->next;
mbefore->next=nnode;
nbefore->next=mnode;
mafter->prev=nnode;
nafter->prev=mnode;
swap(mnode->next,nnode->next);
swap(mnode->prev,nnode->prev);
if(mnode==head)head=nnode;
else if(nnode==head)head=mnode;
}
}
void disp()
{
temp=first;
do
{
cout<<temp->data<<"\n";
temp=temp->next;
}while(temp!=first);
}
int main()
{
first=temp=ttemp=nullptr;
int n;
cout<<"enter n: ";
cin>>n;
for(int i=0;i<n;i++)
addnode();
cout<<"\n";
disp();
cout<<"\n";
swapFS();
disp();
cout<<"\n";
swapL2L();
disp();
cout<<"\n";
swapFL();
disp();
cout<<"\n";
swapMN(first,3,5);
disp();
cout<<"\n";
}