Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace list_lab_1
- {
- public class myCell {
- public string element;
- public myCell next;
- }
- public class myList {
- public myCell Head = new myCell();
- public string GetList() {
- myCell tmp = Head;
- string str = " ";
- while (tmp.next != null) {
- tmp = tmp.next;
- str = tmp.element + ",";
- }
- return str;
- }
- public int Count (){
- myCell tmp = Head;
- int i = 0;
- while (tmp.next != null) {
- tmp = tmp.next;
- i++;
- }
- return i;
- }
- public myCell Add(string val, myCell pos) {
- myCell tmp = new myCell();
- tmp.element = val;
- tmp.next = pos.next;
- pos.next = tmp;
- return tmp;
- }
- public void Remove(myCell pos) {
- myCell tmp = Head;
- while (tmp.next != pos) {
- tmp = tmp.next;
- }
- tmp.next = tmp.next.next;
- }
- public void Move(myCell pos, myCell after) {
- Remove(pos);
- Add(pos.element, after);
- }
- public class Program {
- static void Main(string[] args)
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("The list of pets");
- Console.ResetColor();
- Console.WriteLine("Please enter the appropriate number where:" +
- "\n 1 - adding one new element to the list;" +
- "\n 2 - removing one element to the list;" +
- "\n 3 - moving one element in the list;" +
- "\n 4 - showing all the elements of the list;" +
- "\n 5 - counting all the elements of the list.");
- myList List = new myList();
- myCell cell = new myCell();
- myCell cell1 = new myCell();
- while (true)
- {
- int choice = Convert.ToInt32(Console.ReadLine());
- switch (choice)
- {
- case 1:
- Console.WriteLine("Please, input the name of the pet");
- if (Convert.ToInt32(List.GetList()) == 1)
- {
- List.Add(Console.ReadLine(), List.Head);
- }
- else
- {
- List.Add(Console.ReadLine(), cell);
- }
- break;
- case 2:
- List.Remove(cell1);
- break;
- case 3:
- List.Move(cell, cell1);
- break;
- case 4:
- Console.WriteLine(List.GetList());
- break;
- case 5:
- Console.WriteLine(Convert.ToString(List.Count()));
- break;
- default:
- Console.WriteLine("unknown command; try again");
- break;
- }
- }
- Console.ReadKey();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement