VitalyD

Untitled

May 13th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp14
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             LinkedList list = new LinkedList();
  14.  
  15.  
  16.             list.Add(105);
  17.             list.Add(10);
  18.             list.Add(5);
  19.             list.Add(4);
  20.  
  21.             list.Remove(10);
  22.             list.Contains(3);
  23.             list.Show();
  24.  
  25.  
  26.             Console.ReadLine();
  27.             //Node current = GetRoot();
  28.         }
  29.  
  30.         //static Node GetRoot()
  31.         //{
  32.         //    return new Node(0,
  33.         //        new Node(1,
  34.         //        new Node(2,
  35.         //        new Node(3,
  36.         //        new Node(4,
  37.         //        new Node(5, null))))));
  38.         //}
  39.  
  40.     }
  41.  
  42.     class LinkedList
  43.     {
  44.         private Node _root;
  45.         public int Count { get; private set; }
  46.  
  47.         public void Add(int value)
  48.         {
  49.             Node curNode = _root;
  50.  
  51.             if (curNode != null)
  52.             {
  53.                 while(curNode.Next != null)
  54.                 {
  55.                     curNode = curNode.Next;
  56.                 }
  57.                 curNode.Next = new Node(value, null);
  58.             }
  59.             else
  60.             {
  61.                 _root = new Node(value, null);
  62.             }
  63.         }
  64.  
  65.         public void Remove(int value)
  66.         {
  67.             Node curNode = _root;
  68.  
  69.             if (curNode.Data == value)
  70.             {
  71.                 curNode = curNode.Next;
  72.             }
  73.             else
  74.             {
  75.                 Node removeNode = SeachNode(value);
  76.                 if (removeNode.Next != null)
  77.                 {
  78.                     removeNode.Next = removeNode.Next.Next;
  79.                 }
  80.                 else
  81.                 {
  82.                     Console.WriteLine("Такого элемента нет");
  83.                 }
  84.                    
  85.             }
  86.         }
  87.  
  88.         public void RemoveAt(int index)
  89.         {
  90.         }
  91.  
  92.         public void Reverse()
  93.         {
  94.  
  95.         }
  96.  
  97.         public bool Contains(int value)
  98.         {
  99.             Node currentNode = _root;
  100.             while (currentNode.Data != value)
  101.             {
  102.                 currentNode = currentNode.Next;
  103.                 if (currentNode.Next == null)
  104.                 {
  105.                     break;
  106.                 }
  107.             }
  108.             if (currentNode.Data == value)
  109.             {
  110.                 Console.WriteLine(true);
  111.                 return true;
  112.             } else
  113.             {
  114.                 Console.WriteLine(false);
  115.                 return false;
  116.             }          
  117.         }
  118.  
  119.         public Node SeachNode(int value)
  120.         {
  121.             Node curNode = _root;
  122.             while (curNode.Next != null)
  123.             {
  124.                 if (curNode.Next.Data != value)
  125.                 {
  126.                     curNode = curNode.Next;
  127.                 }
  128.                 else
  129.                 {
  130.                     return curNode;
  131.                 }
  132.             }
  133.             return curNode;
  134.         }
  135.  
  136.  
  137.         public void Show ()
  138.         {
  139.             Node currentNode = _root;
  140.             while (currentNode != null)
  141.             {
  142.                 Console.Write(currentNode.Data + " ");
  143.                 currentNode = currentNode.Next;
  144.             }
  145.         }
  146.     }
  147.  
  148.     class Node
  149.     {
  150.         public Node Next;
  151.         public int Data;
  152.  
  153.         public Node(int data, Node next)
  154.         {
  155.             Next = next;
  156.             Data = data;
  157.         }
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment