Advertisement
Glaas2

Lista_din

Jun 26th, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct nodo {
  6.    int valor;
  7.    nodo* ant;
  8.    nodo* sig;
  9. };
  10.  
  11. int main( ) {
  12.    int n;
  13.    std::cin >> n;
  14.  
  15.     nodo* ini = nullptr;
  16.     nodo* ult = nullptr;
  17.     for (int i=0 ;i<n;++i)
  18.     {
  19.         int valor;
  20.         cin>>valor;
  21.         nodo *p= new nodo{valor, nullptr, nullptr};
  22.         if(ini==nullptr)
  23.         {
  24.             ini =p;
  25.             ult=p;
  26.         }
  27.         else{
  28.             ult->sig=p;
  29.             p-> ant=ult;
  30.             ult =p;
  31.         }
  32.     }
  33.  
  34.    for (;;) {
  35.       int borrar;    // leemos el valor a buscar y borrar
  36.       std::cin >> borrar;
  37.  
  38.       for (nodo* p = ini; p != nullptr; p = p->sig) {
  39.          if (p->valor == borrar) {
  40.             if (p->ant != nullptr) {
  41.                p->ant->sig = p->sig;
  42.             }
  43.             if (p->sig != nullptr) {
  44.                p->sig->ant = p->ant;
  45.             }
  46.             if (ini == p) {
  47.                ini = p->sig;
  48.             }
  49.             if(ult ==p)
  50.             {
  51.                 ult=p->ant;
  52.             }
  53.             delete p;
  54.             break;
  55.          }
  56.       }
  57.  
  58.       for (nodo* p = ini; p != nullptr; p = p->sig) {
  59.          std::cout << p->valor << " ";
  60.       }
  61.       std::cout << "\n";
  62.    }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement