Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.77 KB | None | 0 0
  1. include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class lln
  6. {
  7. public:
  8. int d;
  9. lln *ad;
  10. }*r;
  11.  
  12. class ll
  13. {
  14. public:
  15. ll()
  16. {
  17. r = NULL;
  18. }
  19.  
  20. void addbeg(int v);
  21. void addmiddle(int x, int v);
  22. void addend(int v);
  23. void deletebeg();
  24. int deletev(int x);
  25. void deleteend();
  26. int countlist();
  27. void showlist();
  28. };
  29.  
  30. void ll::addbeg(int v)
  31. {
  32. lln *t1 = r;
  33. lln *t2 = new lln();
  34. t2->d = v;
  35. t2->ad = NULL;
  36.  
  37. if (r == NULL)
  38. {
  39. t1 = r = t2;
  40. }
  41. else
  42. {
  43. t2->ad = t1;
  44.  
  45. r = t2;
  46. }
  47.  
  48. cout<<"\nThe Value "<<v<<" Added."<<endl;
  49. }
  50.  
  51. void ll::addmiddle(int x, int v)
  52. {
  53. lln *t1 = r;
  54. lln *t2 = new lln();
  55. t2->d = v;
  56. t2->ad = NULL;
  57.  
  58. while (t1->d != x)
  59. {
  60. t1 = t1->ad;
  61. }
  62.  
  63. if (t1->ad == NULL)
  64. {
  65. t1->ad = t2;
  66. }
  67. else
  68. {
  69. lln *t3 = t1->ad;
  70.  
  71. t1->ad = t2;
  72. t2->ad = t3;
  73. }
  74.  
  75. cout<<"\nThe Value "<<v<<" Added."<<endl;
  76. }
  77.  
  78. void ll::addend(int v)
  79. {
  80. lln *t1 = r;
  81. lln *t2 = new lln();
  82. t2->d = v;
  83. t2->ad = NULL;
  84.  
  85. if (r == NULL)
  86. {
  87. t1 = r = t2;
  88. }
  89. else
  90. {
  91. while (t1->ad != NULL)
  92. {
  93. t1 = t1->ad;
  94. }
  95.  
  96. t1->ad = t2;
  97. }
  98.  
  99. cout<<"\nThe Value "<<v<<" Added."<<endl;
  100. }
  101.  
  102. void ll::deletebeg()
  103. {
  104. lln *t = r;
  105.  
  106. r = r->ad;
  107.  
  108. cout<<"\nThe Value "<<t->d<<" Deleted."<<endl;
  109.  
  110. delete t;
  111. }
  112.  
  113. int ll::deletev(int x)
  114. {
  115. int dcu = 0;
  116.  
  117. lln *t1 = NULL;
  118. lln *t2 = r;
  119.  
  120. while (t2->ad != NULL)
  121. {
  122. if (t2->d != x)
  123. {
  124. t1 = t2;
  125. t2 = t2->ad;
  126. }
  127. else if ( (t2 == r) && (t2->d == x) )
  128. {
  129. lln *t = r;
  130.  
  131. t1 = t2 = r = r->ad;
  132.  
  133. delete t;
  134.  
  135. dcu++;
  136. }
  137. else
  138. {
  139. lln *t = t2;
  140.  
  141. t2 = t2->ad;
  142. t1->ad = t2;
  143.  
  144. delete t;
  145.  
  146. dcu++;
  147. }
  148. } if ( (t2->ad == NULL) && (t2->d == x) )
  149. {
  150. lln *t = t2;
  151.  
  152. t1->ad = NULL;
  153.  
  154. t2 = t1;
  155.  
  156. delete t;
  157.  
  158. dcu++;
  159. }
  160.  
  161. return dcu;
  162. }
  163.  
  164. void ll::deleteend()
  165. {
  166. lln *t = NULL;
  167. lln *t1 = r;
  168.  
  169. do
  170. {
  171. if (t1->ad != NULL)
  172. {
  173. t = t1;
  174. t1 = t1->ad;
  175. }
  176. }
  177. while (t1->ad != NULL);
  178.  
  179. lln *t2 = t1;
  180. t->ad = NULL;
  181. t1 = t;
  182.  
  183. cout<<"\nThe Value "<<t2->d<<" Deleted."<<endl;
  184.  
  185. delete t2;
  186.  
  187.  
  188. }
  189.  
  190. int ll::countlist()
  191. {
  192. int cun = 0;
  193. if (r == NULL)
  194. {
  195. return 0;
  196. }
  197. else
  198. {
  199. lln *t = r;
  200.  
  201. while (t->ad != NULL)
  202. {
  203. cun++;
  204. t = t->ad;
  205. }
  206.  
  207. cun++;
  208.  
  209. return cun;
  210. }void ll::showlist()
  211. {
  212. if (r == NULL)
  213. {
  214. cout<<"\nList is Empty"<<endl;
  215. }
  216. else
  217. {
  218. cout<<"\nList Values:-"<<endl;
  219.  
  220. lln *t = r;
  221.  
  222. while (t->ad != NULL)
  223. {
  224. cout<<t->d<<endl;
  225. t = t->ad;
  226. }
  227.  
  228. cout<<t->d<<endl;
  229. }
  230. }
  231.  
  232. int main()
  233. {
  234. ll *ru = new ll();
  235.  
  236. int ch, x, v;
  237.  
  238. do
  239. {
  240. cout<<"\n1. Add Value at Beginning."<<endl;
  241. cout<<"2. Add Value at a Position."<<endl;
  242. cout<<"3. Add Value at End."<<endl;
  243. cout<<"4. Delete First Value."<<endl;
  244. cout<<"5. Delete Particular Value."<<endl;
  245. cout<<"6. Delete Last Value."<<endl;
  246. cout<<"7. Count Number of Values."<<endl;
  247. cout<<"8. Show All Values."<<endl;
  248. cout<<"9. Exit..."<<endl;
  249. cout<<"Enter Operation No.: ";
  250. cin>>ch;
  251.  
  252. switch(ch)
  253. {
  254. case 1:
  255. {
  256. cout<<"\nEnter a Value: ";
  257. cin>>v;
  258. ru->addbeg(v);
  259. break;
  260. }
  261.  
  262. case 2:
  263. {
  264. cout<<"\nEnter a Value: ";
  265. cin>>v;
  266.  
  267. int cu = ru->countlist();
  268.  
  269. if (cu == 0)
  270. {
  271. ru->addbeg(v);
  272. }
  273. else
  274. {
  275. ru->showlist();
  276. cout<<"Enter The Value to Add After it: ";
  277. cin>>x;
  278. ru->addmiddle(x, v);
  279. }
  280. break;
  281. }
  282.  
  283. case 3:
  284. {
  285. cout<<"\nEnter a Value: ";
  286. cin>>v;
  287. ru->addend(v);
  288. break;
  289. }
  290.  
  291. case 4:
  292. {
  293. int cu = ru->countlist();
  294.  
  295. if (cu == 0)
  296. {
  297. cout<<"\nThe List is Empty"<<endl;
  298. }
  299. else
  300. {
  301. ru->deletebeg();
  302. }
  303. break;
  304. }
  305.  
  306. case 5:
  307. {
  308. int cu = ru->countlist();
  309.  
  310. if (cu == 0)
  311. {
  312. cout<<"\nThe List is Empty"<<endl;
  313. }
  314. else
  315. {
  316. ru->showlist();
  317. cout<<"Enter Value to Delete: ";
  318. cin>>x;
  319. int dcu = ru->deletev(x);
  320.  
  321. if (dcu == 0)
  322. {
  323. cout<<"\nThe Value "<<x<<" is NOT on The List."<<endl;
  324. }
  325. else
  326. {
  327. cout<<"\nThe Values "<<x<<" Deleted."<<endl;
  328. }
  329. }
  330. break;
  331. }
  332.  
  333. case 6:
  334. {
  335. int cu = ru->countlist();
  336.  
  337. if (cu == 0)
  338. {
  339. cout<<"\nThe List is Empty"<<endl;
  340. }
  341. else
  342. {
  343. ru->deleteend();
  344. }
  345. break;
  346. }
  347.  
  348. case 7:
  349. {
  350. int cu = ru->countlist();
  351.  
  352. if (cu==0)
  353. {
  354. cout<<"\nThe List is Empty"<<endl;
  355. }
  356. else
  357. {
  358. cout<<"\nThe List has "<<cu<<" Value."<<endl;
  359. }
  360. break;
  361. }
  362.  
  363. case 8:
  364. {
  365. ru->showlist();
  366. break;
  367. }
  368.  
  369. case 9:
  370. {
  371. cout<<"\n..."<<endl;
  372. break;
  373. }
  374.  
  375. default:
  376. {
  377. cout<<"\nInvalid Operation No"<<endl;
  378. break;
  379. }
  380. }
  381. }
  382. while(ch != 9);
  383.  
  384. return 0;
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement