Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Nadopuniti program koji će raditi s dvostruko povezanom
- listom koja ima definiran lažni prvi čvor i k tome je kružno
- povezana.
- Korisniku se nudi zadani izbornik, svaku od mogućnosti
- realizirati u zasebnoj funkciji (funkcija za dodavanje
- elementa u listu na odgovarajuću poziciju tako da je lista
- sortirana, funkcija za brisanje zadanog elementa liste,
- funkcija za ispis liste). */
- #include <iostream>
- #include <iomanip>
- using namespace std;
- struct node
- {
- int data;
- node *pre;
- node *post;
- };
- void izbornik();
- void create(node*& head);
- void add(node *&head);
- void deleteNode(node *head);
- void write(node *head);
- int main()
- {
- izbornik();
- cin.sync();
- cin.get();
- return 0;
- }
- void izbornik()
- {
- int odabir;
- node *head=0;
- do
- {
- cout<<"\n\n\nI Z B O R N I K\n";
- cout<<"1 - Kreiranje liste (samo lazni cvor)\n";
- cout<<"2 - Unos elemenata liste\n";
- cout<<"3 - Brisanje zadanog elementa liste\n";
- cout<<"4 - Ispis liste\n";
- cout<<"0 - K R A J\n";
- cout<<"\n\nUnesite Vas odabir...";
- cin>>odabir;
- switch(odabir)
- {
- case 1:
- if (head==NULL) create(head);
- break;
- case 2:
- if (head) add(head);
- else cout<<"Unos nije moguc!!! Lazni cvor nije kreiran...\n";
- break;
- case 3:
- if (head) deleteNode(head);
- else cout<<"Brisanje nije moguce!!! Lazni cvor nije kreiran...\n";
- break;
- case 4:
- if (head) write(head);
- else cout<<"Lista je prazna...\n";
- break;
- case 0:
- cout<<"\nKraj.\n";
- /*
- DEALOKACIJA
- */
- if (head)
- {
- node *current=head->post, *tmp;
- while (current!=head)
- {
- tmp=current;
- current=current->post;
- delete tmp;
- }
- delete head;
- head=0;
- }
- }
- }
- while(odabir!=0);
- }
- void create(node*& head)
- {
- head = new node;
- head->post = head;
- head->pre = head;
- cout<<"Kreiran je lazni node liste."<<endl;
- }
- //element liste dodaje se na poziciju tako da lista bude sortirana
- void add(node *&head)
- {
- int elt;
- char x;
- do
- {
- cout<<"Unesite element koji zelite dodati u listu...\n";
- cin>>elt;
- node *current=head->post, *newNode;
- newNode=new node;
- newNode->data=elt;
- while (current!=head)
- {
- if (elt<current->data) break;
- current=current->post;
- }
- newNode->post=current;
- newNode->pre=current->pre;
- current->pre->post=newNode;
- current->pre=newNode;
- cout<<"Zelite li dodati jos elemenata? (d/n) \n";
- cin>>x;
- }
- while(x=='d');
- }
- void write(node *head)
- {
- node *current=head->post;
- if (current==head)
- {
- cout<<"Lista je prazna...\n";
- return;
- }
- while (current!=head)
- {
- cout<<setw(5)<<current->data;
- current=current->post;
- }
- cout<<endl;
- }
- void deleteNode(node *head)
- {
- int elt;
- cout<<"Unesite element koji zelite brisati...";
- cin>>elt;
- node *current=head->post;
- while (current!=head && current->data<elt)
- current=current->post;
- if (current->data==elt)
- {
- current->pre->post=current->post;
- current->post->pre=current->pre;
- delete current;
- current=0;
- }
- else cout<<"Element nije pronadjen...\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement