Practica para finales de Mayo
Hola necesito ayuda con esta practica, a ver si me podeis ayudar la necesito para la ultima semana de Mayo
Gracias
ENUNCIADO:
1. Definir la estructura de datos oportuna para implementar una lista que contiene dos datos. Un numero entero ID y otro campo que es un carácter LE. Queremos además, que cada elemento de la lista disponga de un enlace al siguiente elemento de la misma que le corresponde según un orden numérico ascendente de ID. Por otra parte deberá poseer otro campo que enlazará con el siguiente elemento de la lista según un orden alfabético del campo LE.
2. Módulo que, dado un código numérico, ya leído devuelva el carácter asociado, si dicho código existía en la lista y error en caso contrario.
3. Módulo que, dado un ID numérico y un carácter, ambos ya leídos, intercale un nuevo nodo en el lugar correspondiente de ambas listas, o devuelva error si al menos una de las claves ya existe en la lista.
4. Módulo que, dado un numero entero, devuelva la posición que ocupa en la lista de enteros.
5. Ídem al anterior pero con el campo LE.
6. Módulo que borre un elemento de la lista identificándolo por el ID.
Gracias
ENUNCIADO:
1. Definir la estructura de datos oportuna para implementar una lista que contiene dos datos. Un numero entero ID y otro campo que es un carácter LE. Queremos además, que cada elemento de la lista disponga de un enlace al siguiente elemento de la misma que le corresponde según un orden numérico ascendente de ID. Por otra parte deberá poseer otro campo que enlazará con el siguiente elemento de la lista según un orden alfabético del campo LE.
2. Módulo que, dado un código numérico, ya leído devuelva el carácter asociado, si dicho código existía en la lista y error en caso contrario.
3. Módulo que, dado un ID numérico y un carácter, ambos ya leídos, intercale un nuevo nodo en el lugar correspondiente de ambas listas, o devuelva error si al menos una de las claves ya existe en la lista.
4. Módulo que, dado un numero entero, devuelva la posición que ocupa en la lista de enteros.
5. Ídem al anterior pero con el campo LE.
6. Módulo que borre un elemento de la lista identificándolo por el ID.
Esta pregunta ya aparecio en el foro.
La respuesta es:
// program DupList.cpp
// written in Borland C++ ver 4.5
// lista con header node, cada nodo tiene links a dos campos distintos.
// sin usar clases, a pedido de danmificado.
#include <iostream.h>
#include <iomanip.h>
const int N = 10;
struct Node
{
int id;
char info;
Node *next_id,
*next_info;
}; // STRUCT NODE
void ShowNode(Node *p)
{
if(!p)
cout << "node not found" << endl;
else
cout << setw(4) << p->id << ":" << p->info;
} // SHOW NODE
void ShowById(Node *first)
{
cout << " Show by Id : ";
for(Node *p = first->next_id;p;p = p->next_id)
ShowNode(p);
cout << endl;
} // SHOW BY ID
void ShowByInfo(Node *first)
{
cout << "Show by Info : ";
for(Node *p = first->next_info;p;p = p->next_info)
ShowNode(p);
cout << endl;
} // SHOW BY ID
Node *InitNode(int id,char info)
{
Node *p = new Node;
p->id = id;
p->info = info;
p->next_id = p->next_info = NULL;
return p;
} // INIT NODE
Node *GetNextId(Node *first,int value,int &found)
{
Node *ret = first;
found = 0;
while(ret->next_id && ret->next_id->id <= value)
{
if(ret->next_id->id == value)
{
found = 1;
break;
}
ret = ret->next_id;
}
return ret;
} // GET NEXT ID
Node *GetNextInfo(Node *first,char info)
{
Node *ret = first;
while(ret->next_info && ret->next_info->info < info)
ret = ret->next_info;
return ret;
} // GET NEXT INFO
Node *Insert(Node *anterior,int id,char info)
{
Node *actual = InitNode(id,info);
actual->next_id = anterior->next_id;
anterior->next_id = actual;
return actual;
} // INSERT
void Link(Node *anterior,Node *actual)
{
actual->next_info = anterior->next_info;
anterior->next_info = actual;
} // LINK
void ListBuilder(Node *first)
{
Node a[N] = {{5,'a'},{12,'x'},{35,'c'},{17,'w'},{37,'z'},
{5,'B'},{25,'a'},{22,'y'},{75,'d'},{97,'e'}};
int found;
for(int i = 0;i < N;i++)
{
Node *anterior = GetNextId(first,a[i].id,found);
if(found)
{
cout << "error : id duplicado" << endl;
continue;
}
Node *actual = Insert(anterior,a[i].id,a[i].info);
anterior = GetNextInfo(first,a[i].info);
Link(anterior,actual);
}
} // LIST BUILDER
Node *SearchById(Node *first,int id)
{
cout << endl << "searching id = " << id << " ---> ";
for(Node *ret = first->next_id;ret;ret = ret->next_id)
if(ret->id == id)
return ret;
return NULL;
} // SEARCH BY ID
void InsertNode(Node *first,int id,char info)
{
cout << endl << "insert node ---> " << id << " : " << info << endl;
int found;
Node *anterior = GetNextId(first,id,found);
if(found)
{
cout << "error : id duplication" << endl;
return;
}
Node *actual = Insert(anterior,id,info);
anterior = GetNextInfo(first,info);
Link(anterior,actual);
cout << endl;
} // INSERT NODE
void DeleteNode(Node *first,int id)
{
cout << endl << "delete node for id : " << id << endl;
int found;
Node *anterior = GetNextId(first,id,found);
if(!found)
{
cout << "id number not found" << endl << endl;
return;
}
Node *actual = anterior->next_id;
anterior->next_id = actual->next_id;
Node *p = GetNextInfo(first,actual->info);
p->next_info = actual->next_info;
delete actual;
} // DELETE NODE
int IdIndex(Node *first,int id)
{
int counter = 0,found = 0;
Node *p = first->next_id;
while(p && !found)
{
counter++;
if(p->id == id)
found = counter;
p = p->next_id;
}
return found;
} // ID INDEX
int InfoIndex(Node *first,char info)
{
int counter = 0,found = 0;
Node *p = first->next_info;
while(p && !found)
{
counter++;
if(p->info == info)
found = counter;
p = p->next_info;
}
return found;
} // ID INDEX
void SearchId(Node *first,int id)
{
cout << "index for id " << setw(3) << id << " = ";
int index = IdIndex(first,id);
if(index)
cout << index << endl;
else
cout << "not found" << endl;
} // SEARCH ID
void SearchInfo(Node *first,char info)
{
cout << "index for info " << info << " = ";
int index = InfoIndex(first,info);
if(index)
cout << index << endl;
else
cout << "not found" << endl;
} // SEARCH INFO
void ErraseList(Node *first) // librar memoria retenida po la lista
{
Node *p = first;
while(p)
{
Node *q = p->next_id;
delete p;
p = q;
}
} // ERRASE LIST
void main()
{
Node *first = InitNode(0,'#');
ListBuilder(first);
ShowById(first);
ShowByInfo(first);
ShowNode(SearchById(first,5));
ShowNode(SearchById(first,55));
InsertNode(first,1,'g');
ShowById(first);
ShowByInfo(first);
DeleteNode(first,75);
DeleteNode(first,7);
ShowById(first);
ShowByInfo(first);
cout << endl;
SearchId(first,12);
SearchId(first,121);
SearchInfo(first,'z');
SearchInfo(first,'A');
cout << endl << "end of program - good bye ! ! !" << endl;
ErraseList(first);
} // MAIN
La respuesta es:
// program DupList.cpp
// written in Borland C++ ver 4.5
// lista con header node, cada nodo tiene links a dos campos distintos.
// sin usar clases, a pedido de danmificado.
#include <iostream.h>
#include <iomanip.h>
const int N = 10;
struct Node
{
int id;
char info;
Node *next_id,
*next_info;
}; // STRUCT NODE
void ShowNode(Node *p)
{
if(!p)
cout << "node not found" << endl;
else
cout << setw(4) << p->id << ":" << p->info;
} // SHOW NODE
void ShowById(Node *first)
{
cout << " Show by Id : ";
for(Node *p = first->next_id;p;p = p->next_id)
ShowNode(p);
cout << endl;
} // SHOW BY ID
void ShowByInfo(Node *first)
{
cout << "Show by Info : ";
for(Node *p = first->next_info;p;p = p->next_info)
ShowNode(p);
cout << endl;
} // SHOW BY ID
Node *InitNode(int id,char info)
{
Node *p = new Node;
p->id = id;
p->info = info;
p->next_id = p->next_info = NULL;
return p;
} // INIT NODE
Node *GetNextId(Node *first,int value,int &found)
{
Node *ret = first;
found = 0;
while(ret->next_id && ret->next_id->id <= value)
{
if(ret->next_id->id == value)
{
found = 1;
break;
}
ret = ret->next_id;
}
return ret;
} // GET NEXT ID
Node *GetNextInfo(Node *first,char info)
{
Node *ret = first;
while(ret->next_info && ret->next_info->info < info)
ret = ret->next_info;
return ret;
} // GET NEXT INFO
Node *Insert(Node *anterior,int id,char info)
{
Node *actual = InitNode(id,info);
actual->next_id = anterior->next_id;
anterior->next_id = actual;
return actual;
} // INSERT
void Link(Node *anterior,Node *actual)
{
actual->next_info = anterior->next_info;
anterior->next_info = actual;
} // LINK
void ListBuilder(Node *first)
{
Node a[N] = {{5,'a'},{12,'x'},{35,'c'},{17,'w'},{37,'z'},
{5,'B'},{25,'a'},{22,'y'},{75,'d'},{97,'e'}};
int found;
for(int i = 0;i < N;i++)
{
Node *anterior = GetNextId(first,a[i].id,found);
if(found)
{
cout << "error : id duplicado" << endl;
continue;
}
Node *actual = Insert(anterior,a[i].id,a[i].info);
anterior = GetNextInfo(first,a[i].info);
Link(anterior,actual);
}
} // LIST BUILDER
Node *SearchById(Node *first,int id)
{
cout << endl << "searching id = " << id << " ---> ";
for(Node *ret = first->next_id;ret;ret = ret->next_id)
if(ret->id == id)
return ret;
return NULL;
} // SEARCH BY ID
void InsertNode(Node *first,int id,char info)
{
cout << endl << "insert node ---> " << id << " : " << info << endl;
int found;
Node *anterior = GetNextId(first,id,found);
if(found)
{
cout << "error : id duplication" << endl;
return;
}
Node *actual = Insert(anterior,id,info);
anterior = GetNextInfo(first,info);
Link(anterior,actual);
cout << endl;
} // INSERT NODE
void DeleteNode(Node *first,int id)
{
cout << endl << "delete node for id : " << id << endl;
int found;
Node *anterior = GetNextId(first,id,found);
if(!found)
{
cout << "id number not found" << endl << endl;
return;
}
Node *actual = anterior->next_id;
anterior->next_id = actual->next_id;
Node *p = GetNextInfo(first,actual->info);
p->next_info = actual->next_info;
delete actual;
} // DELETE NODE
int IdIndex(Node *first,int id)
{
int counter = 0,found = 0;
Node *p = first->next_id;
while(p && !found)
{
counter++;
if(p->id == id)
found = counter;
p = p->next_id;
}
return found;
} // ID INDEX
int InfoIndex(Node *first,char info)
{
int counter = 0,found = 0;
Node *p = first->next_info;
while(p && !found)
{
counter++;
if(p->info == info)
found = counter;
p = p->next_info;
}
return found;
} // ID INDEX
void SearchId(Node *first,int id)
{
cout << "index for id " << setw(3) << id << " = ";
int index = IdIndex(first,id);
if(index)
cout << index << endl;
else
cout << "not found" << endl;
} // SEARCH ID
void SearchInfo(Node *first,char info)
{
cout << "index for info " << info << " = ";
int index = InfoIndex(first,info);
if(index)
cout << index << endl;
else
cout << "not found" << endl;
} // SEARCH INFO
void ErraseList(Node *first) // librar memoria retenida po la lista
{
Node *p = first;
while(p)
{
Node *q = p->next_id;
delete p;
p = q;
}
} // ERRASE LIST
void main()
{
Node *first = InitNode(0,'#');
ListBuilder(first);
ShowById(first);
ShowByInfo(first);
ShowNode(SearchById(first,5));
ShowNode(SearchById(first,55));
InsertNode(first,1,'g');
ShowById(first);
ShowByInfo(first);
DeleteNode(first,75);
DeleteNode(first,7);
ShowById(first);
ShowByInfo(first);
cout << endl;
SearchId(first,12);
SearchId(first,121);
SearchInfo(first,'z');
SearchInfo(first,'A');
cout << endl << "end of program - good bye ! ! !" << endl;
ErraseList(first);
} // MAIN
