Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.88 KB | None | 0 0
  1. //Auteurs: Maxime Gagnon et Mathieu Auger
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace Liste_CSharp
  10. {
  11.     class Noeud<T>
  12.     {
  13.  
  14.         public T value { get; set; }
  15.         public Noeud<T> succ { get; set; }
  16.  
  17.  
  18.         public Noeud(T val)
  19.         {
  20.             value = val;
  21.             succ = null;
  22.         }
  23.  
  24.         public Noeud(Noeud<T> head)
  25.         {
  26.             value = head.value;
  27.             while (head.succ != null)
  28.             {
  29.                 head = head.succ;
  30.                 succ = head.succ;
  31.             }
  32.         }
  33.     }
  34.  
  35.     class Liste<T>
  36.     {
  37.         private Noeud<T> head { get; set; }
  38.         public Noeud<T> tail { get; set; }
  39.  
  40.         public int size { get; private set; }
  41.         public Liste()
  42.         {
  43.             head = null;
  44.             tail = null;
  45.             size = 0;
  46.         }
  47.  
  48.         ~Liste()
  49.         {
  50.             while (!isEmpty())
  51.             {
  52.                 Extract();
  53.             }
  54.         }
  55.  
  56.  
  57.  
  58.         public bool isEmpty()
  59.         {
  60.             return head == null;
  61.         }
  62.  
  63.         public void Add(T val)
  64.         {
  65.  
  66.             if (isEmpty())
  67.             {
  68.                 head = new Noeud<T>(val);
  69.                 tail = head;
  70.             }
  71.             else
  72.             {
  73.                 Noeud<T> item = head;
  74.                 while(item.succ!=null)
  75.                 {
  76.                     item = item.succ;
  77.                 }
  78.                 item.succ = new Noeud<T>(val);
  79.                 tail = item.succ;
  80.             }
  81.  
  82.             ++size;
  83.         }
  84.  
  85.         public Liste<T> Clone()
  86.         {
  87.             Liste<T> liste = new Liste<T>();
  88.             Noeud<T> temp = head;
  89.  
  90.             while (temp != null)
  91.             {
  92.                 liste.Add(temp.value);
  93.                 temp = temp.succ;
  94.             }
  95.  
  96.             return liste;
  97.         }
  98.  
  99.         public T Extract()
  100.         {
  101.             if(isEmpty())
  102.             {
  103.                 throw new IndexOutOfRangeException();
  104.             }
  105.             Noeud<T> item = head;
  106.             head = head.succ;
  107.             T val = item.value;
  108.             --size;
  109.  
  110.             return val;
  111.         }
  112.  
  113.         public void Show(TextWriter stream)
  114.         {
  115.             Liste<T>copy = this.Clone();
  116.             if(copy.size>0)
  117.             {
  118.                 while (copy.head.succ != null)
  119.                 {
  120.                     stream.WriteLine(copy.head.value);
  121.                     copy.head = copy.head.succ;
  122.                 }
  123.                 stream.WriteLine(copy.head.value);
  124.             }
  125.         }
  126.  
  127.         public void Empty()
  128.         {
  129.             int nbBoucle = size;
  130.             for(int i=0;i<nbBoucle;i++)
  131.             {
  132.                 Extract();
  133.             }
  134.         }
  135.  
  136.         public bool SameAs(Liste<T> other)
  137.         {
  138.             Liste<T> main = this.Clone();
  139.             Liste<T> otherListe = other.Clone();
  140.             bool result = true;
  141.             if(other.size==size)
  142.             {
  143.                 while (otherListe.head.succ != null)
  144.                 {
  145.                     if(!EqualityComparer<T>.Default.Equals(main.head.value, otherListe.head.value))
  146.                     {
  147.                         result = false;
  148.                     }
  149.                     otherListe.head = otherListe.head.succ;
  150.                     main.head = main.head.succ;
  151.                 }
  152.             }
  153.             else
  154.             {
  155.                 result = false;
  156.             }
  157.             return result;
  158.         }
  159.         public void Reverse()
  160.         {
  161.             Noeud<T> newHead = null;
  162.             Noeud<T> newTail = null;
  163.            
  164.             while (head!=null)
  165.             {
  166.                 Noeud<T> item = new Noeud<T>(head);
  167.                 if (newTail==null)
  168.                 {
  169.                     newTail = item;
  170.                 }
  171.                 item.succ = newHead;
  172.                 newHead = item;
  173.                 item = head;
  174.                 head = head.succ;
  175.             }
  176.             tail = newTail;
  177.             head = newHead;
  178.         }
  179.     }
  180.  
  181.  
  182.  
  183.     class Program
  184.     {
  185.         static void Main(string[] args)
  186.         {
  187.             int NB_ENTIERS = 25;
  188.             Console.WriteLine("Creation Liste1");
  189.             Liste<int> lst = new Liste<int>();
  190.  
  191.             //Ajouter des éléments à la liste
  192.             for (int i = 0; i < NB_ENTIERS; i++)
  193.                 lst.Add(i + 1);
  194.  
  195.             //Dupliquer une liste
  196.             Console.WriteLine("Dupliquer Liste1 sur Liste2");
  197.             Liste<int> lstInv = lst.Clone();
  198.  
  199.             //Identiques?
  200.             Console.WriteLine("Liste1 et Liste 2 sont identique: ");
  201.             Console.WriteLine(lst.SameAs(lstInv));
  202.  
  203.             //Inverser les éléments d'une liste
  204.             Console.WriteLine("Inverse Liste 2");
  205.             lstInv.Reverse();
  206.  
  207.             //Connaître le nombre d'éléments dans la liste
  208.             Console.WriteLine("Nb Elements Liste 2: ");
  209.             int NB_ELEMENTS = lstInv.size;
  210.             Console.WriteLine(NB_ELEMENTS);
  211.  
  212.             //Identiques?
  213.             Console.WriteLine("Liste1 et Liste 2 sont identique: ");
  214.             Console.WriteLine(lst.SameAs(lstInv));
  215.  
  216.             Console.WriteLine("Liste 1: ");
  217.             lst.Show(Console.Out);
  218.  
  219.             Console.WriteLine("Liste 2: ");
  220.             lstInv.Show(Console.Out);
  221.  
  222.             Console.WriteLine("Vider Liste2");
  223.             lstInv.Empty();
  224.  
  225.             Console.WriteLine("Extraction des elements 1 par 1 de Liste1: ");
  226.  
  227.             for (int i = 0; i < NB_ELEMENTS; i++)
  228.                 Console.WriteLine("Extraction de "+lst.Extract() + " ");
  229.  
  230.             Console.WriteLine("Liste 1: ");
  231.             lst.Show(Console.Out);
  232.  
  233.             Console.WriteLine("Liste 2: ");
  234.             lstInv.Show(Console.Out);
  235.         }
  236.     }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement