Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct element {
- string ime;
- int broj;
- };
- struct node {
- element val;
- node *next, *prev;
- };
- struct DLCL {
- node *head, *tail;
- void init() {
- head = NULL;
- tail = NULL;
- }
- void addFirst(element e) {
- node *new_node = new node;
- new_node->val = e;
- new_node->next = NULL;
- new_node->prev = NULL;
- if(head == NULL) {
- head = new_node;
- head->next = head;
- head->prev = head;
- tail = head;
- }
- else {
- new_node->next = head;
- head->prev = new_node;
- new_node->prev = tail;
- head = new_node;
- tail->next = head;
- }
- }
- void addLast(element e) {
- node *new_node = new node;
- new_node->val = e;
- new_node->next = NULL;
- new_node->prev = NULL;
- if(head == NULL) {
- head = new_node;
- head->prev = head;
- head->next = head;
- tail = head;
- }
- else {
- tail->next = new_node;
- new_node->prev = tail;
- new_node->next = head;
- head->prev = new_node;
- tail = new_node;
- }
- }
- void deleteFirst() {
- if(head != NULL) {
- if(head -> next == head) {
- delete head;
- head = NULL;
- tail = NULL;
- }
- else {
- node *tmp = head;
- head = head->next;
- head->prev = tail;
- tail->next = head;
- delete tmp;
- }
- }
- }
- void deleteLast() {
- if(head != NULL) {
- if(head->next == head) {
- delete head;
- head = NULL;
- tail = NULL;
- }
- else {
- node *tmp = tail;
- tail = tail->prev;
- tail->next = head;
- head->prev = tail;
- delete tmp;
- }
- }
- }
- void print() {
- node *tmp = head;
- while(tmp != tail) {
- cout << tmp->val.ime << "," << tmp->val.broj << " <--> ";
- tmp = tmp->next;
- }
- cout << tmp->val.ime << "," << tmp->val.broj << " <--> ";
- }
- };
- void fja(DLCL &lista, int br1, int br2, int br3, int br4) {
- node *tmp = lista.head;
- while(tmp->val.broj != br1) {
- tmp = tmp->next;
- }
- node *S1 = tmp;
- node *tmps1 = S1;
- tmp = lista.head;
- while(tmp->val.broj != br2) {
- tmp = tmp->next;
- }
- node *S2 = tmp;
- DLCL fi, si;
- fi.init();
- si.init();
- while(S1 != S2) {
- fi.addLast(S1->val);
- S1 = S1->next;
- }
- fi.addLast(S1->val);
- S1 = tmps1;
- fi.print();
- cout << endl;
- tmp = lista.head;
- while(tmp->val.broj != br3) {
- tmp = tmp->next;
- }
- node *T1 = tmp;
- node *tmpt2 = T1;
- tmp = lista.head;
- while(tmp->val.broj != br4) {
- tmp = tmp->next;
- }
- node *T2 = tmp;
- while(T1 != T2) {
- si.addLast(T1->val);
- T1 = T1->next;
- }
- si.addLast(T1->val);
- T1 = tmpt2;
- si.print();
- cout << endl;
- DLCL res;
- res.init();
- tmp = lista.head;
- while(tmp != lista.tail) {
- if(tmp == S1) {
- while(si.head != NULL) {
- res.addLast(si.head->val);
- cout << si.head -> val.ime << "<-->";
- si.deleteFirst();
- }
- while(tmp != S2) {
- tmp = tmp->next;
- }
- tmp = tmp->next;
- }
- else if(tmp == T1) {
- while(fi.head != NULL) {
- res.addLast(fi.head->val);
- cout << fi.head -> val.ime << "<-->";
- fi.deleteFirst();
- }
- while(tmp != T2) {
- tmp = tmp->next;
- }
- tmp = tmp->next;
- }
- else {
- res.addLast(tmp->val);
- cout << tmp->val.ime << "<-->";
- tmp = tmp->next;
- }
- }
- if(tmp == lista.tail) {
- res.addLast(tmp->val);
- cout << tmp->val.ime<<endl;
- }
- // res.print();
- }
- int main()
- {
- DLCL dl;
- dl.init();
- element e;
- e.ime = "Luka";
- e.broj = 5;
- dl.addLast(e);
- e.ime = "Ana";
- e.broj = 1;
- dl.addLast(e);
- e.ime = "Petar";
- e.broj = 2;
- dl.addLast(e);
- e.ime = "Marko";
- e.broj = 6;
- dl.addLast(e);
- e.ime = "Mia";
- e.broj = 4;
- dl.addLast(e);
- e.ime = "Tea";
- e.broj = 8;
- dl.addLast(e);
- e.ime = "Boban";
- e.broj = 10;
- dl.addLast(e);
- fja(dl, 1, 6, 4, 8);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement