VitalyD

Untitled

May 19th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.58 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.             list.Add(105);
  16.             list.Add(10);
  17.             list.Add(5);
  18.             list.Add(4);
  19.  
  20.             list.Reverse();
  21.             list.Remove(10);
  22.             list.RemoveAt(2);
  23.             list.Contains(3);
  24.             list.Show();
  25.  
  26.  
  27.             Console.ReadLine();
  28.             //Node current = GetRoot();
  29.         }
  30.  
  31.         //static Node GetRoot()
  32.         //{
  33.         //    return new Node(0,
  34.         //        new Node(1,
  35.         //        new Node(2,
  36.         //        new Node(3,
  37.         //        new Node(4,
  38.         //        new Node(5, null))))));
  39.         //}
  40.  
  41.     }
  42.  
  43.     class LinkedList
  44.     {
  45.         private Node _root;
  46.         public int Count { get; private set; }
  47.  
  48.         public void Add(int value)
  49.         {
  50.             Node curNode = _root;
  51.  
  52.             if (curNode != null)
  53.             {
  54.                 while(curNode.Next != null)
  55.                 {
  56.                     curNode = curNode.Next;
  57.                 }
  58.                 curNode.Next = new Node(value, null);
  59.             }
  60.             else
  61.             {
  62.                 _root = new Node(value, null);
  63.             }
  64.         }
  65.  
  66.         public void Remove(int value)
  67.         {
  68.             Node curNode = _root;
  69.  
  70.             if (curNode.Data == value)
  71.             {
  72.                 curNode = curNode.Next;
  73.             }
  74.             else
  75.             {
  76.                 Node removeNode = SeachNode(value);
  77.                 if (removeNode.Next != null)
  78.                 {
  79.                     removeNode.Next = removeNode.Next.Next;
  80.                 }
  81.                 else
  82.                 {
  83.                     Console.WriteLine("Такого элемента нет");
  84.                 }
  85.                    
  86.             }
  87.         }
  88.  
  89.         public void Balblabla ()
  90.         {
  91.             Node currentNode = _root;
  92.             currentNode = currentNode.Next;
  93.         }
  94.  
  95.         public void RemoveAt(int index)
  96.         {
  97.             int count = 0;
  98.             Node currentNode = _root;
  99.             Node prevNode = currentNode;
  100.             while (count <= index)
  101.             {
  102.                 if (count == index)
  103.                 {
  104.                     prevNode.Next = currentNode.Next;
  105.                     currentNode = null;
  106.                 }
  107.                 else
  108.                 {
  109.                     prevNode = currentNode;
  110.                     currentNode = currentNode.Next;
  111.                 }
  112.                 count++;
  113.             }
  114.         }  
  115.  
  116.         public void Reverse()
  117.         {
  118.             Node curNode = _root, nextNodes = null;
  119.             while (curNode != null)
  120.             {
  121.                 Node stor = curNode.Next;
  122.                 curNode.Next = nextNodes;
  123.                 nextNodes = curNode;
  124.                 curNode = stor;
  125.             }
  126.             _root = nextNodes;
  127.         }
  128.  
  129.         public bool Contains(int value)
  130.         {
  131.             Node currentNode = _root;
  132.             while (currentNode.Data != value)
  133.             {
  134.                 currentNode = currentNode.Next;
  135.                 if (currentNode.Next == null)
  136.                 {
  137.                     break;
  138.                 }
  139.             }
  140.             if (currentNode.Data == value)
  141.             {
  142.                 Console.WriteLine(true);
  143.                 return true;
  144.             } else
  145.             {
  146.                 Console.WriteLine(false);
  147.                 return false;
  148.             }          
  149.         }
  150.  
  151.         public Node SeachNode(int value)
  152.         {
  153.             Node curNode = _root;
  154.             while (curNode.Next != null)
  155.             {
  156.                 if (curNode.Next.Data != value)
  157.                 {
  158.                     curNode = curNode.Next;
  159.                 }
  160.                 else
  161.                 {
  162.                     return curNode;
  163.                 }
  164.             }
  165.             return curNode;
  166.         }
  167.  
  168.         public void Show ()
  169.         {
  170.             Node currentNode = _root;
  171.             while (currentNode != null)
  172.             {
  173.                 Console.Write(currentNode.Data + " ");
  174.                 currentNode = currentNode.Next;
  175.             }
  176.         }
  177.     }
  178.  
  179.     class Node
  180.     {
  181.         public Node Next;
  182.         public int Data;
  183.  
  184.         public Node(int data, Node next)
  185.         {
  186.             Next = next;
  187.             Data = data;
  188.         }
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment