Advertisement
mrAnderson33

реализация класса List

Mar 14th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace lab03
  7. {
  8.     class A
  9.     {
  10.         int data;
  11.         public A(int data )
  12.         {
  13.             this.data = data;
  14.         }
  15.         public override string ToString()
  16.         {
  17.             return data.ToString();
  18.         }
  19.  
  20.     }
  21.     class  list
  22.     {
  23.  
  24.         private object[] arr;
  25.         public int count { get; private set; }
  26.         public int capacity { get; private set; }
  27.         public list()
  28.         {
  29.             arr = new object[0];
  30.         }
  31.  
  32.         public list (object item)
  33.         {
  34.             arr = new object[4];
  35.             arr[0] = item;
  36.             count = 1;
  37.             capacity = 4;
  38.         }
  39.  
  40.         public void Add(object item)
  41.         {
  42.            
  43.             if ((count < capacity))
  44.                 if (count == 1)
  45.                 arr[count-1] = item;
  46.             else
  47.             {
  48.                 capacity += 4;
  49.                 object[] temp = new object[capacity];
  50.                 Array.Copy(arr, temp, arr.Length);
  51.                 temp[count] = item;
  52.                 arr = new object[capacity];
  53.                 Array.Copy(temp, arr, temp.Length);
  54.              }
  55.             count++;
  56.            
  57.         }
  58.  
  59.         public void Print()
  60.         {
  61.             for (int i =0; i < arr.Length;++i) if (arr[i] != null) Console.WriteLine("arr[{0}]={1}",i,arr[i]);
  62.         }
  63.  
  64.         public void PrintPropertys()
  65.         {
  66.             Console.WriteLine("count = {0}       capacity={1}", count, capacity);
  67.         }
  68.         public override string ToString()
  69.         {
  70.             string res = "";
  71.             foreach (var n in arr) { res = res + n + "\n"; }
  72.             return res;
  73.         }
  74.         public void Clear()
  75.         {
  76.             for (int i = 0; i <= count; ++i) arr[i] = null;
  77.                 count = 0;
  78.         }
  79.         public bool Contains (object item)
  80.         {
  81.             foreach (var n in arr) if ((n != null) && (item == n)) return true;
  82.             return false;
  83.         }
  84.     public bool Remove(object item)
  85.         {
  86.            
  87.                
  88.                 for (int i = 0;i < count; ++i)
  89.                 {
  90.                     if ((arr[i]!=null) && (arr[i] == item))
  91.                     {
  92.                         Array.Copy(arr, i + 1, arr, i, count - i  - 1);
  93.                         arr[count--] = null;
  94.                         return true;
  95.                     }
  96.                 }
  97.                 return false;
  98.                
  99.            
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement