Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Napišimo program koji korisniku nudi izbornik u
- kojem može odabrati želi li unijeti element u listu,
- ispisati je ili brisati određeni element. Elementi se u
- listu dodaju na odgovarajuću poziciju tako da je lista u
- svakom trenutku sortirana uzlazno. */
- #include <iostream>
- using namespace std;
- struct node
- {
- int data;
- node *link;
- };
- void write(node *head);
- void input(node *&head, int elt);
- void deleteNode(node *&head, int elt);
- int main()
- {
- int izb, x;
- node *head=0;
- do
- {
- cout<<"\n1. unos elementa\n2. ispis elementa\n3. brisanje\n0. kraj"<<endl;
- cin>>izb;
- switch(izb)
- {
- case 1:
- cout<<"Unesite element u listu."<<endl;
- cin>>x;
- input(head,x);
- break;
- case 2:
- write(head);
- break;
- case 3:
- cout<<"Unesite element za brisanje."<<endl;
- cin>>x;
- deleteNode(head,x);
- break;
- case 0:
- cout<<"Kraj."<<endl;
- }
- }while(izb!=0);
- // DEALOKACIJA
- node *temp=head, *eraseNode;
- while(temp)
- {
- eraseNode=temp;
- temp=temp->link;
- delete eraseNote;
- }
- head=0;
- return 0;
- }
- void write(node *head)
- {
- while(head) // MOŽE I: while(head!=0)
- {
- cout<<head->data<<" ";
- head=head->link;
- }
- }
- void input(node *&head, int elt)
- {
- node *current=new node;
- current->data=elt;
- current->link=0;
- node *temp=head, *pre=0;
- while(temp && temp->data<elt)
- {
- pre=temp;
- temp=temp->link;
- }
- if(pre) //if(pre!=0)
- {
- current->link=temp;
- pre->link=current;
- }
- else {
- current->link = head;
- head=current;
- }
- }
- void deleteNode(node *&head, int elt)
- {
- node *temp=head, *pre=0;
- while(temp)
- {
- if(temp->data==elt)
- break;
- pre=temp;
- temp=temp->link;
- }
- if(temp)
- {
- if(pre==0)
- head=head->link;
- else
- pre->link=temp->link;
- delete temp;
- temp=0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment