Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ejercicio2
- {
- class Program
- {
- static void Main(string[] args)
- {
- Lista<Producto> lista = new Lista<Producto>();
- int opc;
- do
- {
- Console.WriteLine("Agregar producto al principio de la lista..........[1]");
- Console.WriteLine("Agregar producto al final de la lista..............[2]");
- Console.WriteLine("Eliminar producto desde el principio de la lista...[3]");
- Console.WriteLine("Eliminar producto desde el final de la lista.......[4]");
- Console.WriteLine("Mostrar productos..................................[5]");
- Console.WriteLine("SALIR..............................................[6]");
- Console.Write("ESCOJA UNA OPCION:~ ");
- opc = Int32.Parse(Console.ReadLine());
- switch (opc)
- {
- case 1: lista.AddToHead(NuevoProducto());
- Console.Clear();
- break;
- case 2: lista.AddToLast(NuevoProducto());
- Console.Clear();
- break;
- case 3: lista.RemoveFromHead();
- Console.Write("Eliminado");
- Console.ReadKey();
- Console.Clear();
- break;
- case 4: lista.RemoveFromTail();
- Console.Write("Eliminado");
- Console.ReadKey();
- Console.Clear();
- break;
- case 5: Console.Clear();
- lista.viewList();
- Console.ReadKey();
- Console.Clear();
- break;
- case 6: Console.Write("Bye Bye");
- break;
- default:Console.WriteLine("Ops, opcion incorrecta");
- Console.ReadKey();
- break;
- }
- }while(opc!=6);
- }
- public static Producto NuevoProducto()
- {
- Producto P = new Producto();
- Console.Write("Código: ");
- P.Code = int.Parse(Console.ReadLine());
- Console.Write("Descripción: ");
- P.Description = Console.ReadLine();
- Console.Write("Fecha de expiración (dd/mm/yyyy): ");
- P.ExpirationDate = Convert.ToDateTime(Console.ReadLine());
- Console.Write("Precio: $");
- P.SalePrice = double.Parse(Console.ReadLine(), System.Globalization.CultureInfo.InvariantCulture);
- return P;
- }
- }
- }
- /*-------------------------CLASE PRODUCTO--------------------------------------*/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ejercicio2
- {
- class Producto
- {
- private int code;
- private string description;
- private DateTime expirationDate;
- private double salePrice;
- public Producto() { }
- public Producto(int code, DateTime expirationDate, string description, double saleprice)
- {
- this.Code = code;
- this.ExpirationDate = expirationDate;
- this.Description = description;
- this.SalePrice = saleprice;
- }
- public int Code
- {
- get
- {
- return code;
- }
- set
- {
- code = value;
- }
- }
- public DateTime ExpirationDate
- {
- get
- {
- return expirationDate;
- }
- set
- {
- expirationDate = value;
- }
- }
- public string Description
- {
- get
- {
- return description;
- }
- set
- {
- description = value;
- }
- }
- public double SalePrice
- {
- get
- {
- return salePrice;
- }
- set
- {
- salePrice = value;
- }
- }
- public override string ToString()
- {
- return "\nCodigo: " + this.code + "\nDescripcion: " + this.description + "\nFechaExpiracion: " + this.ExpirationDate.ToShortDateString() + "\nprecio: $" + this.salePrice;
- }
- }
- }
- /*-------------------------CLASE LISTA CON CLASE NODO PRIVADA--------------------------------------*/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ejercicio2
- {
- class Lista<E>
- {
- private Node head;
- private int count;
- private Node tail;
- public bool Empty
- {
- get { return this.count <= 0; }
- }
- public Lista()
- {
- this.head = null;
- this.count = 0;
- this.tail = null;
- }
- public void AddToHead(E item)
- {
- Node temp = new Node(item ,this.head);
- if(Empty)
- {
- this.tail = temp;
- }
- this.head = temp;
- ++this.count;
- }
- public void AddToLast(E item)
- {
- Node temp = new Node(item, null);
- if (Empty)
- {
- this.head = temp;
- }
- else
- {
- this.tail.next = temp;
- }
- this.tail = temp;
- ++this.count;
- }
- public E RemoveFromHead()
- {
- if (Empty)
- {
- throw new Exception("La lista esta vacia");
- }
- //toma el elemento que esta en el primer nodo
- E item = this.head.item;
- //avanza el primer nodo al siguiente
- this.head = this.head.next;
- //si no hay mas nodos
- if (this.head == null)
- {
- //vaciar la lista
- this.tail = null;
- }
- --this.count;
- return item;
- }
- public E RemoveFromTail()
- {
- if (Empty)
- {
- throw new Exception("La lista esta vacia");
- }
- E item = this.tail.item;
- //si es el unico nodo
- if (this.head.next == null)
- {
- //vacia la lista
- this.head = this.tail = null;
- }
- else
- {
- Node skip = this.head;
- //recorre la lista mientras haya dos o mas nodos
- for (; skip.Next.Next != null; skip = skip.next)
- { }
- //skip es el penultimo nodo que ahora sera el ultimo
- this.tail = skip;
- //anula la referencia del al siguiente nodo
- this.tail.next = null;
- }
- --this.count;
- return item;
- }
- public void viewList()
- {
- if (Empty)
- {
- Console.WriteLine("Lista vacía...");
- }
- else
- {
- Node aux = this.head;
- while (aux.Next != null)
- {
- Console.WriteLine(aux.Item);
- aux = aux.Next;
- }
- Console.WriteLine(aux.Item);
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- private class Node
- {
- public E item;
- public Node next;
- public Node()
- {
- }
- public Node(E item)
- {
- this.item = item;
- }
- public Node(E item, Node next)
- {
- this.item = item;
- this.next = next;
- }
- public Node(Node next)
- {
- this.next = next;
- }
- public Node Next
- {
- get { return this.next; }
- set { this.next = value;}
- }
- public E Item
- {
- get { return this.item; }
- set { this.item = value;}
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement