Doubly-linked List Help

through the list in reverse and the deletion of nodes

Page 1 of 1

4 Replies - 2265 Views - Last Post: 27 October 2010 - 05:37 AM Rate Topic: -----

#1 giancarlocili   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-October 10

Doubly-linked List Help

Posted 27 October 2010 - 04:58 AM

I need help with this program, I get to do I delete a node and show me the list in backwards.

#include<iostream>
#include<fstream>
using namespace std;

struct _agenda{
       char nombre[20],direccion[20];
       char telefono_movil[20], telefono_fijo[20];
       struct _agenda *siguiente,*anterior,;      
};
int nposicion;
struct _agenda *primero , *ultimo;

void mostrar_menu(){
     system("cls");
     cout<<"Menu del sistema\n";
     cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
     cout<<"[1]. Agragar nodo\n";
     cout<<"[2]. Mostrar lista\n";
     cout<<"[3]. Insertar nodo\n";
     cout<<"[4]. muestra al recorrido inverso\n";
     cout<<"[5]. eliminar nodo\n";
     cout<<"[6]. Salir sistema\n";
     cout<<"\nDigite la opcion: ";     
}

void mostrar_lista(){
     struct _agenda *aux;
     int i;
     i=0;
     aux=primero;
     cout<<"\nMostrar lista\n";
     cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
     while(aux!=NULL){
          cout<<"\npara la posicion "<<i+1<<" \n";
          cout<<"\nNombre: "<<aux->nombre;
          cout<<"\nNumero movil: "<<aux->telefono_movil;
          cout<<"\nNumero fijo: "<<aux->telefono_fijo;
          cout<<"\nDireccon: "<<aux->direccion;
          cout<<"\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n";
          aux=aux->siguiente;
          i++;
     }
     if(i==0){
        cout<<"\nLalista esta vacia\n";
     }
     system("pause");
}

void mostrar_lista_inversa(){
     struct _agenda *aux;
     int i;
     i=0;
     aux=ultimo;
     cout<<"\nMostrar lista\n";
     cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
     while(aux!=NULL){
          cout<<"\npara la posicion "<<i+1<<" \n";
          cout<<"\nNombre: "<<aux->nombre;
          cout<<"\nNumero movil: "<<aux->telefono_movil;
          cout<<"\nNumero fijo: "<<aux->telefono_fijo;
          cout<<"\nDireccon: "<<aux->direccion;
          cout<<"\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n";
          aux=aux->anterior;
          i--;
     }
     if(i==0){
        cout<<"\nLalista esta vacia\n";
     }
     system("pause");
}

void agregar_elemento(){
     struct _agenda *nuevo;
     nuevo=(struct _agenda*) malloc(sizeof(struct _agenda));
     
     if(nuevo==NULL){
        cout<<"\nNo hay sufuciente memoria";
     }
     else{
         cout<<"\nDigite el nombre: ";
         cin>>nuevo->nombre;
         cout<<"\nDigite el numero movil: ";
         cin>>nuevo->telefono_movil;
         cout<<"\nDigite el numero fijo: ";
         cin>>nuevo->telefono_fijo;
         cout<<"\nDigite el direccion: ";
         cin>>nuevo->direccion;
         nuevo->anterior=NULL;
         nuevo->siguiente=NULL;
     }
     if(primero==NULL){
        primero=nuevo;
        ultimo=nuevo;
     }
     else{
     nuevo->anterior=ultimo;
     ultimo->siguiente=nuevo;
     ultimo=nuevo;
     }
}

void insertar_nodo(){
     int i=1;
     struct _agenda *nuevo2,*aux2;
     nuevo2=(struct _agenda*) malloc(sizeof(struct _agenda));
     aux2=primero;
     if(nuevo2==NULL){
        cout<<"\nNo hay sufuciente memoria";
     }
     else{
         cout<<"\nDigite la posicion: "; 
         cin>>nposicion;
         cout<<"\nDigite el nombre: ";
         cin>>nuevo2->nombre;
         cout<<"\nDigite el numero movil: ";
         cin>>nuevo2->telefono_movil;
         cout<<"\nDigite el numero fijo: ";
         cin>>nuevo2->telefono_fijo;
         cout<<"\nDigite el direccion: ";
         cin>>nuevo2->direccion;
         nuevo2->siguiente=NULL;
     }
     
     while(aux2!=NULL){
           if((i+1)==nposicion){
              nuevo2->siguiente=aux2->siguiente;
              aux2->siguiente=nuevo2;
           }
           aux2=aux2->siguiente;             
     i++;
     }    
}

void eliminar(){
      int i=0;
     struct _agenda *nuevo3,*aux3;
     nuevo3=(struct _agenda*) malloc(sizeof(struct _agenda));
     aux3=primero;
     if(nuevo3==NULL){
        cout<<"\nNo hay sufuciente memoria";
     }
     else{
         cout<<"\nDigite la posicion: "; 
         cin>>nposicion;
                  
         while(aux3!=NULL){
              if((i+1)==nposicion){
              
              aux3->anterior->siguiente=aux3->siguiente;
              delete (aux3);
              
           }
           aux3=aux3->siguiente;             
           i++;
         }    
         nuevo3->siguiente=NULL;
         
     }
     
    
         
}
int main(){
    system("color 1f");
    int opc;
    primero=(struct _agenda*)NULL;
    do{
       mostrar_menu();
       cin>>opc;
       switch(opc){
                case 1:
                     agregar_elemento();
                     system("pause");
                     break;
                
                case 2:
                     mostrar_lista();
                     break;
                
                case 3:
                     insertar_nodo();
                     system("pause");
                     break;
                
                case 4:
                     mostrar_lista_inversa();
                     break;
                
                case 5:
                     eliminar();
                     break;
                     
                case 6:
                     break;
                     
                default:
                        cout<<"invalido\n";           
       }
    }while(opc!=6);
    return 0;
}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 27 October 2010 - 05:08 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Doubly-linked List Help

#2 PhunkRabbit   User is offline

  • D.I.C Regular
  • member icon

Reputation: 81
  • View blog
  • Posts: 406
  • Joined: 23-August 10

Re: Doubly-linked List Help

Posted 27 October 2010 - 05:02 AM

Please use code tags.

[code ] [/ code] - without the spaces.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Doubly-linked List Help

Posted 27 October 2010 - 05:10 AM

You need to tell us exactly the problems you are having. Are you getting errors? Post them exactly as they appear. Is it not working like you want? Give examples.

Help US to help YOU.
Was This Post Helpful? 0
  • +
  • -

#4 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: Doubly-linked List Help

Posted 27 October 2010 - 05:25 AM

English dude... English. :)

Please always post your like this :code:

btw, in function void eliminar() you are supposed to delete a node. So why you are allocating memory to nuevo3?

From the kind of code you have I am guessing it's a doubly linked list. In your delete function you have a problem in deletion logic. You are going till the node next to one you want to delete and then marking it for deletion.

So, here is what could be the way to go for it. It might need a bit of change as I wrote it on the fly.

If it's a doubly linked list it could be.
Take position of node to delete.
1. If it's 1 then we are up to delete head node goto step 6 else continue to next step
2. Have a loop that moves upto the node to delete (or end of linked list to end up with no deletion, invalid position entered).
3. now we are at node n. so set n's prev's next pointer as n's next pointer.
4. If n's next is not NULL, set n's next's prev pointer as n's prev pointer.
5. now we have a list connected with n-1 and n+1 linked to each other move to step 9
6. assign a head pointer to n.
7. set head pointer to head's next pointer.
8. set head's prev pointer to NULL.
9. delete n.
10. return.

For reverse printing you can do...
You can do it in 2 ways, one is using recursion and the other is using loops.

In recursion... (you accept node pointer as parameter to function)
1. check the node's next is NULL if yes print all information of node and return, else continue.
2. call this same function with parameter as this node's next.
3. print all information of this node.

In loops...
1. assign head to some other pointer say n.
2. use a loop to take this pointer till the last node (till n's next is not NULL)
3. set another loop that traverses back (setting n to n's prev every iteration) till first node (n's prev is not NULL)
3.a inside loop print info about n.
4. return.

I hope this will help you. :)

This post has been edited by AmitTheInfinity: 27 October 2010 - 05:49 AM

Was This Post Helpful? 1
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Doubly-linked List Help

Posted 27 October 2010 - 05:37 AM

One thing I noticed is this code:
cout<<"\nDigite el nombre: ";
cin>>nuevo->nombre;
cout<<"\nDigite el numero movil: ";
cin>>nuevo->telefono_movil;
cout<<"\nDigite el numero fijo: ";
cin>>nuevo->telefono_fijo;
cout<<"\nDigite el direccion: ";
cin>>nuevo->direccion;



should be

cout<<"\nDigite el nombre: ";
cin.getline(nuevo->nombre, sizeof(nuevo->nombre));
cout<<"\nDigite el numero movil: ";
cin.getline(nuevo->telefono_movil, sizeof(nuevo->telefono_movil));
cout<<"\nDigite el numero fijo: ";
cin.getline(nuevo->telefono_fijo, sizeof(nuevo->telefono_fijo));
cout<<"\nDigite el direccion: ";
cin.getline(nuevo->direccion, sizeof(nuevo->direccion));



so you can accept strings with whitespace.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1