Advertisement
desislava_topuzakova

CustomList

Jun 27th, 2020
1,651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace ExamPreparation2
  6. {
  7.     class CustomList
  8.     {
  9.         //data – стринг масив
  10.         //count – брой
  11.         //capacity – капацитет
  12.         private List<string> data;
  13.         private int count;
  14.  
  15.         public CustomList()
  16.         {
  17.             data = new List<string>();
  18.             count = 0;
  19.         }
  20.  
  21.         public int Count
  22.         {
  23.             get
  24.             {
  25.                 return count;
  26.             }
  27.             set
  28.             {
  29.                 count = value;
  30.             }
  31.         }
  32.        
  33.  
  34.         public void Add(string element)
  35.         {
  36.             data.Add(element);
  37.  
  38.  
  39.         }
  40.  
  41.         public bool Contains(string element)
  42.         {
  43.             return data.Contains(element);
  44.         }
  45.  
  46.         public bool RemoveByIndex(int index)
  47.         {
  48.             string elementToRemove = data[index];
  49.             return data.Remove(elementToRemove);
  50.         }
  51.  
  52.         public void ReplaceLastWithFirst()
  53.         {
  54.             // 1,2,3,4 -> 4,2,3,1
  55.             string first = data[0];
  56.             string last = data[data.Count - 1];
  57.  
  58.             data[0] = last;
  59.             data[data.Count - 1] = first;
  60.         }
  61.  
  62.         public StringBuilder Info()
  63.         {
  64.             StringBuilder builder = new StringBuilder();
  65.             //CustomList has total count of - 2 and capacity - 2
  66.             builder.Append($"CustomList has total count of - {data.Count}\n");
  67.             foreach(string element in data)
  68.             {
  69.                 builder.Append(element + "\n");
  70.             }
  71.             return builder;
  72.         }
  73.  
  74.         public void Resize()
  75.         {
  76.             count = (count + 1) * 2;
  77.         }
  78.  
  79.  
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement