Advertisement
Guest User

Ejercicio6 TP4

a guest
Oct 19th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.25 KB | None | 0 0
  1. --------Clase Principal-----------
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace TP4Ejercicio6
  9. {
  10.     public class Eje6tp4
  11.     {
  12.  
  13.         public static void Main(String[] arg)
  14.         {
  15.             int n;
  16.             Queue<int> Cola = new Queue<int>();
  17.             Console.WriteLine("ingrese la cantidad de elementos a cargar");
  18.             n = Convert.ToInt16(Console.ReadLine());
  19.             Cargar(Cola,n);
  20.             MostrarProximoEnSalir(Cola, n);
  21.  
  22.             Console.ReadKey();
  23.             Console.Clear();
  24.  
  25.             Console.WriteLine("Cola vacia. Se Vuelve a cargar la cola");
  26.             Console.WriteLine("ingrese la cantidad de elementos a cargar");
  27.             n = Convert.ToInt16(Console.ReadLine());
  28.             Cargar(Cola, n);
  29.             MostrarProximoEnSalir(Cola, n);
  30.         }
  31.  
  32.         static void Cargar(Queue<int> Cola, int n)
  33.         {
  34.             int e = 0;
  35.            
  36.             for (int i = 0; i < n; i++)
  37.             {
  38.                 Console.WriteLine("ingrese un elemento");
  39.                 e = Convert.ToInt16(Console.ReadLine());
  40.                 Cola.Enqueue(e);
  41.             }
  42.         }
  43.  
  44.         static void MostrarProximoEnSalir(Queue<int> cola , int n)
  45.         {
  46.             for (int i = 0; i < n; i++)
  47.             {
  48.                 Console.WriteLine("proximo en salir: " + cola.Peek());
  49.                 Console.WriteLine("Sale: " + cola.Dequeue());
  50.             }
  51.         }
  52.     }
  53. }
  54.  
  55. ----------------------------------
  56.  
  57. ---------Clase lista---------------
  58. using System;
  59. using System.Collections.Generic;
  60. using System.Linq;
  61. using System.Text;
  62. using System.Threading.Tasks;
  63.  
  64. namespace TP4Ejercicio6
  65. {
  66.     public class Lista<E>
  67.     {
  68.  
  69.         private Node<E> head;
  70.         private int count;
  71.         private Node<E> tail;
  72.  
  73.         public Lista()
  74.         {
  75.             this.head = null;
  76.             this.count = 0;
  77.             this.tail = null;
  78.         }
  79.         public bool Contains(E item)
  80.         {
  81.             for (Node<E> temp = this.head; temp != null; temp = temp.next)
  82.             {
  83.                 if (temp.item.Equals(item))
  84.                 {
  85.                     return true;
  86.                 }
  87.  
  88.             }
  89.             return false;
  90.         }
  91.  
  92.         public bool isEmpty()
  93.         {
  94.             return this.count == 0;
  95.         }
  96.  
  97.  
  98.         public void AddToHead(E item)
  99.         {
  100.             Node<E> temp = new Node<E>(item, this.head);
  101.             if (isEmpty())
  102.             {
  103.                 this.tail = temp;
  104.             }
  105.             this.head = temp;
  106.             ++this.count;
  107.         }
  108.  
  109.         public void AddToTail(E item)
  110.         {
  111.             Node<E> temp = new Node<E>(item, null);
  112.             if (this.isEmpty())
  113.             {
  114.                 this.head = temp;
  115.             }
  116.             else
  117.             {
  118.                 this.tail.next = temp;
  119.             }
  120.             this.tail = temp;
  121.             ++this.count;
  122.         }
  123.         public E RemveFromHead()
  124.         {
  125.             if (this.isEmpty())
  126.             {
  127.                 throw new Exception("la lista esta vacia...");
  128.             }
  129.             E item = this.head.item;
  130.             this.head = this.head.next;
  131.  
  132.             if (this.head == null)
  133.             {
  134.                 this.tail = null;
  135.             }
  136.             --this.count;
  137.             return item;
  138.         }
  139.  
  140.         public E RemveFromTail()
  141.         {
  142.             if (this.isEmpty())
  143.             {
  144.                 throw new Exception("la lista esta vacia...");
  145.             }
  146.             E item = this.tail.item;
  147.             if (this.head.next == null)
  148.             {
  149.                 this.head = this.tail = null;
  150.             }
  151.             else
  152.             {
  153.                 Node<E> Skip = this.head;
  154.                 for (; Skip.next.next != null; Skip = Skip.next) { }
  155.                 this.tail = Skip;
  156.                 this.tail.next = null;
  157.             }
  158.             --this.count;
  159.             return item;
  160.  
  161.         }
  162.  
  163.         public void Motrar()
  164.         {
  165.             for (Node<E> Skip = this.head; Skip != null; Skip = Skip.next)
  166.             {
  167.                 Console.WriteLine("%s", Skip.toString());
  168.             }
  169.         }
  170.     }
  171. }
  172.  
  173. -----------------------------------
  174.  
  175. ----------Clase Node----------------
  176. using System;
  177. using System.Collections.Generic;
  178. using System.Linq;
  179. using System.Text;
  180. using System.Threading.Tasks;
  181.  
  182. namespace TP4Ejercicio6
  183. {
  184.     public class Node<E>
  185.     {
  186.         public E item;
  187.         public Node<E> next;
  188.  
  189.  
  190.  
  191.         public Node()
  192.         {
  193.  
  194.         }
  195.  
  196.         public Node(E item)
  197.         {
  198.             this.item = item;
  199.         }
  200.         public Node(E item, Node<E> next)
  201.         {
  202.             this.item = item;
  203.             this.next = next;
  204.         }
  205.  
  206.         public Node(Node<E> next)
  207.         {
  208.             this.next = next;
  209.         }
  210.  
  211.         public E getItem()
  212.         {
  213.             return item;
  214.         }
  215.         public void setItem(E item)
  216.         {
  217.             this.item = item;
  218.         }
  219.         public Node<E> getNext()
  220.         {
  221.             return next;
  222.         }
  223.  
  224.         public void setNext(Node<E> next)
  225.         {
  226.             this.next = next;
  227.         }
  228.  
  229.  
  230.         public String toString()
  231.         {
  232.             return this.item.ToString();
  233.         }
  234.     }
  235. }
  236.  
  237. -------------------------------------
  238.  
  239. ----------Clase Queue----------------
  240. using System;
  241. using System.Collections.Generic;
  242. using System.Linq;
  243. using System.Text;
  244. using System.Threading.Tasks;
  245.  
  246. namespace TP4Ejercicio6
  247. {
  248.     public class Queue<E> : Lista<E>
  249.     {
  250.  
  251.  
  252.  
  253.     private Lista<E> item;
  254.  
  255.     public Queue()
  256.     {
  257.  
  258.     }
  259.  
  260.     public void Enqueue(E item)
  261.     {
  262.  
  263.         this.AddToTail(item); ;
  264.     }
  265.  
  266.     public E Dequeue()
  267.     {
  268.         if (this.isEmpty())
  269.         {
  270.  
  271.             throw new Exception("la cola esta vacia");
  272.         }
  273.  
  274.         return (E)this.RemveFromHead();
  275.     }
  276.  
  277.     public E Peek()
  278.     {
  279.         if (this.isEmpty())
  280.         {
  281.             throw new Exception("la cola esta vacia");
  282.  
  283.         }
  284.         E R = (E)this.RemveFromHead();
  285.         this.AddToHead(R);
  286.         return R;
  287.  
  288.     }
  289.  
  290. }
  291. }
  292.  
  293. -------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement