Está en la página 1de 3

Estructura de datos

COLAS - EJERCICIOS DE APOYO

1. Insertar el elemento K como penultimo


C(5,8,9,11,4) K=100 ==> C(5,8,9,11,100,4)

SOLUCIÓN (C++)
int main()
{
nodo *frente=NULL;
nodo *fin=NULL;
nodo *frenteaux=NULL;
nodo *finaux=NULL;
int dato,x,k;

//Llena datos en la cola


do{cout<<"Numero : ";
cin>>dato;
if(dato!=0)
agregarcola(frente,fin,dato);
}while(!(dato==0));

//Ejercicio: Insertar K como penultimo


cout<<"Numero a insertar: ";
cin>>k;
while(frente->siguiente!=NULL) //Traslada (no ult) Cola a Caux
{ sacarcola(frente,fin,dato);
agregarcola(frenteaux,finaux,dato);
}
sacarcola(frente,fin,x);

while(frenteaux!=NULL) //Traslada Caux a Cola


{ sacarcola(frenteaux, finaux, dato);
agregarcola(frente, fin, dato);
}
agregarcola(frente, fin, k);
agregarcola(frente, fin, x);

//Imprime la cola
cout<<"\nLa cola es: ";
while(frente!=NULL)
{ sacarcola(frente,fin,dato);
cout<<dato<<",";
}
return 0;
}

1 Lic. Patricia Fernández Quisbert


Estructura de datos

2. Cambiar elementos impares por 0s


C (4, 7, 5, 16, 11, 13) => C (4,0,0,16,0,0)

SOLUCIÓN (C++)
int main()
{ nodo *frente=NULL;
nodo *fin=NULL;

nodo *frenteaux=NULL;
nodo *finaux=NULL;

int dato;

//Llena datos en la cola


do{cout<<"Numero : ";
cin>>dato;
if(dato!=0)
agregarcola(frente,fin,dato);
}while(!(dato==0));

//Cambiar a los impares por ceros


while(frente!=NULL)
{ sacarcola(frente,fin,dato);
if(dato%2==0)
agregarcola(frenteaux,finaux,dato);
else
agregarcola(frenteaux,finaux,0);
}

while(frenteaux!=NULL) //Traslada Caux a C


{ sacarcola(frenteaux,finaux,dato);
agregarcola(frente,fin,dato);
}

//Imprime datos de la cola


cout<<"\nLa cola es: ";
while(frente!=NULL)
{ sacarcola(frente,fin,dato);
cout<<dato<<",";
}
return 0;
}

2 Lic. Patricia Fernández Quisbert


Estructura de datos

3. Verificar si tiene central


C (8, 3, 7, 6, 2, 5, 9) => “Si tiene central”
C (5, 7, 6, 2) ==> “No hay central”

SOLUCIÓN (C++)
int main()
{ nodo *frente=NULL;
nodo *fin=NULL;

int dato,cont;

do{cout<<"Numero : ";
cin>>dato;
if(dato!=0)
agregarcola(frente,fin,dato);
}while(!(dato==0));

//Verificar si tiene central


cont=0;
while(frente!=NULL)
{ sacarcola(frente,fin,dato);
cont=cont+1;
}

if(cont%2==1)
cout<<"Tiene central";
else
cout<<"No tiene central";

return 0;
}

3 Lic. Patricia Fernández Quisbert

También podría gustarte