Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Kirill3
  7. {
  8.     class Program
  9.     {
  10.         static void Main()
  11.         {
  12.             LinkedList<int> list = new LinkedList<int>();
  13.             list.AddFirst(1);
  14.             list.AddFirst(2);
  15.             list.AddLast(3);
  16.             foreach (var item in list)
  17.             {
  18.                 Console.WriteLine(item);
  19.             }
  20.  
  21.             LinkedListMine l = new LinkedListMine();
  22.             l.AddLast(1);
  23.             l.AddLast(2);
  24.             l.AddLast(3);
  25.             l.AddFirst(4);
  26.             l.AddLast(5);
  27.             l.Print();
  28.            
  29.  
  30.             //пример
  31.         }
  32.  
  33.  
  34.         class LinkedListMine
  35.         {
  36.             public Node First;
  37.             public Node Last;
  38.  
  39.             public void AddFirst(int i)
  40.             {
  41.                 if(First == null)
  42.                 {
  43.                     First = new Node(i);
  44.                     Last = First;
  45.                     return;
  46.                 }
  47.  
  48.                 var newFirst = new Node(i);
  49.                 First.Prev = newFirst;
  50.                 newFirst.Next = First;
  51.                 First = newFirst;
  52.             }
  53.  
  54.             //добавить функцию ADD AFTER
  55.             public void AddAfter(Node node, int value)
  56.             {
  57.  
  58.             }
  59.  
  60.             //добавить функцию Find
  61.             public Node Find(int value)
  62.             {
  63.                 return null;
  64.             }
  65.  
  66.             public void AddAfter(int valueAfter, int value)
  67.             {
  68.                 AddAfter(Find(valueAfter), value);
  69.             }
  70.  
  71.  
  72.  
  73.             //Улучшить с помощтю ссылки на Last
  74.             public void AddLast(int i)
  75.             {
  76.                 if(First == null)
  77.                 {
  78.                     First = new Node(i);
  79.                     Last = First;
  80.                     return;
  81.                 }
  82.  
  83.                 Node prev = First;
  84.                 Node current = First.Next;
  85.                 while(current != null)
  86.                 {
  87.                     prev = current;
  88.                     current = current.Next;
  89.                 }
  90.                 current = new Node(i);
  91.                 prev.Next = current;
  92.                 current.Prev = prev;
  93.  
  94.                 Last = current;
  95.             }
  96.  
  97.             public void Print()
  98.             {
  99.                 Node current = First;
  100.                 Console.WriteLine();
  101.                 while (current != null)
  102.                 {
  103.                     Console.Write(current.Value);
  104.                     Console.Write(" ");
  105.                     current = current.Next;
  106.                 }
  107.                 Console.WriteLine();
  108.             }
  109.         }
  110.  
  111.         class Node
  112.         {
  113.             public int Value;
  114.             public Node Next;
  115.             public Node Prev;
  116.  
  117.             public Node(int value)
  118.             {
  119.                 Value = value;
  120.             }
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement