Você está na página 1de 31

UNIVERSIDAD NACIONAL DE TRUJILLO

Ao de la Consolidacin del Mar de Grau

DOCENTE: Escobedo Neyra , Haybert


CURSO: Algoritmos y estructuras de datos.
TEMA: Listas Enlazadas
CICLO: III
ESCUELA: INGENIERA DE SISTEMAS
INTEGRANTES:

SANCHEZ ACEVEDO, MARIO


MORALES RUIZ, JUAN
PASTOR URQUIAGA, CHRISTIAN
CUEVA LEAN, OSCAR
TACILLA VILELA, MAX

TRUJILLO- PER
2016

EJERCICIO N* 01
// DISEO DEL FORMULARIO

// IMPLEMENTACION DE LA CLASE ClsNodo


Public Structure ClsNodo
Public dato As String
Public enlace As Integer
End Structure
// IMPLEMENTACION DE LA CLASE ClsOperaLista

Imports WindowsApplication1.ClsNodo
Public Class ClsOperaLista
Public CAB As Integer
Public xLib As Integer
Public anterior As Integer
Public Sub inicializarLista(L() As ClsNodo, t As Integer)
CAB = 99
xLib = 1
anterior = 99
For k = 1 To t
L(k).dato = ""
L(k).enlace = k + 1
Next
L(t).enlace = 99
End Sub
Public Function Reservar(L() As ClsNodo) As Integer
Dim aux As Integer
If xLib = 99 Then
aux = 99

Else
aux = xLib
xLib = L(xLib).enlace
End If
Return aux
End Function
Public Sub InsertarElementos(L() As ClsNodo, xDato As String)
Dim aux2 As Integer = Reservar(L)
If aux2 <> 99 Then
L(aux2).dato = xDato
If anterior = 99 Then
L(aux2).enlace = 99
CAB = aux2
Else
L(aux2).enlace = L(anterior).enlace
L(anterior).enlace = aux2
End If
anterior = aux2
Else
MsgBox("Lista Llena ")
End If
End Sub
Public Sub Ordenar(L() As ClsNodo, t As Integer)
Dim i As Integer = 1
Dim j As Integer
Dim aux As String
While i < t
j=i+1
While j <= t
If (L(i).dato > L(j).dato) Then
aux = L(j).dato
L(j).dato = L(i).dato
L(i).dato = aux
End If
j += 1
End While
i += 1
End While
End Sub
End ClasS
DIESO DE LOS BOTONES
Imports WindowsApplication1.ClsNodo
Imports WindowsApplication1.ClsOperaLista
Public Class Form1
Public obj As New ClsOperaLista
Public L() As ClsNodo
Private Sub BtnInicializar_Click(sender As Object, e As EventArgs) Handles
BtnInicializar.Click
ReDim L(TxtTamao.Text)
obj.inicializarLista(L, TxtTamao.Text)
End Sub
Private Sub BtnRegistrar_Click(sender As Object, e As EventArgs) Handles
BtnRegistrar.Click
obj.InsertarElementos(L, TxtElemento.Text)
TxtElemento.Text = ""

TxtElemento.Focus()
End Sub
Private Sub BtnMostrar_Click(sender As Object, e As EventArgs) Handles BtnMostrar.Click
obj.Ordenar(L, TxtTamao.Text)
ListBox1.Items.Clear()
Dim pos As Integer
pos = obj.CAB
Dim xFila As String = ""
While pos <> 99
xFila = CStr(pos) + " " + L(pos).dato + " " + CStr(L(pos).enlace)
ListBox1.Items.Add(xFila)
pos = L(pos).enlace
End While
End Sub

End Class
// IMPLEMENTACION DE LOS BOTONES
Imports WindowsApplication1.ClsNodo
Imports WindowsApplication1.ClsOperaLista
Public Class Form1
Public obj As New ClsOperaLista
Public L() As ClsNodo
Private Sub BtnInicializar_Click(sender As Object, e As EventArgs) Handles
BtnInicializar.Click
ReDim L(TxtTamao.Text)
obj.inicializarLista(L, TxtTamao.Text)
End Sub
Private Sub BtnRegistrar_Click(sender As Object, e As EventArgs) Handles
BtnRegistrar.Click
obj.InsertarElementos(L, TxtElemento.Text)
TxtElemento.Text = ""
TxtElemento.Focus()
End Sub
Private Sub BtnMostrar_Click(sender As Object, e As EventArgs) Handles BtnMostrar.Click
obj.Ordenar(L, TxtTamao.Text)
ListBox1.Items.Clear()
Dim pos As Integer
pos = obj.CAB
Dim xFila As String = ""
While pos <> 99
xFila = CStr(pos) + " " + L(pos).dato + " " + CStr(L(pos).enlace)
ListBox1.Items.Add(xFila)
pos = L(pos).enlace
End While
End Sub
End Class

EJERCICIO N* 2

// IMPLEMENTACION DEL FORMULARIO

// IMPLEMENTACION DE LA CLASE CLSNODO


Public Structure ClsNodo
Public dato As String
Public enlace As Integer
End Structure
// IMPLEMENTACION DE LA CLASE OPERALISTA
Imports WindowsApplication1.ClsNodo
Public Class ClsOperaLista
Public CAB As Integer
Public xLib As Integer
Public anterior As Integer
Public Sub inicializarLista(L() As ClsNodo, t As Integer)
CAB = 99
xLib = 1
anterior = 99
For k = 1 To t
L(k).dato = ""
L(k).enlace = k + 1
Next
L(t).enlace = 99
End Sub
Public Function Reservar(L() As ClsNodo) As Integer
Dim aux As Integer
If xLib = 99 Then
aux = 99

Else
aux = xLib
xLib = L(xLib).enlace
End If
Return aux
End Function
Public Sub InsertarElementos(L() As ClsNodo, xDato As String)
Dim aux2 As Integer = Reservar(L)
If aux2 <> 99 Then
L(aux2).dato = xDato
If anterior = 99 Then
L(aux2).enlace = 99
CAB = aux2
Else
L(aux2).enlace = L(anterior).enlace
L(anterior).enlace = aux2
End If
anterior = aux2
Else
MsgBox("Lista Llena ")
End If
End Sub
Public Sub Ordenar(L() As ClsNodo, t As Integer)
Dim i As Integer = 1
Dim j As Integer
Dim aux As String
While i < t
j=i+1
While j <= t
If (L(i).dato < L(j).dato) Then
aux = L(j).dato
L(j).dato = L(i).dato
L(i).dato = aux
End If
j += 1
End While
i += 1
End While
End Sub
End Class
// IMPLEMENTACION DE LOS BOTONES
Imports WindowsApplication1.ClsNodo
Imports WindowsApplication1.ClsOperaLista
Public Class Form1
Public obj As New ClsOperaLista
Public L() As ClsNodo
Private Sub BtnInicializar_Click(sender As Object, e As EventArgs) Handles
BtnInicializar.Click
ReDim L(TxtTamao.Text)
obj.inicializarLista(L, TxtTamao.Text)
End Sub
Private Sub BtnRegistrar_Click(sender As Object, e As EventArgs) Handles
BtnRegistrar.Click
obj.InsertarElementos(L, TxtElemento.Text)
TxtElemento.Text = ""

TxtElemento.Focus()
End Sub
Private Sub BtnMostrar_Click(sender As Object, e As EventArgs) Handles BtnMostrar.Click
ListBox1.Items.Clear()
Dim pos As Integer
pos = obj.CAB
Dim xFila As String = ""
While pos <> 99
xFila = CStr(pos) + " " + L(pos).dato + " " + CStr(L(pos).enlace)
ListBox1.Items.Add(xFila)
pos = L(pos).enlace
End While
End Sub
Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles
ListBox2.SelectedIndexChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs)
ListBox2.Items.Clear()
Dim pos As Integer
pos = obj.CAB
Dim xFila As String = ""
While pos <> 99
xFila = CStr(pos) + " " + L(pos).dato + " " + CStr(L(pos).enlace)
ListBox2.Items.Add(xFila)
pos = L(pos).enlace
End While
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles
CheckBox1.CheckedChanged
obj.Ordenar(L, TxtTamao.Text)
ListBox2.Items.Clear()
Dim pos As Integer
pos = obj.CAB
Dim xFila As String = ""
While pos <> 99
xFila = CStr(pos) + " " + L(pos).dato + " " + CStr(L(pos).enlace)
ListBox2.Items.Add(xFila)
pos = L(pos).enlace
End While
End Sub
End Class

EJERCICIO N* 04
DISEO DEL FORMULARIO

// IMPLEMENTACION DE LA CLASE CLSESTRUCTURALISTA


#pragma once
class EstructuraLista
{
public:
char dato;
int puntero;
EstructuraLista(void)
{
}
};
// IMPLEMENTACION DE LA CLASE OPERALISTA
#pragma once
#include "EstructuraLista.h"
class OperaLista
{
public:
int CAB;
int LIB;
EstructuraLista L[50];
public:

void Inicializar(EstructuraLista[], int&, int&);


void Agregar(EstructuraLista[], int&, int&, char);
int Buscar(EstructuraLista[], int, int, char, int);

OperaLista(void)
{
}
};
void OperaLista::Inicializar(EstructuraLista L[], int &cab, int &lib)
{
cab=99;
lib=0;
for(int k=0; k<50; k++)
{
L[k].dato=' ';
L[k].puntero=k+1;
}
L[49].puntero = 99;
}
void OperaLista::Agregar(EstructuraLista L[], int &cab, int &lib, char xdato)
{
L[lib].dato = xdato;
if(cab==99)
{
cab=lib;
lib=L[lib].puntero;
L[cab].puntero=99;
}
else
{
int xptr=cab;
while(L[xptr].puntero!=99)
xptr=L[xptr].puntero;
int xlib=lib;
L[xptr].puntero=lib;
lib=L[xlib].puntero;
L[xlib].puntero=99;
}

}
int OperaLista::Buscar(EstructuraLista L[],int cab,int lib,char xdato,int xtipo)
{
//Busquedad si Xtipo
if (cab == 99)
{
return 0;
}else{
int puntero = cab;
while(puntero!=99){
if (xdato == L[puntero].dato)
{
return 1;
}else{
puntero = L[puntero].puntero;
}
}
return 0;
}

}
// IMPLEMENTACION DE LOS BOTONES
#pragma endregion
e) {

private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^


Lista.CAB=99;
Lista.LIB=0;
Lista.Inicializar(Lista.L, Lista.CAB, Lista.LIB);

}
private: System::Void btnAgregar_Click(System::Object^ sender,
System::EventArgs^ e) {
String^ xx = txtElemento->Text;
if (xx->Length == 0 || xx->Length >1 )
{
MessageBox::Show("ERROR: INGRESE 1 ELEMENTO ");
}else{
char xdato = Convert::ToChar(xx);
if (Lista.Buscar(Lista.L, Lista.CAB, Lista.LIB, xdato,0) == 0)
{
Lista.Agregar(Lista.L, Lista.CAB, Lista.LIB, xdato);
txtElemento->Text="";
txtElemento->Focus();
}else{
MessageBox::Show("ERROR: DATO YA REGISTRADO");
}
}
}
private: System::Void btnMostrar_Click(System::Object^ sender, System::EventArgs^ e) {
lstLista->Items->Clear();
int xptr=Lista.CAB;
while(xptr!=99)
{
char xdato=Lista.L[xptr].dato;
String^ xLinea = Convert::ToString(xptr)+" "+Char::ToString(xdato)
+" "+Convert::ToString(Lista.L[xptr].puntero);
lstLista->Items->Add(xLinea);
xptr=Lista.L[xptr].puntero;
}}
private: System::Void btnSalir_Click(System::Object^ sender, System::EventArgs^ e) {
MyForm::Close();
}
private: System::Void lstLista_SelectedIndexChanged(System::Object^ sender,
System::EventArgs^ e) {
}
};}
EJERCICIO N*5 ( INCLUYE EL EJERCICIO N* 03)
// DECLARAR LAS LIBRERIAS
#include "stdafx.h"

#include <iostream>
using namespace std;
struct nodo {
int data;
nodo *sgte;
};
void recorrer(nodo *cab);
void insertarFinal(nodo **cab, int data);
void insertarInicio(nodo **pcab, int data);
void insertarEnN(nodo **pcab, int data, int n);
int contarNodos(nodo *cab);
void eliminar(nodo **pcab, nodo *n);
nodo *buscar(nodo *cab, int data);
int main() {
nodo *lista = NULL;
int op = 0;
int aux, aux2;
do{
do{
cout<<"..::LISTA ENLAZADA SIMPLE::.."<<endl;
cout<<"[1] recorrer."<<endl;
cout<<"[2] insertarFinal"<<endl;
cout<<"[3] insertarInicio"<<endl;
cout<<"[4] insertarEnN"<<endl;
cout<<"[5] eliminar"<<endl;
cout<<"[6] buscar"<<endl;
cout<<"[7] salir"<<endl;
cout<<endl;
cout<<"OPCION: ";
cin>>op;
}while(op<1 || op>7);
switch(op){
case 1:
recorrer(lista);
break;
case 2:
cout<<"Ingrese un valor: ";
cin>>aux;
insertarFinal(&lista, aux);
break;
case 3:
cout<<"Ingrese un valor: ";
cin>>aux;
insertarInicio(&lista, aux);
break;
case 4:
cout<<"Ingrese un valor: ";
cin>>aux;
cout<<"Ingrese la pocicion: ";
cin>>aux2;
insertarEnN(&lista, aux, aux2);
break;
case 5:
cout<<"Ingrese un valor: ";

cin>>aux;
eliminar(&lista, buscar(lista, aux));
break;
case 6:
cout<<"Ingrese un valor: ";
cin>>aux;
cout<<"El nodo esta en la direccion: "<<buscar(lista, aux)<<endl;
break;
}
}while(op!=7);
return 0;
}
//Funcin para recorrer una lista y mostrar la data.
void recorrer(nodo *cab){
//verifica si la lista esta vaca
if(cab == NULL){
//si esta vaca muestra este mensaje
cout<<"La lista esta vacia."<<endl;
}
else{
//si no esta vaca declara un puntero p al primer elemento de la lista
nodo *p = cab;
do{
//muestra la data del nodo referido por p
cout<<"Elemento: "<<p->data<<endl;
//asigna a p la direccin de nodo siguiente o NULL si no lo hay
p = p->sgte;
//repite el proceso mientras p sea diferente de NULL
}while(p != NULL);
cout<<"Fin del recorrido."<<endl;
}
}

//Funcin que inserta un nodo nuevo al final de la lista


void insertarFinal(nodo **pcab, int data){
//crea un nuevo nodo dinmicamente e inicia sus campos
nodo *nuevo = new nodo;
nuevo->data = data;
nuevo->sgte = NULL;

//valida si la lista esta vacia (pcab apunta a la cabecera osea pcab==&cab)


if(*pcab == NULL){
//modifica la cabecera para que apunte al nuevo nodo
*pcab = nuevo;
}
else{

//declara un puntero p que apunta al primer nodo (p==cab)


nodo *p = *pcab;
//avanza p hasta que llega al ultimo nodo (el que tiene NULL en sgte)
while(p->sgte != NULL){
p = p->sgte;
}
//asigna la direccin del nuevo nodo al campo sgte del ultimo nodo
p->sgte = nuevo;
}
}
//Funcion que inserta un nodo nuevo al inicio de la lista
void insertarInicio(nodo **pcab, int data){
//crea un nodo nuevo
nodo *nuevo = new nodo;
//inicia el campo data del nuevo nodo con la data recibida por parmetro
nuevo->data = data;
//asigna al campo sgte del nuevo nodo el valor de la cabecera (*pcab==cab)
//si la lista esta vacia (cab==NULL) sgte sera NULL
//si la lista no esta vacia sgte apuntara al primer elemento
nuevo->sgte = *pcab;
//modifica la cabecera para que apunte al nuevo nodo
//el nuevo nodo sera ahora el primero
*pcab = nuevo;
}
//Funcin que inserta un nodo nuevo en la posicin ensima
void insertarEnN(nodo **pcab, int data, int n){
//contamos el numero de nodos de la lista
int nNodos = contarNodos(*pcab);
//validamos que el ndice n este en un rango valido
if(n > nNodos || n < 0){
//si no esta, mostramos un mensaje de error
cout<<"Posicion no valida. Hay "<<nNodos<<" nodos en la lista"<<endl;
}
//si n es 0 (primera posicin) llamamos a insertarInicio()
else if(n == 0){

insertarInicio(pcab, data);
}
//si n es igual al numero de nodos (ultima posicin) llamamos a insertarFinal()
else if(n == nNodos){
insertarFinal(pcab, data);
}
//en este caso n debe estar en la zona intermedia de la lista
else{
//declaramos un puntero p al primer elemento de la lista
nodo *p = *pcab;
//avanzamos p hasta la posicin n-1
for(int i=0; i<n-1; i++)
p = p->sgte;
//creamos el nuevo nodo e iniciamos su data
nodo *nuevo = new nodo;
nuevo->data = data;
//el campo sgte del nuevo elemento apuntara al nodo siguiente de p
nuevo->sgte = p->sgte;
//el campo sgte de p apuntara al nuevo nodo
p->sgte = nuevo;
}
}
int contarNodos(nodo *cab){
int cont = 0;
while(cab!=NULL){
cab = cab->sgte;
cont++;
}
return cont;
}
//Funcin que elimina un nodo usando como criterio de busqueda la direccion del nodo
void eliminar(nodo **pcab, nodo *n){
//valida si la lista esta vacia
if(*pcab == NULL){
cout<<"La lista esta vacia."<<endl;
}
else{
//valida si el nodo a eliminar existe
if(n != NULL){
//valida si el nodo a eliminar es el primero
if(n == *pcab){

//hace que la cabecera apunte al segundo elemento


*pcab = (*pcab)->sgte;
}
else{
//declara un puntero p al primer elemento de la lista
nodo *p = *pcab;
//hace que p apunte al nodo anterior al que se va a eliminar
while(p->sgte!=NULL && p->sgte!=n){
p = p->sgte;
}
//enlaza al nodo anterior con el siguiente
p->sgte = n->sgte;
}
//libera el espacio de memoria del nodo eliminado
delete n;
cout<<"El elemento se ha eliminado correctamente."<<endl;
}
else{
cout<<"No se encontro el elemento."<<endl;
}
}
}
//Funcin que busca un nodo y devuelve una referencia al nodo. Si no lo encuentra devuelve
NULL.
nodo *buscar(nodo *cab, int data){
//valida si la lista esta vacia
if(cab == NULL){
cout<<"La lista esta vacia."<<endl;
}
else{
//declara un puntero p al primer elemento de la lista
nodo *p = cab;
do{
//valida si la data del nodo es la data buscada
if(p->data == data){
//devuelve la referencia al nodo y termina la funcin
return p;
}
else{
//p apunta al siguiente nodo
p = p->sgte;
}
//se repite mientras no se llegue al final de la lista
}while(p!=NULL);
}
//si termina la lista y no encontro el nodo buscado retorna NULL
return NULL;
}

EJERCICIO N* 06
// DISEO DEL FORMULARIO

IMPLEMENTACION DE LA CLASE clsEstructuraLista


Public Structure clsEstructuraLista
Public dato As String
Public puntero As Integer
End Structure
IMPLEMENTACION DE LA CLASE clsEstructuraLista
Imports problema6.clsEstructuraLista
Public Class clsOperaLista
Public CAB As Integer
Public xLIB As Integer
Public Sub InicializarLista(ByRef L() As clsEstructuraLista, t As Integer)
CAB = 99
xLIB = 1
For i = 1 To t Step 1
L(i).dato = ""
L(i).puntero = i + 1
Next
L(t).puntero = 99
End Sub

Public Sub AgregarDato(ByRef L() As clsEstructuraLista, xDato As String)


If xLIB <> 99 Then
L(xLIB).dato = xDato
If CAB = 99 Then
CAB = xLIB
xLIB = L(CAB).puntero
L(CAB).puntero = 99
Else
Dim xPtr As Integer = CAB
While L(xPtr).puntero <> 99
xPtr = L(xPtr).puntero
End While
Dim wLIB As Integer = xLIB
xLIB = L(xLIB).puntero
L(xPtr).puntero = wLIB
L(wLIB).puntero = 99
End If
End If
End Sub
Public Function devolverTamao(ByRef L() As clsEstructuraLista, t As Integer) As Integer
Dim aux As Integer = xLIB
Dim cont As Integer = 0
While aux <> t
If L(aux).puntero <> 99 Then
cont = cont + 1
aux = L(aux).puntero
End If
End While
Return cont + 1
End Function
End Class
// IMPLEMENTACION DE LOS BOTONES
Imports problema6.clsEstructuraLista
Imports problema6.clsOperaLista
Public Class Form1
Public obj As New clsOperaLista
Public lista() As clsEstructuraLista
Private Sub btnInicializar_Click(sender As Object, e As EventArgs) Handles
btnInicializar.Click
ReDim lista(Val(txtTamao.Text))
obj.InicializarLista(lista, Val(txtTamao.Text))
End Sub
Private Sub btnAgregar_Click(sender As Object, e As EventArgs) Handles btnAgregar.Click
obj.AgregarDato(lista, txtDato.Text)
End Sub
Private Sub btnMostrar_Click(sender As Object, e As EventArgs) Handles btnMostrar.Click
lstDatos.Items.Clear()
Dim expresion As String = 0
Dim pos As Integer
pos = obj.CAB
While pos <> 99
expresion = CStr(pos) + " " + lista(pos).dato + " " + CStr(lista(pos).puntero)

lstDatos.Items.Add(expresion)
pos = lista(pos).puntero
End While
End Sub
Private Sub btnNumElementos_Click(sender As Object, e As EventArgs) Handles
btnNumElementos.Click
Dim aux As Integer
aux = obj.devolverTamao(lista, Val(txtTamao.Text))
txtNumElementos.Text = CStr(aux)
End Sub
End Class

EJERCICIO N* 07

IMPLEMENTACION DEL FORMULARIO

IMPLEMTACION DE LA CLASE EstructuraLista


#pragma once
class EstructuraLista
{
public:
char dato;
int puntero;
EstructuraLista(void)
{
}
};
IMPLEMTACION DE LA CLASE OperaLista
#pragma once
#include "EstructuraLista.h"
class OperaLista
{
public:
int CAB;
int LIB;
EstructuraLista L[50];

public:
void Inicializar(EstructuraLista[], int&, int&);
void Agregar(EstructuraLista[], int&, int&, char);
int Buscar(EstructuraLista[], int, int, char);
void Eliminar(EstructuraLista[], int&, int&, char);
void Editar(EstructuraLista[], int, int, char, char);
void Impar(EstructuraLista[], char);
OperaLista(void)
{
}
};
void OperaLista::Inicializar(EstructuraLista L[], int &cab, int &lib)
{
cab=99;
lib=0;
for(int k=0; k<50; k++)
{
L[k].dato=' ';
L[k].puntero=k+1;
}
L[49].puntero = 99;
}
void OperaLista::Agregar(EstructuraLista L[], int &cab, int &lib, char xdato)
{
L[lib].dato = xdato;
if(cab==99)
{
cab=lib;
lib=L[lib].puntero;
L[cab].puntero=99;
}
else
{
int xptr=cab;
while(L[xptr].puntero!=99)
xptr=L[xptr].puntero;
int xlib=lib;
L[xptr].puntero=lib;
lib=L[xlib].puntero;
L[xlib].puntero=99;
}
}
int OperaLista::Buscar(EstructuraLista L[],int cab,int lib,char xdato)
{

}
void OperaLista::Eliminar(EstructuraLista L[], int &cab, int &lib, char xdato)
{
}
void OperaLista::Editar(EstructuraLista L[], int cab, int lib, char xdato, char ndato)
{

}
// IMPLEMENTACION DE LOS BOTONES
#pragma endregion
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^
e) {
Lista.CAB=99;
Lista.LIB=0;
Lista.Inicializar(Lista.L, Lista.CAB, Lista.LIB);
}
private: System::Void btnAgregar_Click(System::Object^ sender,
System::EventArgs^ e) {
String^ xx = txtElemento->Text;
char xdato = Convert::ToChar(xx);
Lista.Agregar(Lista.L, Lista.CAB, Lista.LIB, xdato);
txtElemento->Text="";
}
private: System::Void btnMostrar_Click(System::Object^ sender, System::EventArgs^ e) {
lstLista->Items->Clear();
listLista2->Items->Clear();
int xptr=Lista.CAB;
while(xptr!=99)
{
char xdato=Lista.L[xptr].dato;
String^ xLinea = Convert::ToString(xptr)+" "+Char::ToString(xdato)
+" "+Convert::ToString(Lista.L[xptr].puntero);

if(xdato%2!=0){
String^ xLinea1 = Convert::ToString(xptr)+" "+Char::ToString(xdato)
+" "+Convert::ToString(Lista.L[xptr].puntero);

listLista2->Items->Add(xLinea1);}

lstLista->Items->Add(xLinea);
xptr=Lista.L[xptr].puntero;

private: System::Void btnEliminar_Click(System::Object^ sender, System::EventArgs^ e) {


}
private: System::Void btnEditar_Click(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void btnBuscar_Click(System::Object^ sender, System::EventArgs^ e) {

}
private: System::Void btnSalir_Click(System::Object^ sender, System::EventArgs^ e) {
MyForm::Close();
}
private: System::Void lstLista_SelectedIndexChanged(System::Object^ sender,
System::EventArgs^ e) {
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
listLista2->Items->Clear();
int xptr=Lista.CAB;
while(xptr!=99)
{
char xdato=Lista.L[xptr].dato;
do{
String^ xLinea = Convert::ToString(xptr)+" "+Char::ToString(xdato)
+" "+Convert::ToString(Lista.L[xptr].puntero);
listLista2->Items->Add(xLinea);
xptr=Lista.L[xptr].puntero;}while(xdato%2==0);

}}
private: System::Void label4_Click(System::Object^ sender, System::EventArgs^ e) {
}
};
}

EJERCICIO N* 08

// IMPLEMENTACION DEL FORMULARIO

// IMPLEMENTACION DE LA ESTRUCTURA clsEstructuraDato


Public Structure clsEstructuraDato
Public dato As String
Public puntero As Integer
End Structure
// IMPLEENTACION DE LA CLASE clsEstructuraDato
Imports problema_8.clsEstructuraDato
Public Class clsOperaLista
Public CAB As Integer
Public xLIB As Integer
Public Sub InicializarLista(ByRef L() As clsEstructuraDato, t As Integer)
CAB = 99
xLIB = 1
For i = 1 To t Step 1
L(i).dato = " "
L(i).puntero = i + 1
Next
L(t).puntero = 99
End Sub
Public Sub AgregarDato(L() As clsEstructuraDato, xDato As String)
If xLIB <> 99 Then
L(xLIB).dato = xDato
If CAB = 99 Then
CAB = xLIB
xLIB = L(CAB).puntero
L(CAB).puntero = 99
Else
Dim xPtr As Integer = CAB
While L(xPtr).puntero <> 99

xPtr = L(xPtr).puntero
End While
Dim wLIB As Integer = xLIB
xLIB = L(xLIB).puntero
L(xPtr).puntero = wLIB
L(wLIB).puntero = 99
End If
End If
End Sub
Public Function Promedio(L() As clsEstructuraDato, t As Integer) As Double
Dim i As Integer
Dim suma As Integer = 0
Dim prom As Double = 0
For i = 1 To t Step 1
suma = suma + Val(L(i).dato)
Next
prom = suma / t
Return prom
End Function
Public Sub listaMayor(L() As clsEstructuraDato, ListMay() As clsEstructuraDato, t As
Integer, promed As Double)
Dim i As Integer
InicializarLista(ListMay, t)
For i = 1 To t Step 1
If L(i).dato > promed Then
AgregarDato(ListMay, L(i).dato)
End If
Next
End Sub
Public Sub eliminarnodo(L() As clsEstructuraDato, t As Integer, xnodo As Integer)
Dim xptr As Integer
xptr = L(xnodo).puntero
'Solo para evaluar primer elemento
If L(xptr).puntero = xnodo Then
L(xptr).puntero = ""
End If
While xptr <> 99
Dim auxptrsig As Integer = L(xptr).puntero
If auxptrsig <> 99 Then
If L(auxptrsig).puntero = xnodo Then
L(xptr).puntero = L(auxptrsig).puntero
L(auxptrsig).dato = " "
Exit While
End If
End If
xptr = L(xptr).puntero
End While
End Sub
End ClasS
// IMPLEMENTACION DE LOS BOTONES
Imports problema_8.clsOperaLista

Imports problema_8.clsEstructuraDato
Public Class Form1
Dim lista() As clsEstructuraDato
Dim listaMayProm() As clsEstructuraDato
Dim obj As New clsOperaLista

Private Sub btnInicializar_Click(sender As Object, e As EventArgs) Handles


btnInicializar.Click
ReDim lista(Val(txtTamao.Text))
obj.InicializarLista(lista, Val(txtTamao.Text))
End Sub
Private Sub btnAgregarDato_Click(sender As Object, e As EventArgs) Handles
btnAgregarDato.Click
obj.AgregarDato(lista, Val(txtDato.Text))
End Sub
Private Sub btnMostrarLista_Click(sender As Object, e As EventArgs) Handles
btnMostrarLista.Click
Dim i As Integer
Dim expresion As String
i = obj.xLIB
While i <> 99
expresion = CStr(i) + " " + lista(i).dato + CStr(lista(i).puntero)
lstDatos.Items.Add(expresion)
i = lista(i).puntero
End While
End Sub
End Class

EJERCICIO N* 09
// DECLARAR LAS LIBRERIAS
#include <iostream>
using namespace std;

//DEFINIR LA ESTRUCTURA DEL NODO


struct nodo {
int dato;
nodo *sgt;
};
//FUNCIONES
void crear_lista(nodo **cab, int numero);
void recorrer(nodo *cab);
void insertarFinal(nodo **cab, int data);
void insertarEnN(nodo **pcab, int data, int n);
int contarNodos(nodo *cab);
void eliminar(nodo **pcab, nodo *n);
nodo *buscar(nodo *cab, int data);
int mostrar_impares(nodo *cab);
//PROGRAMA PRINCIPAL
main(){
//DECLARAMOS VARIABLES
nodo *final = NULL;
int op = 0;
int numero, posicion;
do{
do{
cout<<"\n\n\n";
cout<<"
LISTA ENLAZADA SIMPLE
\n\n"<<endl;
cout<<"
[1] Crear Lista "<<endl;
cout<<"
[2] Insertar al Final"<<endl;
cout<<"
[3] Insertar en una Posici\xA2n"<<endl;
cout<<"
[4] Eliminar en una Posici\xA2n"<<endl;
cout<<"
[5] Reportar"<<endl;
cout<<"
[6] Obtener N\xA3mero de Elementos"<<endl;
cout<<"
[7] Obtener Lista Para Datos Impares"<<endl;
cout<<"
[8] Obtener Lista Para Dato Mayor que el Promedio"<<endl;
cout<<"
[9] Salir"<<endl;
cout<<endl;
cout<<"\t\t ELIJA UNA OPCION: ";
cin>>op;
}while(op<1 || op>9);
switch(op){
case 1:
//llamamos a la funcin crear Lista y le enviamos los parametros
cout<<"\n\t Ingrese el valor del numero: ";
cin>>numero;
crear_lista(&final, numero);
break;
case 2:
cout<<"\n\t Ingrese un valor: ";
cin>>numero;
insertarFinal(&final,numero);
break;
case 3:
cout<<"Ingrese un valor: ";
cin>>numero;
cout<<"Ingrese la pocicion: ";
cin>>posicion;
insertarEnN(&final, numero, posicion);

break;
case 4:
cout<<"Ingrese un valor: ";
cin>>numero;
eliminar(&final, buscar(final,numero));
break;
case 5:
recorrer(final);
break;
case 6:
cout<<"El numero de nodos es: ";cout<<contarNodos(final);
break;
case 7:
cout<<"Los impares son: ";cout<<mostrar_impares(final);
break;
case 8:
//crear_lista(&cabecera, &final);
break;
}
}while(op!=9);
return 0;
}
//-------------------------- FUNCIONES -------------------------//----------- CREAR LISTA -------------void crear_lista(nodo **cab, int dato){
///crea un nodo nuevo
nodo *nuevo = new nodo;
//inicia el campo data del nuevo nodo con la data recibida por parmetro
nuevo->dato = dato;
nuevo->sgt = *cab;
//modifica la cabecera para que apunte al nuevo nodo
//el nuevo nodo sera ahora el primero
*cab = nuevo;
}
//----------- REPORTAR -------------//Funcin para recorrer una lista y mostrar la data.
void recorrer(nodo *cab){
int i = 0;
//verifica si la lista esta vaca
if(cab == NULL){
//si esta vaca muestra este mensaje
cout<<"\n\t La lista esta vacia."<<endl;
}else{
while(cab != NULL){
cout <<' '<< i+1 <<") " << cab->dato<< endl;
cab = cab->sgt;
i++;
}
cout<<"\n\t Fin del recorrido."<<endl;
}
}
//----------- INSERTAR AL FINAL -------------void insertarFinal(nodo **cab, int data){

//crea un nuevo nodo dinmicamente e inicia sus campos


nodo *nuevo = new nodo;
nuevo->dato = data;
nuevo->sgt = NULL;
//valida si la lista esta vacia (pcab apunta a la cabecera osea pcab==&cab)
if(*cab == NULL){
//modifica la cabecera para que apunte al nuevo nodo
*cab = nuevo;
}else{
//declara un puntero p que apunta al primer nodo (p==cab)
nodo *p = *cab;
//avanza p hasta que llega al ultimo nodo (el que tiene NULL en sgte)
while(p->sgt != NULL){
p = p->sgt;
}
//asigna la direccin del nuevo nodo al campo sgte del ultimo nodo
p->sgt = nuevo;
}

}
//----------- INSERTAR EN UNA POSICION -------------void insertarEnN(nodo **cab, int data, int n){
int nNodos = contarNodos(*cab);
//validamos que el ndice n este en un rango valido
if(n > nNodos || n < 0){
//si no esta, mostramos un mensaje de error
cout<<"Posicion no valida. Hay "<<nNodos<<" nodos en la lista"<<endl;
}
else if(n == 0){
crear_lista(cab, data);
}else if(n == nNodos){
insertarFinal(cab, data);
}else{
//declaramos un puntero p al primer elemento de la lista
nodo *p = *cab;
//avanzamos p hasta la posicin n-1
for(int i=0; i<n-1; i++)
p = p->sgt;
nodo *nuevo = new nodo;
nuevo->dato = data;
nuevo->sgt = p->sgt;
p->sgt = nuevo;
}
}
//----------- CONTAR CUANTOS NODOS EXISTEN -------------int contarNodos(nodo *cab){
int cont = 0;
while(cab!=NULL){
cab = cab->sgt;
cont++;
}
return cont;
}
//----------- FUNCION PARA ELIMINAR UN NODO -------------void eliminar(nodo **pcab, nodo *n){
//valida si la lista esta vacia
if(*pcab == NULL){

cout<<"La lista esta vacia."<<endl;


}
else{
//valida si el nodo a eliminar existe
if(n != NULL){
//valida si el nodo a eliminar es el primero
if(n == *pcab){
//hace que la cabecera apunte al segundo elemento
*pcab = (*pcab)->sgt;
}
else{
//declara un puntero p al primer elemento de la lista
nodo *p = *pcab;
//hace que p apunte al nodo anterior al que se va a eliminar
while(p->sgt!=NULL && p->sgt!=n){
p = p->sgt;
}
//enlaza al nodo anterior con el siguiente
p->sgt = n->sgt;
}
//libera el espacio de memoria del nodo eliminado
delete n;
cout<<"El elemento se ha eliminado correctamente."<<endl;
}
else{
cout<<"No se encontro el elemento."<<endl;
}
}
}
//----------- FUNCION PARA BUSCAR UN NODO -------------nodo *buscar(nodo *cab, int data){
//valida si la lista esta vacia
if(cab == NULL){
cout<<"La lista esta vacia."<<endl;
}
else{
//declara un puntero p al primer elemento de la lista
nodo *p = cab;
do{
//valida si la data del nodo es la data buscada
if(p->dato == data){
//devuelve la referencia al nodo y termina la funcin
return p;
}
else{
//p apunta al siguiente nodo
p = p->sgt;
}
//se repite mientras no se llegue al final de la lista
}while(p!=NULL);
}
//si termina la lista y no encontro el nodo buscado retorna NULL
return NULL;
}

//----------- MOSTRAR IMPARES -------------//Funcin para recorrer una lista


int mostrar_impares(nodo *cab){
int i = 0;
nodo *p;
//verifica si la lista esta vaca
if(cab == NULL){
//si esta vaca muestra este mensaje
cout<<"\n\t La lista esta vacia."<<endl;
}else{
while(cab != NULL ){
if (cab->dato %2 ==0){
cab->sgt = cab;
p = cab->sgt;
}
}
return 1;
}
}
// EJECUCION DEL CODIGO

Você também pode gostar