Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------Clase Principal-----------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TP4Ejercicio6
- {
- public class Eje6tp4
- {
- public static void Main(String[] arg)
- {
- int n;
- Queue<int> Cola = new Queue<int>();
- Console.WriteLine("ingrese la cantidad de elementos a cargar");
- n = Convert.ToInt16(Console.ReadLine());
- Cargar(Cola,n);
- MostrarProximoEnSalir(Cola, n);
- Console.ReadKey();
- Console.Clear();
- Console.WriteLine("Cola vacia. Se Vuelve a cargar la cola");
- Console.WriteLine("ingrese la cantidad de elementos a cargar");
- n = Convert.ToInt16(Console.ReadLine());
- Cargar(Cola, n);
- MostrarProximoEnSalir(Cola, n);
- }
- static void Cargar(Queue<int> Cola, int n)
- {
- int e = 0;
- for (int i = 0; i < n; i++)
- {
- Console.WriteLine("ingrese un elemento");
- e = Convert.ToInt16(Console.ReadLine());
- Cola.Enqueue(e);
- }
- }
- static void MostrarProximoEnSalir(Queue<int> cola , int n)
- {
- for (int i = 0; i < n; i++)
- {
- Console.WriteLine("proximo en salir: " + cola.Peek());
- Console.WriteLine("Sale: " + cola.Dequeue());
- }
- }
- }
- }
- ----------------------------------
- ---------Clase lista---------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TP4Ejercicio6
- {
- public class Lista<E>
- {
- private Node<E> head;
- private int count;
- private Node<E> tail;
- public Lista()
- {
- this.head = null;
- this.count = 0;
- this.tail = null;
- }
- public bool Contains(E item)
- {
- for (Node<E> temp = this.head; temp != null; temp = temp.next)
- {
- if (temp.item.Equals(item))
- {
- return true;
- }
- }
- return false;
- }
- public bool isEmpty()
- {
- return this.count == 0;
- }
- public void AddToHead(E item)
- {
- Node<E> temp = new Node<E>(item, this.head);
- if (isEmpty())
- {
- this.tail = temp;
- }
- this.head = temp;
- ++this.count;
- }
- public void AddToTail(E item)
- {
- Node<E> temp = new Node<E>(item, null);
- if (this.isEmpty())
- {
- this.head = temp;
- }
- else
- {
- this.tail.next = temp;
- }
- this.tail = temp;
- ++this.count;
- }
- public E RemveFromHead()
- {
- if (this.isEmpty())
- {
- throw new Exception("la lista esta vacia...");
- }
- E item = this.head.item;
- this.head = this.head.next;
- if (this.head == null)
- {
- this.tail = null;
- }
- --this.count;
- return item;
- }
- public E RemveFromTail()
- {
- if (this.isEmpty())
- {
- throw new Exception("la lista esta vacia...");
- }
- E item = this.tail.item;
- if (this.head.next == null)
- {
- this.head = this.tail = null;
- }
- else
- {
- Node<E> Skip = this.head;
- for (; Skip.next.next != null; Skip = Skip.next) { }
- this.tail = Skip;
- this.tail.next = null;
- }
- --this.count;
- return item;
- }
- public void Motrar()
- {
- for (Node<E> Skip = this.head; Skip != null; Skip = Skip.next)
- {
- Console.WriteLine("%s", Skip.toString());
- }
- }
- }
- }
- -----------------------------------
- ----------Clase Node----------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TP4Ejercicio6
- {
- public class Node<E>
- {
- public E item;
- public Node<E> next;
- public Node()
- {
- }
- public Node(E item)
- {
- this.item = item;
- }
- public Node(E item, Node<E> next)
- {
- this.item = item;
- this.next = next;
- }
- public Node(Node<E> next)
- {
- this.next = next;
- }
- public E getItem()
- {
- return item;
- }
- public void setItem(E item)
- {
- this.item = item;
- }
- public Node<E> getNext()
- {
- return next;
- }
- public void setNext(Node<E> next)
- {
- this.next = next;
- }
- public String toString()
- {
- return this.item.ToString();
- }
- }
- }
- -------------------------------------
- ----------Clase Queue----------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TP4Ejercicio6
- {
- public class Queue<E> : Lista<E>
- {
- private Lista<E> item;
- public Queue()
- {
- }
- public void Enqueue(E item)
- {
- this.AddToTail(item); ;
- }
- public E Dequeue()
- {
- if (this.isEmpty())
- {
- throw new Exception("la cola esta vacia");
- }
- return (E)this.RemveFromHead();
- }
- public E Peek()
- {
- if (this.isEmpty())
- {
- throw new Exception("la cola esta vacia");
- }
- E R = (E)this.RemveFromHead();
- this.AddToHead(R);
- return R;
- }
- }
- }
- -------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement