Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8. class Stack <T>
  9. {
  10. private node<T> First;
  11. public Stack()
  12. {
  13. First = null;
  14. }
  15. public bool IsEmpty()
  16. {
  17. if (First != null)
  18. return false;
  19. return true;
  20. }
  21. public T Top()
  22. {
  23. return this.First.GetValue();
  24. }
  25. public string ToString()
  26. {
  27. string s = "";
  28. while (First != null)
  29. {
  30. s = s + First.GetValue();
  31. if (First.GetNext() != null)
  32. {
  33. First = First.GetNext();
  34. s = s + " , ";
  35. }
  36. }
  37. return s;
  38. }
  39. public void Push(T x)
  40. {
  41. if (this.Top() == null)
  42. First.SetValue(x);
  43. node<T> s = new node<T>(x, First);
  44. s = First;
  45. }
  46. public T Pop()
  47. {
  48. node<T> x = First;
  49. First = null;
  50. return x.GetValue();
  51. }
  52. }
  53. class node<T>
  54. {
  55. private T Value;
  56. private node<T> next;
  57. public node(T X)
  58. {
  59. this.Value = X;
  60. this.next = null;
  61. }
  62. public node(T X, node<T> next)
  63. {
  64. this.Value = X;
  65. this.next = next;
  66. }
  67. public T GetValue()
  68. {
  69. return this.Value;
  70. }
  71. public node<T> GetNext()
  72. {
  73. return this.next;
  74. }
  75. public void SetValue(T x)
  76. {
  77. this.Value = x;
  78. }
  79. public void SetNext(node<T> next)
  80. {
  81. this.next = next;
  82. }
  83. public bool HasNext()
  84. {
  85. if (this.next == null)
  86. {
  87. return false;
  88. }
  89. return true;
  90. }
  91. public override string ToString()
  92. {
  93. return "(the value is " + this.Value + ")";
  94. }
  95. static public bool Alef1(node<T> P, T n)
  96. {
  97. while (P != null)
  98. {
  99. if (n.Equals(P.GetValue()))
  100. {
  101. P = P.GetNext();
  102. return true;
  103. }
  104. P = P.GetNext();
  105. return false;
  106. }
  107. if (n.Equals(P.GetValue()))
  108. return true;
  109. return false;
  110. }
  111. static public int Bet1(node<T> P, T n)
  112. {
  113. int counter = 0;
  114. while (P != null)
  115. {
  116. if (n.Equals(P.GetValue()))
  117. return counter;
  118. else
  119. {
  120. counter++;
  121. P = P.GetNext();
  122. }
  123. }
  124. return -1;
  125. }
  126. static public void Gimel1(node<T> P, T n, T x)
  127. {
  128. if (Alef1(P, n))
  129. {
  130. int place = Bet1(P, n);
  131. for (int i = 0; i < place; i++)
  132. {
  133. P = P.GetNext();
  134. }
  135. P.SetValue(x);
  136. }
  137. }
  138. static public int GetLength(node<T> P)
  139. {
  140. int i = 0;
  141. while (P != null)
  142. {
  143. i++;
  144. P = P.GetNext();
  145. }
  146. return i;
  147. }
  148. static public void Delete(node<T> P, int n)
  149. {
  150. while (P != null)
  151. {
  152. if (P.GetNext().GetValue().Equals(n))
  153. P.SetNext(P.GetNext().GetNext());
  154. }
  155. }
  156. static public void Change(node<T> P, int n)//O(n)
  157. {
  158. T value; T value1;
  159. for (int i = 0; i < n; i++)
  160. {
  161. P = P.GetNext();
  162. }
  163. value = P.GetValue();
  164. value1 = P.GetNext().GetValue();
  165. P.SetValue(value1);
  166. P.GetNext().SetValue(value);
  167. }
  168. public static void connect(node<T> P, node<T> M)
  169. {
  170. while (P.GetNext() != null)
  171. {
  172. P = P.GetNext();
  173. }
  174. P.GetNext().SetNext(M);
  175. }
  176. public static node<T> Q5(node<T> P, node<T> M)
  177. {
  178. if (P == null)
  179. return M;
  180. if (M == null)
  181. return P;
  182. node<T> mrgList;
  183. if (P.GetValue() < M.GetValue())
  184. {
  185. mrgList = P;
  186. P = P.GetNext();
  187. }
  188. else
  189. {
  190. mrgList = M;
  191. M = M.GetNext();
  192. }
  193. node<T> posMrg = mrgList;
  194. while (P != null && M != null)
  195. {
  196. if (P.GetValue() < M.GetValue())
  197. {
  198. posMrg.SetNext(P);
  199. P = P.GetNext();
  200. }
  201. else
  202. {
  203. posMrg.SetNext(M);
  204. M = M.GetNext();
  205. }
  206. posMrg = posMrg.GetNext();
  207. }
  208. while (P != null)
  209. {
  210. posMrg.SetNext(P);
  211. P = P.GetNext();
  212. posMrg = posMrg.GetNext();
  213. }
  214. while (M != null)
  215. {
  216. posMrg.SetNext(M);
  217. M = M.GetNext();
  218. posMrg = posMrg.GetNext();
  219. }
  220. return mrgList;
  221. }
  222. public static node<T> Q6(node<T> P, node<T> M)
  223. {
  224. {
  225. if (P == null || M == null)
  226. return null;
  227. node<T> cutList;
  228. while (P != null && M != null)
  229. {
  230. if (P.GetValue().Equals(M.GetValue()))
  231. {
  232. cutList = P.GetValue();
  233. P = P.GetNext();
  234. M = M.GetNext();
  235. }
  236. }
  237. return cutList.GetNext();
  238. }
  239.  
  240. }
  241. }
  242. class Program
  243. {
  244.  
  245.  
  246. static public node<int> shirsore(node<int> p, node<int> p1)
  247. {
  248. node<int> l = p;
  249. while (p != null)
  250. {
  251. p = p.GetNext();
  252. }
  253. p.SetNext(p1);
  254. return l;
  255. }
  256. static public void print(node<int> p)
  257. {
  258. if (p == null) return;
  259. while (p != null)
  260. {
  261. Console.WriteLine(p.ToString());
  262. p = p.GetNext();
  263. }
  264. }
  265. static void Main(string[] args)
  266. {
  267. node<int> p = new node<int>(4);
  268. node<int> p1 = new node<int>(2, p);
  269. node<int> p2 = new node<int>(8, p1);
  270. }
  271. }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement