Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.40 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 num
  8. {
  9.     class Node
  10.     {
  11.         public Node next;
  12.         public int data;
  13.         public int index;
  14.  
  15.         public Node() : this(null, 0, 0) { }
  16.  
  17.         public Node(int data, int index) : this(null, data, index) { }
  18.  
  19.         public Node(Node next, int data) : this(next, data, 0) { }
  20.  
  21.         public Node(Node next, int data, int index)
  22.         {
  23.             this.data = data;
  24.             this.next = next;
  25.             this.index = index;
  26.         }
  27.     }
  28.  
  29.     class OurList
  30.     {
  31.         private int size = 0;
  32.         private Node root = null;
  33.         private Node mirror(Node _temp)
  34.         {
  35.             Node temp = null;
  36.             while (_temp != null)
  37.             {
  38.                 temp = new Node(temp, _temp.data, _temp.index);
  39.                 _temp = _temp.next;
  40.             }
  41.             return temp;
  42.         }
  43.      
  44.         public int Size
  45.         {
  46.             get => size;
  47.             private set => size = value;
  48.         }
  49.         public void DeleteList()
  50.         {
  51.             root = null;
  52.         }
  53.         public void Add(int data, int index)
  54.         {
  55.            
  56.             if (root == null)
  57.             {
  58.                 ++size;
  59.                 root = new Node(data, index);
  60.                
  61.             }
  62.             else if (index == Find(index).index)
  63.             {
  64.                 Console.WriteLine("error index " + data + ":"+ index + " was already added" );
  65.                 return;
  66.             }
  67.             else
  68.             {
  69.                 ++size;
  70.                 root = new Node(root, data, index);
  71.             }
  72.          
  73.         }
  74.         public void Add(int data)
  75.         {
  76.             if (root == null)
  77.             {
  78.                 ++size;
  79.                 root = new Node(data, size);
  80.                
  81.             }
  82.             else
  83.             {
  84.                 ++size;
  85.                 root = new Node(root, data, size);
  86.              
  87.             }
  88.         }
  89.         public Node Find(int index)
  90.         {
  91.             Node save = new Node(-1, -1);
  92.             Node temp = root;
  93.             while (temp != null )
  94.             {
  95.                 if (temp.index == index)
  96.                     return temp;
  97.                 temp = temp.next;
  98.             }
  99.             return save;
  100.            
  101.  
  102.         }
  103.         public void Print()
  104.         {
  105.             Node temp = mirror(root);
  106.             while (temp != null)
  107.             {
  108.                 Console.WriteLine(" data:{0,5} index:{1,5}",temp.data,temp.index );
  109.                 temp = temp.next;
  110.             }
  111.             Console.WriteLine();
  112.         }
  113.         public void DeleteFirstElement()
  114.         {
  115.             root = First.next;
  116.             root = First;
  117.         }
  118.         public Node First
  119.         {
  120.             get => mirror(root);
  121.         }
  122.         public override string ToString()
  123.         {
  124.             return base.ToString();
  125.         }
  126.     }
  127.  
  128.     class Program
  129.     {
  130.         static void Main(string[] args)
  131.         {
  132.             OurList a = new OurList();
  133.  
  134.            
  135.            
  136.             a.Add(15,4);
  137.             a.Add(152);
  138.             a.Add(123, 3);
  139.             a.Add(1534, 25);
  140.             a.Add(1534, 43);
  141.             a.Add(1534, 20);
  142.             a.Print();
  143.            
  144.                
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement