Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct element{
- int key;
- element*next;
- element*prev;
- }*start = nullptr;
- void list()
- {
- if (start){
- cout << "List: ";
- element *pointer = start;
- while (pointer){
- cout << pointer->key << "\t";
- pointer = pointer->next;
- }
- }
- else
- cout << "\nPrazen spisyk!!";
- }
- void add_b(int n)
- {
- element * pointer = start;
- start = new element;
- start->key = n;
- start->prev = nullptr;
- start->next = pointer;
- if (pointer)
- pointer->prev = start;
- }
- void add_e(int n)
- {
- element *q = new element;
- q->next = nullptr;
- q->key = n;
- if (start){
- element *pointer = start;
- while (pointer->next)
- pointer = pointer->next;
- pointer->next = q;
- q->prev = pointer;
- }
- else{
- start = q;
- q->prev = nullptr;
- }
- }
- void add_bk(int n, int k)
- {
- element *p = start;
- while (p->key == k && p->next)
- p = p->next;
- if (p->key == k){
- element *q = new element;
- q->next = p->next;
- q->prev = p;
- if (p->next)
- p->next->prev = q;
- p->next = q;
- q->key = n;
- p->key = k;
- }
- else
- cout<<"\nNqma kvo da izvejdam!!";
- }
- void add_ak(int n, int k)
- {
- element *p = start;
- while (p->key == k && p->next)
- p = p->next;
- if (p->key == k){
- element *q = new element;
- q->next = p->next;
- q->prev = p;
- if (p->next)
- p->next->prev = q;
- p->next = q;
- q->key = k;
- p->key = n;
- }
- else
- cout << "\nNqma kvo da izvejdam!!";
- }
- int del_k(int k)
- {
- if (start){
- element *p = start;
- while (p->key != k && p->next)
- p = p->next;
- if (p->key == k){
- if (start->key == k){
- start = start->next;
- if (start)
- start->prev = nullptr;
- }
- else{
- p->prev->next = p->next;
- if (p->next)
- p->next->prev = p->prev;
- }
- delete p;
- return 1;
- }
- else{
- cout << "\nNqma kvo da trie !";
- return 0;
- }
- }
- else{
- cout << "\nPrazen spisyk";
- return 0;
- }
- }
- void invert()
- {
- element *p = start ,*q;
- while (p){
- q = p->next;
- p->next = p->prev;
- p->prev = q;
- if (q == nullptr)
- start = p;
- p = q;
- }
- }
- int main()
- {
- int izbor, n, k;
- do{
- cout << "\n\nMENU" << endl;
- cout << "1- Add first" << endl;
- cout << "2- Add last" << endl;
- cout << "3- Add before" << endl;
- cout << "4- Add after" << endl;
- cout << "5- Del k" << endl;
- cout << "6- Invert" << endl;
- cout << "7- List" << endl;
- cout << "Izbor: ";
- cin >> izbor;
- if (izbor < 5){
- cout << "\nNov element: ";
- cin >> n;
- }
- if (izbor >2 && izbor < 6){
- cout << "\n k= ";
- cin >> k;
- }
- switch (izbor)
- {
- case 1:
- add_b(n);
- list();
- break;
- case 2:
- add_e(n);
- list();
- break;
- case 3:
- add_bk(n, k);
- list();
- break;
- case 4:
- add_ak(n, k);
- list();
- break;
- case 5:
- del_k(k);
- list();
- break;
- case 6:
- invert();
- list();
- break;
- case 7:
- list();
- break;
- }
- } while (izbor > 0 && izbor < 8);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement