Advertisement
J00ker

S, Q, L

Apr 23rd, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. //--------------Stiva--------------
  2. struct Stiva
  3. {
  4. int st[Nmax];
  5. int top;
  6.  
  7. void Init()
  8. {
  9. top = -1;
  10. }
  11.  
  12. int Empty()
  13. {
  14. if(top == -1) return 1;
  15. return 0;
  16. }
  17.  
  18. int Full()
  19. {
  20. if(top == Nmax - 1) return 1;
  21. return 0;
  22. }
  23.  
  24. void Push(int x)
  25. {
  26. if(Full()) cout << "Stack Overflow!";
  27. else st[++top] = x;
  28. }
  29.  
  30. void Pop()
  31. {
  32. if(!Empty()) top--;
  33. }
  34.  
  35. int Top()
  36. {
  37. return st[top];
  38. }
  39. int Size()
  40. {
  41. return top+1;
  42. }
  43. };
  44.  
  45. //--------------Coada--------------
  46. struct Coada
  47. {
  48. int q[Nmax];
  49. int pr, ul;
  50.  
  51. void Init()
  52. {
  53. pr = 0; ul = - 1;
  54. }
  55.  
  56. int Empty()
  57. {
  58. if(pr <= ul) return 0;
  59. return 1;
  60. }
  61.  
  62. void Push(int x)
  63. {
  64. q[++ul] = x;
  65. }
  66.  
  67. void Pop()
  68. {
  69. if(!Empty()) pr++;
  70. }
  71.  
  72. int Front()
  73. {
  74. return q[pr];
  75. }
  76.  
  77. int Size()
  78. {
  79. return ul - pr + 1;
  80. }
  81. };
  82.  
  83. //--------------Lista--------------
  84. struct Lista
  85. {
  86. int t[Nmax];
  87. int pr, ul;
  88.  
  89. void Init()
  90. {
  91. pr = 0; ul = -1;
  92. }
  93.  
  94. int Empty()
  95. {
  96. return pr > ul;
  97. }
  98.  
  99. void InsertEnd(int x)
  100. {
  101. t[++ul] = x;
  102. }
  103.  
  104. void Insert(int poz, int x)
  105. {
  106. for(int i = ul; i >= poz + pr - 1; i--)
  107. t[i+1] = t[i];
  108. t[poz + pr - 1] = x;
  109. ul++;
  110. }
  111.  
  112. void Parcurgere()
  113. {
  114. if(Empty()) cout << "Lista este vida!";
  115. else
  116. {
  117. for(int i = pr; i <= ul; i++)
  118. cout << t[i] << " ";
  119. cout << "\n";
  120. }
  121. }
  122.  
  123. void DeleteEnd()
  124. {
  125. if(!Empty()) ul--;
  126. }
  127.  
  128. void DeleteBegin()
  129. {
  130. if(!Empty()) pr++;
  131. }
  132.  
  133. void Delete(int poz)
  134. {
  135. if(Empty()) return;
  136. for(int i = poz + pr - 1; i < ul; i++)
  137. t[i] = t[i+1];
  138. ul--;
  139. }
  140.  
  141. int GetElement(int k)
  142. {
  143. return t[pr + k - 1];
  144. }
  145.  
  146. int Search(int x)
  147. {
  148. int i;
  149. for(i = pr; i <= ul; i++)
  150. if(t[i] == x) return i - pr + 1;
  151. return -1;
  152. }
  153.  
  154. int Size()
  155. {
  156. return ul - pr + 1;
  157. }
  158.  
  159. };
  160.  
  161. //--------------Lista simplu inlantuita--------------
  162. struct Nod
  163. {
  164. int info;
  165. Nod *leg;
  166. };
  167.  
  168. struct Lista /// Simplu inlantuita
  169. {
  170. Nod *L;
  171. void Inserare1(int x) /// Inserare la inceput
  172. {
  173. Nod *p;
  174. p = new Nod;
  175. p -> info = x;
  176. p -> leg = L;
  177. L = p;
  178. }
  179.  
  180. void Inserare2(Nod *p, int x) /// Inserare dupa nodul de adresa memorata in p
  181. {
  182. Nod *q;
  183. q = new Nod;
  184. q -> info = x;
  185. q -> leg = p -> leg;
  186. p -> leg = q;
  187. }
  188.  
  189. /// Insereaza dupa al k-lea nod
  190. /// un nou nod cu informatia x;
  191. /// Daca lista are mai putin de
  192. /// k noduri, inserarea se face
  193. /// la sfarsitul listei.
  194. void Inserare3(int k, int x)
  195. {
  196. Nod *p, *q;
  197. for(p = L; (k > 1) && (p != NULL); p = p -> leg)
  198. {
  199. k--;
  200. q = p;
  201. }
  202. if(p == NULL) /// Inserez dupa q
  203. Inserare2(q, x);
  204. else /// Inserez dupa p
  205. Inserare2(p, x);
  206. }
  207.  
  208. void Parcurgere() /// Parcurgerea listei
  209. {
  210. for(Nod *p = L; p != NULL; p = p -> leg)
  211. cout << p -> info << " ";
  212. cout << "\n";
  213. }
  214.  
  215. void Init() /// Initializarea listei vide
  216. {
  217. L = NULL;
  218. }
  219.  
  220. void Sterge1() /// Stergerea primului nod
  221. {
  222. if(L = NULL) return;
  223. Nod *p;
  224. p = L;
  225. L = p -> leg;
  226. delete p;
  227. }
  228.  
  229. void Sterge2(Nod *p) /// Stergerea nodului dupa nodul de adresa memorata p
  230. {
  231. Nod *q;
  232. q = p -> leg;
  233. p -> leg = q -> leg;
  234. delete q;
  235. }
  236.  
  237. Nod *Cautare(int x) /// Cautarea valorii x in lista si returnarea pointer-ului
  238. {
  239. Nod *p;
  240. for(p = L; p != NULL; p = p -> leg)
  241. if(p -> info == x)
  242. return p;
  243. return NULL;
  244. }
  245.  
  246. void ParcDS(Nod *p) /// Parcurgerea de la dreapta la stanga a nodurilor listei
  247. {
  248. if(p != NULL)
  249. {
  250. ParcDS(p -> leg);
  251. cout << p -> info << " ";
  252. }
  253. }
  254. };
  255.  
  256. //--------------Lista Dublu Inlantuita--------------
  257. struct Nod
  258. {
  259. int info;
  260. Nod *stg, *drp;
  261. };
  262.  
  263. Nod *head, *tail;
  264.  
  265. void Init() /// Initializare
  266. {
  267. head = tail = NULL;
  268. }
  269.  
  270. void Creare(int x) /// Creare l.d.i cu un nod
  271. {
  272. head = new Nod;
  273. head -> info = x;
  274. head -> drp = NULL;
  275. head -> stg = NULL;
  276. tail = head;
  277. }
  278.  
  279. void AdInceput(int x) /// Adaugarea la inceput
  280. {
  281. Nod *p;
  282. p = new Nod;
  283. p -> info = x;
  284. p -> drp = head;
  285. p -> stg = NULL;
  286. head -> stg = p;
  287. head = p;
  288. }
  289.  
  290. void AdSfarsit(int x) /// Adaugarea la sfarsit
  291. {
  292. Nod *p;
  293. p = new Nod;
  294. p -> info = x;
  295. p -> stg = tail;
  296. p -> drp = NULL;
  297. tail -> drp = p;
  298. tail = p;
  299. }
  300.  
  301. void AdDupaNod(Nod *p, int x) /// Adaugarea dupa un nod
  302. {
  303. Nod *q;
  304. q = new Nod;
  305. q -> info = x;
  306. q -> stg = p;
  307. q -> drp = p -> drp;
  308. p -> drp = q;
  309. p -> drp -> stg = q;
  310. }
  311.  
  312. void ParcSD() /// Parcurgerea St->Dr
  313. {
  314. for(Nod *p = head; p != NULL; p = p -> drp)
  315. cout << p -> info << " ";
  316. cout << "\n";
  317. }
  318.  
  319. void ParcDS() /// Parcurgerea Dr->St
  320. {
  321. for(Nod *p = tail; p != NULL; p = p -> stg)
  322. cout << p -> info << " ";
  323. cout << "\n";
  324. }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement