Advertisement
Guest User

ListaDwukierunkowa

a guest
Jan 14th, 2021
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.77 KB | None | 0 0
  1. // Link.cs
  2.  
  3. using System;
  4.  
  5. namespace DoublyLinkedList
  6. {
  7.     public class Link<T> : IComparable<Link<T>> where T : IComparable<T>
  8.     {
  9.         public T Data { get; set; }
  10.         public Link<T> Next { get; set; }
  11.         public Link<T> Previous { get; set; }
  12.         public Link() { Next = null; Previous = null; }
  13.         public Link(T d) { Data = d; Next = null; Previous = null; }
  14.         public void DisplayLink()
  15.         { Console.Write(Data.ToString() + " "); }
  16.         public int CompareTo(Link<T> other) { return Data.CompareTo(other.Data); }
  17.         public int CompareTo(T other) { return Data.CompareTo(other); }
  18.     }
  19. }
  20.  
  21. // DoublyLinkedList.cs
  22.  
  23. using System;
  24.  
  25. namespace DoublyLinkedList
  26. {
  27.     public class DoublyLinkedList<T> where T : IComparable<T>
  28.     {
  29.         public Link<T> First { get; set; }
  30.         public Link<T> Last { get; set; }
  31.         public DoublyLinkedList(){ First = null;Last = null; }
  32.         public bool IsEmpty()
  33.         { return First == null; }
  34.         public Link<T> Find(T key)
  35.         {
  36.             if (IsEmpty()) return null;
  37.             Link<T> current = First;
  38.             while(current.CompareTo(key) != 0)
  39.             {
  40.                 if (current.Next == null)
  41.                     return null;
  42.                 else
  43.                     current = current.Next;
  44.             }
  45.             return current;
  46.         }
  47.         public void InsertFirst(T dd)
  48.         {
  49.             Link<T> newLink = new Link<T>(dd);
  50.             if (IsEmpty())
  51.                 Last = newLink;
  52.             else
  53.                 First.Previous = newLink;
  54.             newLink.Next = First;
  55.             First = newLink;
  56.         }
  57.         public void InsertLast(T dd)
  58.         {
  59.             Link<T> newLink = new Link<T>(dd);
  60.             if (IsEmpty())
  61.                 First = newLink;
  62.             else
  63.             {
  64.                 Last.Next = newLink;
  65.                 newLink.Previous = Last;
  66.             }
  67.             Last = newLink;
  68.         }
  69.         public Link<T> DeleteFirst()
  70.         {
  71.             if (IsEmpty()) return null;
  72.             Link<T> temp = First;
  73.             if (First.Next == null)
  74.                 Last = null;
  75.             else
  76.                 First.Next.Previous = null;
  77.             First = First.Next;
  78.             return temp;
  79.         }
  80.         public Link<T> DeleteLast()
  81.         {
  82.             if (IsEmpty()) return null;
  83.             Link<T> temp = Last;
  84.             if (First.Next == null)
  85.                 First = null;
  86.             else
  87.                 Last.Previous.Next = null;
  88.             Last = Last.Previous;
  89.             return temp;
  90.         }
  91.         public bool InsertAfter(T key, T dd)
  92.         {
  93.             Link<T> current = Find(key);
  94.             if (current == null) return false;
  95.             Link<T> newLink = new Link<T>(dd);
  96.             if(current == Last)
  97.             {
  98.                 newLink.Next = null;
  99.                 Last = newLink;
  100.             }
  101.             else
  102.             {
  103.                 newLink.Next = current.Next;
  104.                 current.Next.Previous = newLink;
  105.             }
  106.             newLink.Previous = current;
  107.             current.Next = newLink;
  108.             return true;
  109.         }
  110.         public void Insert(T key)
  111.         {
  112.             Link<T> newLink = new Link<T>(key);
  113.             Link<T> previous = null;
  114.             Link<T> current = First;
  115.             while(current != null && current.CompareTo(key) < 0)
  116.             {
  117.                 previous = current;
  118.                 current = current.Next;
  119.             }
  120.             if (previous == null)
  121.                 First = newLink;
  122.             else
  123.                 previous.Next = newLink;
  124.             newLink.Next = current;
  125.             //Poprawka dla listy dwukierunkowej
  126.             if (current == null)
  127.                 Last = newLink;
  128.             else
  129.                 current.Previous = newLink;
  130.             newLink.Previous = previous;
  131.         }
  132.         public Link<T> DeleteKey(T key)
  133.         {
  134.             Link<T> current = Find(key);
  135.             if (current == null) return null;
  136.             if (current == First)
  137.                 First = current.Next;
  138.             else
  139.                 current.Previous.Next = current.Next;
  140.             if (current == Last)
  141.                 Last = current.Previous;
  142.             else
  143.                 current.Next.Previous = current.Previous;
  144.             return current;
  145.         }
  146.         public void DisplayForward()
  147.         {
  148.             Console.Write("List (first-->last): ");
  149.             Link<T> current = First;
  150.             while(current != null)
  151.             {
  152.                 current.DisplayLink();
  153.                 current = current.Next;
  154.             }
  155.             Console.WriteLine();
  156.         }
  157.         public void DisplayBackward()
  158.         {
  159.             Console.Write("List (last-->first): ");
  160.             Link<T> current = Last;
  161.             while (current != null)
  162.             {
  163.                 current.DisplayLink();
  164.                 current = current.Previous;
  165.             }
  166.             Console.WriteLine();
  167.         }
  168.     }
  169. }
  170.  
  171. // Program.cs
  172.  
  173. using System;
  174.  
  175. namespace DoublyLinkedList
  176. {
  177.     class Program
  178.     {
  179.         static void Main(string[] args)
  180.         {
  181.             DoublyLinkedList<int> L = new DoublyLinkedList<int>();
  182.             char esc;
  183.             string str;
  184.             do
  185.             {
  186.                 Console.WriteLine("0. Usuwanie wszystkich elementów listy");
  187.                 Console.WriteLine("1. Sprawdzanie czy lista jest pusta");
  188.                 Console.WriteLine("2. Wyszukiwanie elementu na liście ");
  189.                 Console.WriteLine("3. Wstawienie elementu na początek listy ");
  190.                 Console.WriteLine("4. Wstawienie elementu na koniec listy ");
  191.                 Console.WriteLine("5. Usuwanie elementu z początku listy ");
  192.                 Console.WriteLine("6. Usuwanie elementu z końca listy ");
  193.                 Console.WriteLine("7. Wstawienie elementu za wybranym elementem listy ");
  194.                 Console.WriteLine("8. Wstawienie elementu do listy w sposób uporządkowany");
  195.                 Console.WriteLine("9. Usuwanie wybranegp elementu z listy ");
  196.                 Console.WriteLine("10. Wyświetlanie listy od głowy");
  197.                 Console.WriteLine("11. Wyświetlanie listy od ogona");
  198.                 str = Console.ReadLine();
  199.                 int.TryParse(str, out int choice);
  200.                 switch(choice)
  201.                 {
  202.                     case 0:
  203.                         {
  204.                             while (!L.IsEmpty())
  205.                                 L.DeleteFirst();
  206.                             break;
  207.                         }
  208.                     case 1:
  209.                         {
  210.                             Console.WriteLine(L.IsEmpty());
  211.                             break;
  212.                         }
  213.                     case 2:
  214.                         {
  215.                             Console.WriteLine("Podaj klucz węzła jaki chcesz wyszukać na liście");
  216.                             str = Console.ReadLine();
  217.                             int.TryParse(str, out int key);
  218.                             if (L.Find(key) == null)
  219.                                 Console.WriteLine("Węzła o podanym kluczu nie znaleziono");
  220.                             else
  221.                                 Console.WriteLine("Węzeł o podanym kluczu znaleziono");
  222.                             break;
  223.                         }
  224.                     case 3:
  225.                         {
  226.                             Console.WriteLine("Podaj klucz węzła jaki chcesz wstawić");
  227.                             str = Console.ReadLine();
  228.                             int.TryParse(str, out int dd);
  229.                             L.InsertFirst(dd);
  230.                             break;
  231.                         }
  232.                     case 4:
  233.                         {
  234.                             Console.WriteLine("Podaj klucz węzła jaki chcesz wstawić");
  235.                             str = Console.ReadLine();
  236.                             int.TryParse(str, out int dd);
  237.                             L.InsertLast(dd);
  238.                             break;
  239.                         }
  240.                     case 5:
  241.                         {
  242.                             Link<int> temp = L.DeleteFirst();
  243.                             if (temp == null)
  244.                                 Console.WriteLine("Węzła nie udało się usunąć");
  245.                             else
  246.                             {
  247.                                 Console.WriteLine("Usunięto węzeł:");
  248.                                 temp.DisplayLink();
  249.                                 Console.WriteLine();
  250.                             }
  251.                             break;
  252.                         }
  253.                     case 6:
  254.                         {
  255.                             Link<int> temp = L.DeleteLast();
  256.                             if (temp == null)
  257.                                 Console.WriteLine("Węzła nie udało się usunąć");
  258.                             else
  259.                             {
  260.                                 Console.WriteLine("Usunięto węzeł:");
  261.                                 temp.DisplayLink();
  262.                                 Console.WriteLine();
  263.                             }
  264.                             break;
  265.                         }
  266.                     case 7:
  267.                         {
  268.                             Console.WriteLine("Podaj klucz węzła za którym chcesz wstawić węzeł do listy");
  269.                             str = Console.ReadLine();
  270.                             int.TryParse(str, out int key);
  271.                             Console.WriteLine("Podaj klucz węzła jaki chcesz wstawić do listy");
  272.                             str = Console.ReadLine();
  273.                             int.TryParse(str, out int dd);
  274.                             Console.WriteLine(key.ToString() + " " +dd.ToString());
  275.                             bool isInserted = L.InsertAfter(key, dd);
  276.                             if (isInserted)
  277.                                 Console.WriteLine("Węzeł udało się wstawić");
  278.                             else
  279.                                 Console.WriteLine("Węzła nie udało się wstawić");
  280.                             break;
  281.                         }
  282.                     case 8:
  283.                         {
  284.                             Console.WriteLine("Podaj klucz węzła jaki chcesz wstawić");
  285.                             str = Console.ReadLine();
  286.                             int.TryParse(str, out int dd);
  287.                             L.Insert(dd);
  288.                             break;
  289.                         }
  290.                     case 9:
  291.                         {
  292.                             Console.WriteLine("Podaj klucz węzła jaki chcesz usunąć");
  293.                             str = Console.ReadLine();
  294.                             int.TryParse(str, out int dd);
  295.                             Link<int> temp = L.DeleteKey(dd);
  296.                             if (temp == null)
  297.                                 Console.WriteLine("Węzła nie udało się usunąć");
  298.                             else
  299.                             {
  300.                                 Console.WriteLine("Usunięto węzeł:");
  301.                                 temp.DisplayLink();
  302.                                 Console.WriteLine();
  303.                             }
  304.                             break;
  305.                         }
  306.                     case 10:
  307.                         {
  308.                             L.DisplayForward();
  309.                             break;
  310.                         }
  311.                     case 11:
  312.                         {
  313.                             L.DisplayBackward();
  314.                             break;
  315.                         }
  316.                     default:
  317.                         {
  318.                             Console.WriteLine("Brak operacji na liście");
  319.                             break;
  320.                         }
  321.                 }
  322.                 esc = (char)Console.ReadKey().Key;
  323.             }
  324.             while (esc != (char)ConsoleKey.Escape);
  325.         }
  326.     }
  327. }
  328.  
  329.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement