Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1. namespace list_lab_1
  2. {
  3.     public class myCell {
  4.         public string element;
  5.         public myCell next;
  6.     }
  7.  
  8.     public class myList {
  9.  
  10.         public myCell Head = new myCell();
  11.  
  12.         public string GetList() {
  13.             myCell tmp = Head;
  14.             string str = " ";
  15.             while (tmp.next != null) {
  16.                 tmp = tmp.next;
  17.                 str = tmp.element + ",";
  18.             }
  19.             return str;
  20.         }
  21.  
  22.         public int Count (){
  23.             myCell tmp = Head;
  24.             int i = 0;
  25.             while (tmp.next != null) {
  26.                 tmp = tmp.next;
  27.                 i++;
  28.             }
  29.             return i;
  30.         }
  31.  
  32.         public myCell Add(string val, myCell pos) {
  33.             myCell tmp = new myCell();
  34.             tmp.element = val;
  35.             tmp.next = pos.next;
  36.             pos.next = tmp;
  37.             return tmp;
  38.         }
  39.  
  40.         public void Remove(myCell pos) {
  41.             myCell tmp = Head;
  42.             while (tmp.next != pos) {
  43.                 tmp = tmp.next;
  44.             }
  45.             tmp.next = tmp.next.next;
  46.         }
  47.  
  48.         public void Move(myCell pos, myCell after) {
  49.             Remove(pos);
  50.             Add(pos.element, after);
  51.         }
  52.  
  53.         public class Program {
  54.             static void Main(string[] args)
  55.             {
  56.                 Console.ForegroundColor = ConsoleColor.Yellow;
  57.                 Console.WriteLine("The list of pets");
  58.                 Console.ResetColor();
  59.  
  60.                 Console.WriteLine("Please enter the appropriate number where:" +
  61.                     "\n 1 - adding one new element to the list;" +
  62.                     "\n 2 - removing one element to the list;" +
  63.                     "\n 3 - moving one element in the list;" +
  64.                     "\n 4 - showing all the elements of the list;" +
  65.                     "\n 5 - counting all the elements of the list.");
  66.  
  67.                 myList List = new myList();
  68.                 myCell cell = new myCell();
  69.                 myCell cell1 = new myCell();
  70.  
  71.                 while (true)
  72.                 {
  73.                     int choice = Convert.ToInt32(Console.ReadLine());
  74.                     switch (choice)
  75.                     {
  76.                         case 1:
  77.                             Console.WriteLine("Please, input the name of the pet");
  78.                             if (Convert.ToInt32(List.GetList()) == 1)
  79.                             {
  80.                                 List.Add(Console.ReadLine(), List.Head);
  81.                             }
  82.                             else
  83.                             {
  84.                                 List.Add(Console.ReadLine(), cell);
  85.                             }
  86.                             break;
  87.  
  88.                         case 2:
  89.                             List.Remove(cell1);
  90.                             break;
  91.  
  92.                         case 3:
  93.                             List.Move(cell, cell1);
  94.                             break;
  95.  
  96.                         case 4:
  97.                             Console.WriteLine(List.GetList());
  98.                             break;
  99.  
  100.                         case 5:
  101.                             Console.WriteLine(Convert.ToString(List.Count()));
  102.                             break;
  103.  
  104.                         default:
  105.                             Console.WriteLine("unknown command; try again");
  106.                             break;
  107.                     }
  108.                 }
  109.                 Console.ReadKey();
  110.             }
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement