sábado, 15 de junio de 2013

EXAMEN U3 PREGUNTA 1 (PILA ENLAZADA)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;
namespace Examen_UIII_pregunta_1
{
    class Nodo
    {
        public int dato;//v
        public Nodo sig;//s

        public Nodo()
        {
        }

        public Nodo(int pdato, Nodo psig)
        {
            dato = pdato;//v=vv;
            sig = psig;//s=ss;
        }
    }

        class PE
        {
            Nodo u;
            public PE()
            {
                u = null;
            }

            public void Agregar(int dato)
            {
                Nodo nu = new Nodo(dato, u);
                u = nu;
            }

            public int Extraer()
            {
                Nodo naux = new Nodo();
                int val;
                if (u == null)
                    return -1;
                naux = u;
                u = naux.sig;
                val = naux.dato;
                return val;
            }         
        }
    }
==========================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Examen_UIII_pregunta_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           // dataGridView1.ColumnCount = 3;
            dataGridView1.RowCount = 9;//Se asignan los renglones al DataGrigView
            dataGridView1.Rows.Add();
        }
      
       
        PE exam = new PE();//Se declara un objeto global
        int tam = 9;//Un entero global para el tamaño del arreglo


        private void butAgregar_Click(object sender, EventArgs e)
        {
 
            int dato;
            try//Validar que sea de tipo entero
            {
                dato = Int32.Parse(textAgregar.Text);
                textAgregar.Text = "";

            }
            catch
            {
                MessageBox.Show("Solo numeros");
                textAgregar.Text = "";
                return;
            }

            if (tam != 0)//tam!=0
            {
                exam.Agregar(dato);//Se agrega un nodo a la lista.
                dataGridView1[0, tam].Value = "";
                dataGridView1[0, tam--].Value = dato;
            }
            else//Si no se puede agregar la pila tipo lista esta llena
            {
                dataGridView1[0, tam].Value = "LISTA LLENA";
                return;
            }

            if (tam >= 0)
                dataGridView1[0, tam].Value = "Tope";
            textAgregar.Select();
        }


        private void butExtraer_Click(object sender, EventArgs e)
        {
            if (tam >= 0 && tam < 9)
            {
                textExtraer.Text = exam.Extraer().ToString();//Se muestra el numero eliminado en este caso el atributo del nodo
                dataGridView1[0, tam].Value = "";
                dataGridView1[0, tam].Value = "";
                dataGridView1[0, ++tam].Value = "";
            }
            else//Si no se cumple la pila tipo lista esta vacia
            {
                dataGridView1[0, tam].Value = "NO HAY DATOS";
                return;
            }
            if (tam >= 0)
                dataGridView1[0, tam].Value = "Tope";
        }
    }

}


No hay comentarios:

Publicar un comentario