Advertisement
Guest User

AiSD1_1

a guest
Oct 20th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.15 KB | None | 0 0
  1. using System;
  2.  
  3. namespace AiSD1
  4. {
  5.     class Program
  6.     {
  7.         public class Stec
  8.         {
  9.             int[] S = new int[100];
  10.             int top = 0;
  11.             public void PUSH(int x)
  12.             {
  13.                 S[top] = x;
  14.                 top++;
  15.             }
  16.             public bool POP()
  17.             {
  18.                 if (!ISEMPTY())
  19.                 {
  20.                     top--;
  21.                     return true;
  22.                 }
  23.                 else
  24.                 {
  25.                     return false;
  26.                 }
  27.             }
  28.             public bool ISEMPTY()
  29.             {
  30.                 if (top == 0)
  31.                 {
  32.                     return true;
  33.                 }
  34.                 else
  35.                 {
  36.                     return false;
  37.                 }
  38.             }
  39.             public void CLAER()
  40.             {
  41.                 top = 0;
  42.             }
  43.             public int TOP()
  44.             {
  45.                 return S[top - 1];
  46.             }
  47.         }
  48.         public class MyList
  49.         {
  50.             public char data = '0';
  51.             public MyList Next = null;
  52.         }
  53.         public static void Add(char c, MyList Head)
  54.         {
  55.             if(Head.data == '0')
  56.             {
  57.                 Head.data = c;
  58.                 Head.Next = null;
  59.                 return;
  60.             }
  61.             if(Head.Next == null)
  62.             {
  63.                 MyList newElement = new MyList();
  64.                 newElement.data = c;
  65.                 Head.Next = newElement;
  66.                 newElement.Next = null;
  67.                 if (c < Head.data)
  68.                 {
  69.                     newElement.data = Head.data;
  70.                     Head.data = c;
  71.                 }
  72.                 return;
  73.             }
  74.             if(Head.data > c)
  75.             {
  76.                 //сделать новый элемент после Head, записать туда данные из Head, а в Head записать данные нового элемента
  77.                 MyList tmp = Head.Next;
  78.                 Head.Next = new MyList();
  79.                 Head.Next.Next = tmp;
  80.                 Head.Next.data = Head.data;
  81.                 Head.data = c;
  82.             }
  83.             while(Head.Next.data < c)
  84.             {
  85.                 Head = Head.Next;
  86.             }
  87.             MyList newElem = new MyList();
  88.             newElem.data = c;
  89.             newElem.Next = Head.Next;
  90.             Head.Next = newElem;
  91.         }
  92.         public static bool Remove(char c, MyList Head)
  93.         {
  94.             if(Head == null || Head.data == '0')
  95.             {
  96.                 return false;
  97.             }
  98.             if(Head.data == c)
  99.             {
  100.                 Head.data = Head.Next.data;
  101.                 Head.Next = Head.Next.Next;
  102.                 return true;
  103.             }
  104.             while (Head.Next.data != c)
  105.             {
  106.                 if (Head.Next.Next == null)
  107.                 {
  108.                     return false;
  109.                 }
  110.                 Head = Head.Next;
  111.             }
  112.             Head.Next = Head.Next.Next;
  113.             return true;
  114.         }
  115.         public static int FindePos(char c, MyList Head)
  116.         {
  117.             int i = 0;
  118.             while (Head.data != c)
  119.             {
  120.                 i++;
  121.                 if (Head.Next == null)
  122.                 {
  123.                     return -1;
  124.                 }
  125.                 Head = Head.Next;
  126.             }
  127.             return i;
  128.         }
  129.         public static void Clear(MyList Head)
  130.         {
  131.             Head = null;
  132.         }
  133.  
  134.         static void Main(string[] args)
  135.         {
  136.             //Stec S = new Stec();
  137.  
  138.             //Console.WriteLine("Put -1 to close");
  139.             //Console.WriteLine("Enter comand: ");
  140.             //string s = Console.ReadLine();
  141.             //string[] sw = new string[2];
  142.  
  143.             //while(s != "-1")
  144.             //{
  145.             //    sw = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  146.             //    switch (sw[0])
  147.             //    {
  148.             //        case "PUSH":
  149.             //            S.PUSH(sw[1][0] - '0');
  150.             //            break;
  151.             //        case "POP":
  152.             //            S.POP();
  153.             //            break;
  154.             //        case "TOP":
  155.             //            Console.WriteLine(S.TOP());
  156.             //            break;
  157.             //        case "CLEAR":
  158.             //            S.CLAER();
  159.             //            break;
  160.             //        case "ISEMPTY":
  161.             //            Console.WriteLine(S.ISEMPTY());
  162.             //            break;
  163.             //    }
  164.             //    Console.WriteLine("Enter comand: ");
  165.             //    s = Console.ReadLine();
  166.             //}
  167.  
  168.             MyList Head = new MyList();
  169.             Add('b', Head);
  170.             Add('c', Head);
  171.             Add('a', Head);
  172.             Remove('f', Head);
  173.             Console.WriteLine((FindePos('c', Head) + 1).ToString());
  174.             while (Head != null)
  175.             {
  176.                 Console.Write(Head.data.ToString() + ' '.ToString());
  177.                 Head = Head.Next;
  178.             }
  179.         }
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement