Advertisement
KTVX94

MyA II Parcial 1

Apr 26th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Node<T>
  6. {
  7.     T _value;
  8.     public Node<T> next;
  9.     public Node<T> prev;
  10.  
  11.     public void SetValue(T v)
  12.     {
  13.         _value = v;
  14.     }
  15.  
  16.     public T GetValue()
  17.     {
  18.         return _value;
  19.     }
  20.  
  21.     public void SetNodeNext(T newNode)
  22.     {
  23.         var tempNode = new Node<T>();
  24.         tempNode.SetValue(newNode);
  25.         next = tempNode;
  26.         next.prev = this;
  27.     }
  28.  
  29. }
  30.  
  31. ////////////////////////////////////////////////////
  32.  
  33. public class LinkdList<T>
  34. {
  35.     Node<T> first;
  36.     Node<T> last;
  37.  
  38.     public void Initialization(T element)
  39.     {
  40.         first = new Node<T>();
  41.         first.prev = null;
  42.         first.SetValue(element);
  43.         last = first;
  44.     }
  45.  
  46.     public void Add(T element)
  47.     {
  48.         if (first == null)
  49.             Initialization(element);
  50.         else
  51.         {
  52.             last.SetNodeNext(element);
  53.             last = last.next;
  54.         }
  55.     }
  56.  
  57.     public int Count()
  58.     {
  59.         int value = 0;
  60.         var current = first;
  61.         if (current == null)
  62.             return value;
  63.  
  64.         while (current != null)
  65.         {
  66.             value++;
  67.             current = current.next;
  68.         }
  69.  
  70.         return value;
  71.     }
  72.  
  73. ///////////////////////////////////
  74.  
  75. public class MyQueue<T> : IQueue<T>
  76. {
  77.     private MyLinkedList<T> _queue;
  78.  
  79.     //Tiene algun elemento?
  80.     public bool IsEmpty { get { return _queue.Count <= 0; } }
  81.  
  82.     //Cantidad de elementos
  83.     public int Count { get { return _queue.Count; } }
  84.  
  85.     public MyQueue() { _queue = new MyLinkedList<T>(); }
  86.  
  87.     //Limpia la cola
  88.     public void Clear() { _queue.Clear(); }
  89.  
  90.     //Saca el primer elemento de la cola
  91.     public T Dequeue()
  92.     {
  93.         T element = Peek();
  94.         _queue.RemoveFirst();
  95.  
  96.         return element;
  97.     }
  98.  
  99.     //Inserta un elemento al final de la cola
  100.     public void Enqueue(T element) { _queue.AddLast(element); }
  101.  
  102.     //Muestra el primer elemento de la cola
  103.     public T Peek() { return _queue.First.Value; }
  104. }
  105.  
  106. /////////////////////////////
  107.  
  108. public class MyStack<T> : IStack<T>
  109. {
  110.     private DynamicArray<T> _stack;
  111.  
  112.     //Tiene algun elemento?
  113.     public bool IsEmpty { get { return _stack.Count <= 0; } }
  114.  
  115.     //Cantidad de elementos
  116.     public int Count { get { return _stack.Count; } }
  117.  
  118.     public MyStack() { _stack = new DynamicArray<T>(); }
  119.  
  120.     //Limpia la cola
  121.     public void Clear() { _stack.Clear(); }
  122.  
  123.     //Saca el primer elemento de la pila
  124.     public T Pop()
  125.     {
  126.         T element = Peek();
  127.         _stack.RemoveAt(_stack.Count - 1);
  128.  
  129.         return element;
  130.     }
  131.  
  132.     //Muestra el primer elemento de la pila
  133.     public T Peek() { return _stack[_stack.Count - 1]; }
  134.  
  135.     //Inserta un elemento al tope de la pila
  136.     public void Push(T element) { _stack.Add(element); }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement