-
Notifications
You must be signed in to change notification settings - Fork 0
/
hetrolist.cpp
202 lines (177 loc) · 3.27 KB
/
hetrolist.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
#include<iostream>
using namespace std;
struct node1;
struct node2;
struct node1 {
int data;
node2 * next;
node1(int data) {
this->data = data;
this->next = NULL;
}
};
struct node2 {
char data;
node1 * next;
node2(int data) {
this->data = data;
this->next = NULL;
}
};
class list {
node1 * head1;
node1 * start;
node2 * head2;
int n;
public:
list() {
start = NULL;
head1 = NULL;
head2 = NULL;
n = 0;
}
void insert_int(int ele) {
if (start == NULL) {
start = new node1(ele);
head1 = start;
cout << "\nhead 1 is created:" << head1->data;
n++;
return;
}
while (head1->next->next != NULL) {
head1 = head1->next->next;
}
node1 * newnode = new node1(ele);
head1->next->next = newnode;
head1 = newnode;
n++;
cout << "\nvalue inserted at last." << head1->data;
return;
}
void insert_char(char ele) {
if (start == NULL) {
cout << "\nnot possible...";
return;
}
if (start->next == NULL) {
head2 = new node2(ele);
cout << "\nhead 2 created:" << head2->data;
start->next = head2;
n++;
return;
}
while (head2->next->next != NULL) {
head2 = head2->next->next;
}
node2 * newnode = new node2(ele);
head2->next->next = newnode;
head2 = newnode;
n++;
cout << "\nelement inserted at last." << head2->data;
return;
}
void display() {
if (start == NULL) {
cout << "\nlist is empty....";
return;
}
cout << "\n";
cout << "\nn is:" << n << "\n";
node1 * ptr = start;
if (n % 2 == 0 && head2!=NULL) {
while (ptr != head2->next) {
cout << ptr->data;
cout << ptr->next->data;
ptr = ptr->next->next;
}
}
else {
while (ptr != head1) {
cout << ptr->data;
cout << ptr->next->data;
ptr = ptr->next->next;
}
cout << ptr->data;
}
return;
}
void deletion() {
node1 * ptr = start;
if (head1 == start) {
if (start->next == head2 && head2!=NULL) {
head2=NULL;
head1->next = NULL;
return;
}
cout << "\ndeletion not posible";
return;
}
else{
if (n % 2 != 0) {
node1 * temp = head1;
while (ptr->next->next != head1) {
ptr = ptr->next->next;
}
head1 = ptr;
head2 = ptr->next;
delete temp;
n--;
return;
}
else {
node2 * temp = head2;
while (ptr->next->next->next != head2) {
ptr = ptr->next->next;
}
head2 = ptr->next;
head1 = ptr->next->next;
delete temp;
n--;
return;
}
// else{
// cout<<"nothing";
// return;
}
}
void displayheads() {
cout << head1->data;
cout << head2->data;
}
};
int main() {
list l1;
l1.insert_int(9);
l1.insert_char('w');
l1.insert_int(8);
l1.insert_char('m');
l1.insert_int(7);
l1.display();
// 9w8m7
//cout << "\ndisplaying heads:";
//l1.displayheads();
l1.deletion();
cout << "\n";
l1.display();
//9w8m
l1.deletion();
cout << "\n";
//l1.displayheads();
//cout << "\n";
l1.display();
//9w8
l1.deletion();
cout << "\n";
l1.display();
//9w
l1.deletion();
cout << "\n";
l1.display();
//9
l1.deletion();
cout << "\n";
l1.display();
//not possible.
//l1.deletion();
return 0;
}