Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct nodo{int info; nodo* next; nodo(int a=0, nodo* b=0){info=a; next=b;}};
- nodo* build(nodo* L) {
- int x;
- cin>>x;
- if(x!=-1) {
- return new nodo(x,build(L));
- }
- else
- return 0;
- }
- //PRE=(L = L_0, L_1, ... è una lista di interi)
- int cerca(int k, nodo* L) {
- if(L==0)
- return -1;
- if(L->info==k)
- return 0;
- else {
- int x=1+cerca(k,L->next);
- if(x==0)
- return -1;
- else
- return x;
- }
- }
- /*POST=(ritorna il più piccolo indice i tale che L_i.chiave == k,
- oppure -1 se non esiste)*/
- int main() {
- cout<<"start"<<endl;
- nodo* L=build(L);
- int key;
- cin>>key;
- int pos=cerca(key,L);
- if(pos!=-1)
- cout<<"Il valore "<<key<<" si trova in posizione "<<pos<<endl;
- else
- cout<<"Il valore "<<key<<" non si trova nella lista"<<endl;
- cout<<"end"<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement