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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
#include "pch.h"
#include <iostream>
using namespace std;
struct node {
int data;
struct node *next;
struct node *prev;
};
void create_list(node *&head, node *&tail, int num);
void display_dlist(node *head);
void reverse_display_dlist(node *head);
void insert_position_dlist(node *current, node *tail, int value, int position);
void insert_before_dlist(node *&head, int key);
void delete_element_dlist(node *&head, int key);
void delete_position_dlist(node *&head, int pos);
void delete_whole_dlist(node *&head);
void search_dlist(node *head, int key);
int Size = 0;
int main()
{
int choice, value, position;
node *head = NULL;
node *tail = NULL;
cout << "1.Sukuria dvikrypti sarasa (INSERTS A NODE)" << endl;
cout << "2.Spausdinimas nuo pradziu" << endl;
cout << "3.Spausdinimas nuo galo" << endl;
cout << "4.Iterpia nauja elementa i nurodyta saraso pozicija" << endl;
cout << "5.Iterpia elementa pries tam tikra elementa" << endl;
cout << "6.Istrina pasirinkta reiksme (temp->next nullptr" << endl;
cout << "7.Istrina pasirinkta pozicija" << endl;
cout << "8.Istrina visa sarasa" << endl;
cout << "9.Elemento paieskat" << endl;
while (1) {
cout << "Iveskite pasirinkima: ";
cin >> choice;
cout << endl;
switch (choice) {
case 1:
cout << "Iveskite elementa: ";
cin >> value;
create_list(head, tail, value);
cout << endl;
break;
case 2:
cout << "Sarasas is priekio- ";
display_dlist(head);
cout << endl;
break;
case 3:
cout << "Sarasas is galo- ";
reverse_display_dlist(head);
cout << endl;
break;
case 4:
cout << "Iveskite norima pozicija ";
cin >> position;
cout << endl;
cout << "Iveskite elementa ";
cin >> value;
insert_position_dlist(head, tail, value, position);
cout << endl;
break;
case 5:
cout << "Iveskite elementa pries kuri norite iterpti ";
int key;
cin >> key;
cout << endl;
insert_before_dlist(head, key);
break;
case 6:
cout << "Kuria reiksme norite istrinti ";
cin >> key;
cout << endl;
delete_element_dlist(head, key);
break;
case 7:
cout << "Kuria pozicija norite istrinti ";
cin >> position;
delete_position_dlist(head, position);
cout << endl;
break;
case 8:
cout << "Sarasas istrintas";
delete_whole_dlist(head);
cout << endl;
break;
case 9:
cout << "Kokio elemento ieskote? ";
cin >> key;
search_dlist(head, key);
cout << endl;
break;
default:
cout << "Tokio pasirinkimo nera" << endl;
break;
}
}
return 0;
}
void create_list(node *&head, node *&tail, int num) {
struct node *temp, *temp1;
temp = new node;
temp->data = num;
temp->next = NULL;
if (head == NULL) {
temp->prev = NULL;
head = temp;
}
else {
temp1 = head;
while (temp1->next != NULL)
temp1 = temp1->next;
temp1->next = temp;
temp->prev = temp1;
tail = temp;
}
Size++;
}
void display_dlist(node *head) {
node *temp = head;
if (temp == NULL) {
cout << "Sarasas tuscias" << endl;
return;
}
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void reverse_display_dlist(node *head) {
node *temp = head;
if (temp == NULL) {
cout << "Sarasas tuscias" << endl;
return;
}
while (temp->next != NULL) {
temp = temp->next;
}
while (temp != head) {
cout << temp->data << " ";
temp = temp->prev;
}
cout << temp->data << endl;
}
void insert_position_dlist(node *head, node *tail, int value, int position) { //kazkas su head insertion negerai
node *temp = new node;
node *temp1;
node *temp2;
temp->data = value;
if (position == 1 || head == NULL)
{
if (head == NULL && position == 1)
{
temp->prev = temp->next = NULL;
head = temp;
return;
}
head->prev = temp;
temp->prev = NULL;
temp->next = head;
head = temp;
return;
}
temp1 = head;
for (int i = 0; i < position - 2; i++)
{
temp1 = temp1->next;
}
temp->next = temp1->next;
temp->prev = temp1;
temp1->next = temp;
if (temp->next != NULL)
{
temp2 = temp->next;
temp2->prev = temp;
}
Size++;
}
void insert_before_dlist(node *&head, int key) {
node *temp = new node;
node *temp1 = head;
int num;
cout << "Iveskite elementa ";
cin >> num;
cout << endl;
temp->data = num;
while (temp1 != NULL) {
if (temp1->data == key) {
break;
}
temp1 = temp1->next;
if (temp1->data == key) {
break;
}
}
temp->prev = temp1->prev;
temp->next = temp1;
temp1->prev = temp;
if (temp->prev != NULL)
temp->prev->next = temp;
else {
head = temp;
}
Size++;
}
void delete_element_dlist(node *&head, int key) { //kazkodel pradejo neveikt EYOOOOO
node *temp;
temp = head;
if (temp == NULL)
{
cout << "Sarasas tuscias" << endl;
return;
}
else if (temp->data == key) //+++++++++++
{
head = temp->next;
delete temp;
}
else
{
while (temp->data != key)
temp = temp->next;
node* temp2 = NULL;
temp2 = temp->next;
while (temp->next->data != key)
{
temp = temp->next;
temp2 = temp->next;
}
temp->next = temp2->next;
temp2->prev = temp;
delete temp;
}
Size--;
}
void delete_position_dlist(node *&head, int pos) { // 1 element-crash
node *temp;
temp = head;
if (pos > Size + 1) {
cout << "Blogai ivesta pozicija" << endl;
return;
}
if (temp == NULL) {
cout << "Sarasas tuscias" << endl;
return;
}
if (temp->next == NULL) {
delete temp;
return;
}
for (int i = 1; i < pos&&temp != NULL; i++)
temp = temp->next;
if (pos == 1) {
temp = head;
head = head->next;
head->prev = NULL;
delete temp;
}
else if (temp->next == NULL) {
node *temp;
temp = head;
while (temp->next->next != NULL)
temp = temp->next;
delete temp->next;
temp->next = NULL;
}
else if (temp != NULL) {
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
delete temp;
}
Size--;
}
void delete_whole_dlist(node *&head) {
if (head == NULL) {
cout << "Sarasas jau yra tuscias" << endl;
return;
}
node *current = head;
node *temp;
while (current != NULL)
{
temp = current->next;
delete current;
current = temp;
}
head = NULL;
Size = 0;
}
void search_dlist(node *current, int key) {
int pos = 0;
if (current == NULL) {
cout << "Sarasas tuscias" << endl;
return;
}
cout << "Elemento pozicijos- ";
while (current != NULL) {
pos++;
if (current->data == key) {
cout << pos << " ";
}
if (current->next != NULL)
current = current->next;
else
break;
}
cout << endl;
}
|