Advertisement
Guest User

AiSD1_2

a guest
Sep 18th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 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.             while(Head.Next.data < c)
  75.             {
  76.                 Head = Head.Next;
  77.             }
  78.             MyList newElem = new MyList();
  79.             newElem.data = c;
  80.             newElem.Next = Head.Next;
  81.             Head.Next = newElem;
  82.         }
  83.         public static bool Remove(char c, MyList Head)
  84.         {
  85.             if(Head == null || Head.data == '0')
  86.             {
  87.                 return false;
  88.             }
  89.             if(Head.data == c)
  90.             {
  91.                 Head.data = Head.Next.data;
  92.                 Head.Next = Head.Next.Next;
  93.                 return true;
  94.             }
  95.             while (Head.Next.data != c)
  96.             {
  97.                 if (Head.Next.Next == null)
  98.                 {
  99.                     return false;
  100.                 }
  101.                 Head = Head.Next;
  102.             }
  103.             Head.Next = Head.Next.Next;
  104.             return true;
  105.         }
  106.         public static int FindePos(char c, MyList Head)
  107.         {
  108.             int i = 0;
  109.             while (Head.data != c)
  110.             {
  111.                 i++;
  112.                 if (Head.Next == null)
  113.                 {
  114.                     return -1;
  115.                 }
  116.                 Head = Head.Next;
  117.             }
  118.             return i;
  119.         }
  120.         public static void Clear(MyList Head)
  121.         {
  122.             Head = null;
  123.         }
  124.  
  125.         static void Main(string[] args)
  126.         {
  127.             Stec S = new Stec();
  128.             S.PUSH(1);
  129.             S.PUSH(2);
  130.             S.PUSH(3);
  131.             S.PUSH(4);
  132.             S.POP();
  133.             Console.WriteLine(S.TOP());
  134.             S.PUSH(17);
  135.             Console.WriteLine(S.TOP());
  136.             Console.WriteLine(S.ISEMPTY());
  137.             S.CLAER();
  138.             Console.WriteLine(S.ISEMPTY());
  139.             Console.WriteLine(' ');
  140.             MyList Head = new MyList();
  141.  
  142.             Remove('b', Head);
  143.             Console.WriteLine((FindePos('c', Head) + 1).ToString());
  144.             while(Head != null)
  145.             {
  146.                 Console.Write(Head.data.ToString() + ' '.ToString());
  147.                 Head = Head.Next;
  148.             }
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement