Advertisement
Guest User

AiSD1_2

a guest
Sep 18th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using System;
  2.  
  3. namespace AiSD1
  4. {
  5.     class Program
  6.     {
  7.         public class MyList
  8.         {
  9.             public char data = '0';
  10.             public MyList Next = null;
  11.         }
  12.         public static void Add(char c, MyList Head)
  13.         {
  14.             if(Head.data == '0')
  15.             {
  16.                 Head.data = c;
  17.                 Head.Next = null;
  18.                 return;
  19.             }
  20.             if(Head.Next == null)
  21.             {
  22.                 MyList newElement = new MyList();
  23.                 newElement.data = c;
  24.                 Head.Next = newElement;
  25.                 newElement.Next = null;
  26.                 if (c < Head.data)
  27.                 {
  28.                     newElement.data = Head.data;
  29.                     Head.data = c;
  30.                 }
  31.                 return;
  32.             }
  33.             while(Head.Next.data < c)
  34.             {
  35.                 Head = Head.Next;
  36.             }
  37.             MyList newElem = new MyList();
  38.             newElem.data = c;
  39.             newElem.Next = Head.Next;
  40.             Head.Next = newElem;
  41.         }
  42.         public static bool Remove(char c, MyList Head)
  43.         {
  44.             while (Head.Next.data != c)
  45.             {
  46.                 if (Head.Next.Next == null)
  47.                 {
  48.                     return false;
  49.                 }
  50.                 Head = Head.Next;
  51.             }
  52.             Head.Next = Head.Next.Next;
  53.             return true;
  54.         }
  55.         public static int FindePos(char c, MyList Head)
  56.         {
  57.             int i = 0;
  58.             while (Head.data != c)
  59.             {
  60.                 i++;
  61.                 if (Head.Next == null)
  62.                 {
  63.                     return -1;
  64.                 }
  65.                 Head = Head.Next;
  66.             }
  67.             return i;
  68.         }
  69.         public static void Clear(MyList Head)
  70.         {
  71.             Head = null;
  72.         }
  73.  
  74.         static void Main(string[] args)
  75.         {
  76.             MyList Head = new MyList();
  77.             Add('f', Head);
  78.             Add('a', Head);
  79.             Add('b', Head);
  80.             Add('c', Head);
  81.             Remove('f', Head);
  82.             Console.WriteLine((FindePos('c', Head) + 1).ToString());
  83.             while(Head != null)
  84.             {
  85.                 Console.Write(Head.data.ToString() + ' '.ToString());
  86.                 Head = Head.Next;
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement