Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Link.cs
- using System;
- namespace DoublyLinkedList
- {
- public class Link<T> : IComparable<Link<T>> where T : IComparable<T>
- {
- public T Data { get; set; }
- public Link<T> Next { get; set; }
- public Link<T> Previous { get; set; }
- public Link() { Next = null; Previous = null; }
- public Link(T d) { Data = d; Next = null; Previous = null; }
- public void DisplayLink()
- { Console.Write(Data.ToString() + " "); }
- public int CompareTo(Link<T> other) { return Data.CompareTo(other.Data); }
- public int CompareTo(T other) { return Data.CompareTo(other); }
- }
- }
- // DoublyLinkedList.cs
- using System;
- namespace DoublyLinkedList
- {
- public class DoublyLinkedList<T> where T : IComparable<T>
- {
- public Link<T> First { get; set; }
- public Link<T> Last { get; set; }
- public DoublyLinkedList(){ First = null;Last = null; }
- public bool IsEmpty()
- { return First == null; }
- public Link<T> Find(T key)
- {
- if (IsEmpty()) return null;
- Link<T> current = First;
- while(current.CompareTo(key) != 0)
- {
- if (current.Next == null)
- return null;
- else
- current = current.Next;
- }
- return current;
- }
- public void InsertFirst(T dd)
- {
- Link<T> newLink = new Link<T>(dd);
- if (IsEmpty())
- Last = newLink;
- else
- First.Previous = newLink;
- newLink.Next = First;
- First = newLink;
- }
- public void InsertLast(T dd)
- {
- Link<T> newLink = new Link<T>(dd);
- if (IsEmpty())
- First = newLink;
- else
- {
- Last.Next = newLink;
- newLink.Previous = Last;
- }
- Last = newLink;
- }
- public Link<T> DeleteFirst()
- {
- if (IsEmpty()) return null;
- Link<T> temp = First;
- if (First.Next == null)
- Last = null;
- else
- First.Next.Previous = null;
- First = First.Next;
- return temp;
- }
- public Link<T> DeleteLast()
- {
- if (IsEmpty()) return null;
- Link<T> temp = Last;
- if (First.Next == null)
- First = null;
- else
- Last.Previous.Next = null;
- Last = Last.Previous;
- return temp;
- }
- public bool InsertAfter(T key, T dd)
- {
- Link<T> current = Find(key);
- if (current == null) return false;
- Link<T> newLink = new Link<T>(dd);
- if(current == Last)
- {
- newLink.Next = null;
- Last = newLink;
- }
- else
- {
- newLink.Next = current.Next;
- current.Next.Previous = newLink;
- }
- newLink.Previous = current;
- current.Next = newLink;
- return true;
- }
- public void Insert(T key)
- {
- Link<T> newLink = new Link<T>(key);
- Link<T> previous = null;
- Link<T> current = First;
- while(current != null && current.CompareTo(key) < 0)
- {
- previous = current;
- current = current.Next;
- }
- if (previous == null)
- First = newLink;
- else
- previous.Next = newLink;
- newLink.Next = current;
- //Poprawka dla listy dwukierunkowej
- if (current == null)
- Last = newLink;
- else
- current.Previous = newLink;
- newLink.Previous = previous;
- }
- public Link<T> DeleteKey(T key)
- {
- Link<T> current = Find(key);
- if (current == null) return null;
- if (current == First)
- First = current.Next;
- else
- current.Previous.Next = current.Next;
- if (current == Last)
- Last = current.Previous;
- else
- current.Next.Previous = current.Previous;
- return current;
- }
- public void DisplayForward()
- {
- Console.Write("List (first-->last): ");
- Link<T> current = First;
- while(current != null)
- {
- current.DisplayLink();
- current = current.Next;
- }
- Console.WriteLine();
- }
- public void DisplayBackward()
- {
- Console.Write("List (last-->first): ");
- Link<T> current = Last;
- while (current != null)
- {
- current.DisplayLink();
- current = current.Previous;
- }
- Console.WriteLine();
- }
- }
- }
- // Program.cs
- using System;
- namespace DoublyLinkedList
- {
- class Program
- {
- static void Main(string[] args)
- {
- DoublyLinkedList<int> L = new DoublyLinkedList<int>();
- char esc;
- string str;
- do
- {
- Console.WriteLine("0. Usuwanie wszystkich elementów listy");
- Console.WriteLine("1. Sprawdzanie czy lista jest pusta");
- Console.WriteLine("2. Wyszukiwanie elementu na liście ");
- Console.WriteLine("3. Wstawienie elementu na początek listy ");
- Console.WriteLine("4. Wstawienie elementu na koniec listy ");
- Console.WriteLine("5. Usuwanie elementu z początku listy ");
- Console.WriteLine("6. Usuwanie elementu z końca listy ");
- Console.WriteLine("7. Wstawienie elementu za wybranym elementem listy ");
- Console.WriteLine("8. Wstawienie elementu do listy w sposób uporządkowany");
- Console.WriteLine("9. Usuwanie wybranegp elementu z listy ");
- Console.WriteLine("10. Wyświetlanie listy od głowy");
- Console.WriteLine("11. Wyświetlanie listy od ogona");
- str = Console.ReadLine();
- int.TryParse(str, out int choice);
- switch(choice)
- {
- case 0:
- {
- while (!L.IsEmpty())
- L.DeleteFirst();
- break;
- }
- case 1:
- {
- Console.WriteLine(L.IsEmpty());
- break;
- }
- case 2:
- {
- Console.WriteLine("Podaj klucz węzła jaki chcesz wyszukać na liście");
- str = Console.ReadLine();
- int.TryParse(str, out int key);
- if (L.Find(key) == null)
- Console.WriteLine("Węzła o podanym kluczu nie znaleziono");
- else
- Console.WriteLine("Węzeł o podanym kluczu znaleziono");
- break;
- }
- case 3:
- {
- Console.WriteLine("Podaj klucz węzła jaki chcesz wstawić");
- str = Console.ReadLine();
- int.TryParse(str, out int dd);
- L.InsertFirst(dd);
- break;
- }
- case 4:
- {
- Console.WriteLine("Podaj klucz węzła jaki chcesz wstawić");
- str = Console.ReadLine();
- int.TryParse(str, out int dd);
- L.InsertLast(dd);
- break;
- }
- case 5:
- {
- Link<int> temp = L.DeleteFirst();
- if (temp == null)
- Console.WriteLine("Węzła nie udało się usunąć");
- else
- {
- Console.WriteLine("Usunięto węzeł:");
- temp.DisplayLink();
- Console.WriteLine();
- }
- break;
- }
- case 6:
- {
- Link<int> temp = L.DeleteLast();
- if (temp == null)
- Console.WriteLine("Węzła nie udało się usunąć");
- else
- {
- Console.WriteLine("Usunięto węzeł:");
- temp.DisplayLink();
- Console.WriteLine();
- }
- break;
- }
- case 7:
- {
- Console.WriteLine("Podaj klucz węzła za którym chcesz wstawić węzeł do listy");
- str = Console.ReadLine();
- int.TryParse(str, out int key);
- Console.WriteLine("Podaj klucz węzła jaki chcesz wstawić do listy");
- str = Console.ReadLine();
- int.TryParse(str, out int dd);
- Console.WriteLine(key.ToString() + " " +dd.ToString());
- bool isInserted = L.InsertAfter(key, dd);
- if (isInserted)
- Console.WriteLine("Węzeł udało się wstawić");
- else
- Console.WriteLine("Węzła nie udało się wstawić");
- break;
- }
- case 8:
- {
- Console.WriteLine("Podaj klucz węzła jaki chcesz wstawić");
- str = Console.ReadLine();
- int.TryParse(str, out int dd);
- L.Insert(dd);
- break;
- }
- case 9:
- {
- Console.WriteLine("Podaj klucz węzła jaki chcesz usunąć");
- str = Console.ReadLine();
- int.TryParse(str, out int dd);
- Link<int> temp = L.DeleteKey(dd);
- if (temp == null)
- Console.WriteLine("Węzła nie udało się usunąć");
- else
- {
- Console.WriteLine("Usunięto węzeł:");
- temp.DisplayLink();
- Console.WriteLine();
- }
- break;
- }
- case 10:
- {
- L.DisplayForward();
- break;
- }
- case 11:
- {
- L.DisplayBackward();
- break;
- }
- default:
- {
- Console.WriteLine("Brak operacji na liście");
- break;
- }
- }
- esc = (char)Console.ReadKey().Key;
- }
- while (esc != (char)ConsoleKey.Escape);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment