Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cassert>
  4. using namespace std;
  5.  
  6. class Node;
  7. class List;
  8.  
  9. class Iterator
  10.  {public:
  11.    Iterator();
  12.    string get() const;
  13.    Iterator next()const;
  14.    Iterator previous()const;
  15.    bool equals(Iterator b) const;
  16.    bool is_null() const;
  17.   private:
  18.    Node* position;
  19.   friend class List;
  20.  };
  21.  
  22. class List
  23. {public:
  24.    List();
  25.    void push_back(string s);
  26.    void insert(Iterator p, string s);
  27.    void erase(Iterator p);
  28.    bool presente();
  29.    bool empty() const;
  30.    Iterator begin();
  31.    Iterator end();
  32.  private:
  33.    Node* first;
  34.    Node* last;
  35. };
  36.  
  37. class Node
  38.  {public:
  39.    Node(string s);
  40.   private:
  41.    string data;
  42.    Node* prec;
  43.    Node* succ;
  44.   friend class List;
  45.   friend class Iterator;
  46.  };
  47.  
  48. Node::Node(string s)
  49.   {data = s;
  50.    prec = NULL;
  51.    succ = NULL;
  52.   }
  53.  
  54. Iterator::Iterator()
  55.   {position = NULL;
  56.   }
  57.  
  58. string Iterator::get() const
  59.   {
  60. assert(position != NULL);
  61.    return position->data;
  62.   }
  63.  
  64. bool Iterator::equals(Iterator b) const
  65.   {
  66.    return position == b.position;
  67.   }
  68.  
  69.  
  70. Iterator Iterator::next() const
  71.  
  72. {
  73. assert(position != NULL);
  74.  
  75.  Iterator t;
  76.  
  77.  t.position = position->succ;
  78.  
  79.  return t;
  80.  
  81. }
  82.  
  83.  
  84.  
  85.  
  86. Iterator Iterator::previous() const
  87.  
  88. {
  89.   assert(position != NULL);
  90.  
  91.     Iterator t;
  92.  
  93.     t.position = position->prec;
  94.  
  95.     return t;
  96.  
  97. }
  98.  
  99.  
  100. bool Iterator::is_null() const
  101.   {return position==NULL;
  102.   }
  103.  
  104. List::List()
  105.   {first = NULL;
  106.    last = NULL;
  107.   }
  108.  
  109. bool List::empty() const
  110.   { return first==NULL; }
  111.  
  112. Iterator List::begin()
  113.   {Iterator i;
  114.    i.position = first;
  115.    return i;
  116.   }
  117.  
  118. Iterator List::end()
  119.   {Iterator i;
  120.    i.position = last;
  121.    return i;
  122.   }
  123.  
  124. void List::push_back(string s)
  125. {Node* n = new Node(s);
  126.    if (last == NULL)
  127.   {first = n;
  128.    last = n;
  129.   }
  130. else  {n->prec = last;
  131.         last->succ = n;
  132.        last= n;
  133.       }
  134. }
  135.  
  136. void List::insert(Iterator p, string s)
  137. {if (empty())
  138.     push_back(s);
  139.  else {Node* n = new Node(s);
  140.        Node* dopo = p.position;
  141.        Node* prima = dopo->prec;
  142.        n->prec = prima;
  143.        n->succ = dopo;
  144.        dopo->prec = n;
  145.        if (prima==NULL)
  146.           first = n;
  147.        else prima->succ = n;
  148.       }
  149. }
  150.  
  151. void List::erase(Iterator p)
  152. {assert (p.position != NULL);
  153.  Node* rimuovi = p.position;
  154.  Node* dopo = rimuovi->succ;
  155.  Node* prima = rimuovi->prec;
  156.  if (prima==NULL)
  157.     first = dopo;
  158.  else prima->succ = dopo;
  159.  if (rimuovi==last)
  160.   last = prima;
  161.  else dopo->prec = prima;
  162.  delete rimuovi;
  163. }
  164.  
  165. const int N = 4;
  166.  
  167. List prefisso(List a[N],const int N,string s)
  168. {
  169.     Iterator p;
  170.     List aus;
  171.     int i,l1=s.length();
  172.  
  173.     for(i=0;i<N;i++)
  174.     {
  175.         for(p=a[i].begin();!p.is_null();p=p.next())
  176.         {
  177.             string s1=p.get();
  178.             if(s1.substr(0,l1)==s)
  179.             {
  180.                 aus.push_back(s1);
  181.             }
  182.         }
  183.  
  184.     }
  185.     return aus;
  186. }
  187.  
  188. int main()
  189. {
  190.     List risultato;
  191.     string s="io";
  192.     int m,i,j;
  193.     string nome, name;
  194.     List a[N];
  195.  
  196.     for( i=0;i<N;i++)
  197.     {
  198.         cout<<"Inserisci Numero membri\n";
  199.         cin>>m;
  200.         for(j=0;j<m;j++)
  201.         {
  202.             cout<<"Inserisci Nome membro \n";
  203.             cin>>nome;
  204.             a[i].push_back(nome.c_str());
  205.         }
  206.     }
  207.  
  208.     risultato=prefisso (a,s);
  209.     Iterator pos;
  210.  
  211.     if(!risultato.empty())
  212.     {
  213.         cout<<"Le stringhe della lista con prefisso " << s << " sono : ";
  214.  
  215.         for(pos=risultato.begin();!pos.is_null();pos=pos.next())
  216.         cout<<pos.get()<<endl;
  217.     }
  218.  
  219. return 0;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement