Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct nodo {
- int valor;
- nodo* ant;
- nodo* sig;
- };
- int main( ) {
- int n;
- std::cin >> n;
- nodo* ini = nullptr;
- nodo* ult = nullptr;
- for (int i=0 ;i<n;++i)
- {
- int valor;
- cin>>valor;
- nodo *p= new nodo{valor, nullptr, nullptr};
- if(ini==nullptr)
- {
- ini =p;
- ult=p;
- }
- else{
- ult->sig=p;
- p-> ant=ult;
- ult =p;
- }
- }
- for (;;) {
- int borrar; // leemos el valor a buscar y borrar
- std::cin >> borrar;
- for (nodo* p = ini; p != nullptr; p = p->sig) {
- if (p->valor == borrar) {
- if (p->ant != nullptr) {
- p->ant->sig = p->sig;
- }
- if (p->sig != nullptr) {
- p->sig->ant = p->ant;
- }
- if (ini == p) {
- ini = p->sig;
- }
- if(ult ==p)
- {
- ult=p->ant;
- }
- delete p;
- break;
- }
- }
- for (nodo* p = ini; p != nullptr; p = p->sig) {
- std::cout << p->valor << " ";
- }
- std::cout << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement