Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package lista1;
- /**
- *
- * @author usuario
- */
- public class Lista1 {nodo1 L;//referencia a la lista
- int dim; //numeros de elemento
- //constructor
- public Lista1(){
- L=null;
- dim=0;
- }
- public boolean vacia(){
- return(L==null);
- }
- public boolean unico(){
- return (L.getEnlace()==null);
- }
- // METODO DE INSERTAR
- public void insertar(int elem){
- if(vacia())
- {
- nodo1 P=new nodo1();
- P.setDato(elem);
- L=P;
- dim++;
- }
- else{
- nodo1 q=L;nodo1 ant=null;
- while((q!=null)&&(q.getDato()<=elem)){
- ant=q;
- q=q.getEnlace();
- }
- if(ant==null){// no se movio
- nodo1 P=new nodo1(elem);
- L=P;
- P.setEnlace(q);
- dim++;
- }else{
- if(ant.getDato()!=elem){
- nodo1 P=new nodo1(elem);
- ant.setEnlace(P);
- P.setEnlace(q);
- dim++;
- }
- }
- }
- }
- public void InsertarP(int ele) {
- nodo1 P = new nodo1(ele);
- P.setEnlace(L);
- L = P;
- dim++;
- }
- public void InsertarU(int ele) {
- if (vacia()) {
- InsertarP(ele);
- } else {
- nodo1 aux = L;
- while ((aux.getEnlace() != null) && (aux.getDato() != ele)) {
- aux = aux.getEnlace();
- }
- if (aux.getDato() != ele) {
- nodo1 P = new nodo1(ele);
- aux.setEnlace(P);
- dim++;
- }
- }
- }
- //insertar con elemetos repetidos
- public void insertar1(int d){
- nodo1 p=new nodo1();
- p.setDato(d);
- p.setEnlace(L);
- L=p;
- dim++;
- }
- //INSETAR ORDENADO
- public void InsertarOrd(int ele){
- if (vacia()){
- InsertarP(ele);
- }else{//hay mas elementos
- nodo1 Aux=L;
- nodo1 Ant=null;
- while ((Aux!=null)&&(Aux.getDato()<=ele)){
- Ant=Aux;
- Aux=Aux.getEnlace();
- }
- if (Ant==null)
- {
- InsertarP(ele);
- }
- else{//inserta en el cuerpo
- if (Ant.getDato()!=ele){
- nodo1 P=new nodo1();
- P.setDato(ele);
- Ant.setEnlace(P);
- P.setEnlace(Aux);
- }
- dim++;
- }
- }
- }
- public void eliminar(int elem){
- if(!vacia()){
- nodo1 q=L;
- nodo1 ant=null;
- while((q!=null)&&(q.getDato()!=elem)){
- ant=q;
- q=q.getEnlace();
- }
- if(ant==null){
- L=L.getEnlace();
- dim--;
- }else{
- if(q!=null){
- ant.setEnlace(q.getEnlace());
- dim--;
- }
- }
- }
- }
- public void pares(){
- if(!vacia()){
- int aux=L.getDato();
- L=L.getEnlace();
- pares();
- if(aux % 2==0){
- if(L==null)
- L=new nodo1(aux);
- else{
- nodo1 par=new nodo1(aux);
- par.setEnlace(L);
- L=par;
- }
- }
- }
- }
- @Override
- public String toString() {
- String s = "Lista --> < ";
- nodo1 P = L;
- while (P != null) {
- s = s + P.getDato() + ", ";
- P = P.getEnlace();
- }
- return s + ">";
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- Lista1 L=new Lista1();
- L.insertar(1);
- L.insertar(2);
- L.insertar(3);
- L.insertar(4);
- L.insertar(5);
- L.insertar(6);
- System.out.println(L.toString());
- L.pares();
- System.out.println(L);
- }
- }
- <----- CODIGO DEL NODO ---->
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package lista1;
- /**
- *
- * @author usuario
- */
- public class nodo1 { int dato;
- nodo1 enlace;
- public nodo1(){
- enlace=null;
- }
- public nodo1(int dato){
- this.dato=dato;
- enlace=null;
- }
- public void setDato(int dato) {
- this.dato = dato;
- }
- public void setEnlace(nodo1 enlace) {
- this.enlace = enlace;
- }
- public int getDato() {
- return dato;
- }
- public nodo1 getEnlace() {
- return enlace;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement