Advertisement
Guest User

nothing

a guest
Jul 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.57 KB | None | 0 0
  1. /*Skip to content
  2.  
  3. Search or jump to…
  4.  
  5. Pull requests
  6. Issues
  7. Marketplace
  8. Explore
  9.  
  10. @nihalbaig0
  11. 1
  12. 0 0 cse-138-2018-a/assignment-1-singly-linked-list-nihalbaig0 Private
  13. Code Issues 0 Pull requests 0 Projects 0 Wiki Security Insights
  14. assignment-1-singly-linked-list-nihalbaig0/20190715_singly_linked_list.cpp
  15. @enamcse enamcse Initial commit
  16. f34e018 34 seconds ago
  17. 225 lines (217 sloc) 6.83 KB*/
  18.  
  19. #include <iostream>
  20. using namespace std;
  21.  
  22. struct node{
  23. int val;
  24. node *next;
  25. };
  26.  
  27. struct SinglyLinkedList{
  28. node *head,*tail;
  29. SinglyLinkedList();
  30. ~SinglyLinkedList();
  31. /**
  32. * This function takes an integer parameter
  33. * and add it after the last element of the
  34. * singly linked list.
  35. */
  36. void enqueue(int x);
  37. /**
  38. * This function removes the first element from
  39. * the singly linked list.
  40. * @return integer - the removed integer.
  41. */
  42. int dequeue();
  43. /**
  44. * This function prints the whole singly linked
  45. * list starting from the header node.
  46. */
  47. void printList();
  48. /**
  49. * This function insert the provided integer just
  50. * after the header node. If there is no node
  51. * exists in the list, it inserts the integer as
  52. * first element.
  53. * @param x - integer - to add in the list.
  54. */
  55. void insertAfterHead(int x);
  56. /**
  57. * This function inserts the provided integer just
  58. * before the trailer node. If there is no node or
  59. * one node exists in the list, it inserts the
  60. * integer as first element.
  61. * @param x - integer - to add in the list.
  62. */
  63. void insertBeforeTail(int x);
  64. /**
  65. * This function inserts the provided integer toAdd
  66. * just before the first occurrence of the value
  67. * toFind. If the value does not exist, it adds
  68. * the integer at the end of the list.
  69. * @param toFind - integer - the value to find.
  70. * @param toAdd - integer - to add in the list.
  71. */
  72. void insertBeforeVal(int toFind, int toAdd);
  73. /**
  74. * This function inserts the provided integer val
  75. * just before the position pos.
  76. * Example: if the provided position is 4, then
  77. * the inserting integer should be added in a
  78. * manner so that the inserted value remains at the
  79. * position 2 after performing this addition. If the
  80. * position overflows, then the value should be
  81. * added at the end of the list. If the position
  82. * underflows, then the value should be added at
  83. * the beginning of the list.
  84. * @param pos - integer - the desired position.
  85. * @param val - integer - to add in the list.
  86. */
  87. void insertBeforePos(int pos, int val);
  88. /**
  89. * This function inserts the provided integer toAdd
  90. * just after the first occurrence of the value
  91. * toFind. If the value does not exist, it adds
  92. * the integer at the end of the list.
  93. * @param toFind - integer - the value to find.
  94. * @param toAdd - integer - to add in the list.
  95. */
  96. void insertAfterVal(int toFind, int toAdd);
  97. /**
  98. * This function inserts the provided integer val
  99. * just after the position pos.
  100. * Example: if the provided position is 4, then
  101. * the inserting integer should be added in a
  102. * manner so that the inserted value remains at the
  103. * position 5 after performing this addition. If the
  104. * position overflows, then the value should be
  105. * added at the end of the list. If the position
  106. * underflows, then the value should be added at
  107. * the beginning of the list.
  108. * @param pos - integer - the desired position.
  109. * @param val - integer - to add in the list.
  110. */
  111. void insertAfterPos(int pos, int val);
  112. /**
  113. * This function inserts the provided integer val
  114. * just at the position pos.
  115. * Example: if the provided position is 4, then
  116. * the inserting integer should be added in a
  117. * manner so that the inserted value remains at the
  118. * position 4 after performing this addition. If the
  119. * position overflows, then the value should be
  120. * added at the end of the list. If the position
  121. * underflows, then the value should be added at
  122. * the beginning of the list.
  123. * @param pos - integer - the desired position.
  124. * @param val - integer - to add in the list.
  125. */
  126. void insertAtPos(int pos, int val);
  127. /**
  128. * This function deletes the integer at position
  129. * pos. If the position does not exist, it prints
  130. * underflow.
  131. * @param pos - integer - the desired position.
  132. */
  133. int deleteFromPos(int x);
  134. /**
  135. * This function deletes the first occurrence of the
  136. * provided integer x. If the position does not
  137. * exist, it prints underflow.
  138. * @param x - integer - the value to be deleted.
  139. */
  140. int deleteVal(int x);
  141. /**
  142. * This function deletes the header node of the
  143. * Singly Linked List. If the list is empty, then
  144. * it prints underflow.
  145. */
  146. int deleteHead();
  147. /**
  148. * This function deletes the trailer node of the
  149. * Singly Linked List. If the list is empty, then
  150. * it prints underflow.
  151. */
  152. int deleteTail();
  153. /**
  154. * This function deletes the node after the
  155. * header node of the Singly Linked List. If the
  156. * list has one element, then it deletes header
  157. * node. If the list is empty, then it prints
  158. * underflow.
  159. */
  160. int deleteValAfterHead();
  161. /**
  162. * This function deletes the node before the
  163. * trailer node of the Singly Linked List. If the
  164. * list has one element, then it deletes trailer
  165. * node. If the list is empty, then it prints
  166. * underflow.
  167. */
  168. int deleteValBeforeTail();
  169. };
  170.  
  171. int SinglyLinkedList::deleteHead(){
  172.  
  173. }
  174.  
  175. SinglyLinkedList::SinglyLinkedList(){
  176. head = tail = NULL;
  177. cout << "Singly Linked List initiated\n";
  178. }
  179.  
  180. SinglyLinkedList::~SinglyLinkedList(){
  181. cout << "Oh! You killed me!\n";
  182. }
  183.  
  184. void SinglyLinkedList::enqueue(int x){
  185. node *cur = new node;
  186. cur->val = x;
  187. cur->next = NULL;
  188. cout << x <<" is enqueued!\n";
  189. if(head==NULL && tail==NULL){
  190. head = tail = cur;
  191. return;
  192. }
  193. tail->next = cur;
  194. tail = tail->next; /// tail = cur;
  195. }
  196. int SinglyLinkedList::dequeue(){
  197. if(head == NULL && tail == NULL){
  198. cout << "Underflow!\n";
  199. return -1;
  200. }
  201. node *cur = head;
  202. int x = cur->val;
  203. cout << x <<" is dequeued!\n";
  204. if(head == tail){
  205. head = tail = NULL;
  206. }
  207. else head = head->next;
  208. delete cur;
  209. return x;
  210. }
  211. void SinglyLinkedList::printList(){
  212. cout << "SinglyLinkedList - Start\n";
  213. node *cur = head;
  214. while(cur!=NULL)
  215. {
  216. cout << cur->val << endl;
  217. cur = cur->next;
  218. }
  219. cout << "SinglyLinkedList - End\n";
  220. }
  221. void SinglyLinkedList::insertAfterHead(int x)
  222. {
  223. node *cur = new node;
  224. cur->val = x;
  225. cur->next = NULL;
  226. if(head == tail)
  227. {
  228. head->next = cur;
  229. tail = cur;
  230. }
  231. else
  232. {
  233. cur->next = head->next;
  234. head->next = cur;
  235. }
  236. cout <<cur->val << " inserted after head\n";
  237.  
  238. }
  239. void SinglyLinkedList::insertBeforeTail(int x)
  240. {
  241. node *cur = new node,*temp;
  242. cur->val = x;
  243. cur->next = NULL;
  244. temp = head;
  245. if(head==tail)
  246. {
  247. cur->next = head;
  248. head = cur;
  249. }
  250. else
  251. {
  252. while(temp->next!=tail)
  253. {
  254. temp = temp->next;
  255. }
  256. temp->next = cur;
  257. cur->next = tail;
  258.  
  259. }
  260. cout <<cur->val << " inserted before tail\n";
  261.  
  262. }
  263. void SinglyLinkedList::insertBeforeVal(int toFind, int toAdd)
  264. {
  265. node *cur = new node,*temp;
  266. cur->val = toAdd;
  267. cur->next = NULL;
  268. temp = head;
  269. if(toFind== head->val)
  270. {
  271. cur->next = head;
  272. head = cur;
  273. }
  274. else{
  275. while(temp->val != toFind)
  276. temp = temp->next;
  277. cur->next = temp;
  278. temp = head;
  279. while(temp->next != cur->next)
  280. temp = temp->next;
  281. temp->next = cur;
  282.  
  283. }
  284. cout << toAdd<< " added before " << toFind <<endl;
  285.  
  286. }
  287. void SinglyLinkedList::insertAfterVal(int toFind, int toAdd)
  288. {
  289. node *cur = new node,*temp;
  290. cur->val = toAdd;
  291. cur->next = NULL;
  292. temp = head;
  293. if(toFind== head->val)
  294. {
  295. cur->next = head->next;
  296. head->next = cur;
  297. }
  298. else
  299. {
  300. while(temp->val != toFind)
  301. temp = temp->next;
  302. cur->next = temp->next;
  303. temp->next = cur;
  304. }
  305. cout << toAdd<< " added after " << toFind <<endl;
  306.  
  307. }
  308.  
  309. void SinglyLinkedList::insertBeforePos(int pos, int val)
  310. {
  311. int i;
  312. node *cur = new node,*temp;
  313. cur->val = val;
  314. cur->next = NULL;
  315. temp = head;
  316. if(pos<=0)
  317. {
  318. cur->next = head;
  319. head = cur;
  320. }
  321. else
  322. {
  323. for(i=0;i <pos-2;i++)
  324. {
  325. temp=temp->next;
  326. if(temp->next=tail)
  327. {
  328. cur->next = tail;
  329. temp->next = cur;
  330. cout << val << " added before position " << pos <<endl;
  331. return;
  332. }
  333. }
  334.  
  335. cur->next = temp->next;
  336. temp->next = cur;
  337. }
  338. cout << val << " added before position " << pos <<endl;
  339. }
  340. void SinglyLinkedList::insertAfterPos(int pos, int val)
  341. {
  342. int i;
  343. node *cur = new node,*temp;
  344. cur->val = val;
  345. cur->next = NULL;
  346. temp = head;
  347. if(pos<=0)
  348. {
  349. cur->next = head;
  350. head = cur;
  351. }
  352. else
  353. {
  354. for(i=0;i <pos-1;i++)
  355. {
  356. temp=temp->next;
  357. if(temp->next==tail)
  358. {
  359. temp->next = cur ;
  360. cur->next = tail;
  361. cout << val << " added after position " << pos <<endl;
  362. return;
  363. }
  364. }
  365. cur->next = temp->next;
  366. temp->next =cur;
  367. cout << val << " added after position " << pos <<endl;
  368. }
  369. }
  370. void SinglyLinkedList::deleteFromPos(int x)
  371. {
  372. int i;
  373. node *cur = new node,*temp;
  374. cur->val = val;
  375. cur->next = NULL;
  376. temp = head;
  377. if(pos<=0)
  378. {
  379. cout << "Underflow\n";
  380. }
  381. else
  382. {
  383. for(i=0;i <pos-1;i++)
  384. {
  385. temp=temp->next;
  386. if(temp==NULL)
  387. {
  388. cout << "Underflow\n";
  389. return;
  390. }
  391. }
  392. for(i=0;i < pos-1;i++)
  393. {
  394. temp = temp->next;
  395. }
  396.  
  397.  
  398. }
  399.  
  400.  
  401. int main()
  402. {
  403. SinglyLinkedList sl;sl.printList();
  404. sl.enqueue(35);sl.printList();
  405. sl.enqueue(46);sl.printList();
  406. sl.insertAfterHead(100);sl.printList();
  407.  
  408. sl.enqueue(98);sl.printList();
  409. sl.insertBeforeVal(46,600);sl.printList();
  410. sl.insertAfterVal(46,700);sl.printList();
  411. sl.insertBeforeTail(199);sl.printList();
  412. sl.dequeue();sl.printList();
  413. sl.dequeue();sl.printList();
  414. sl.dequeue();sl.printList();
  415. sl.dequeue();sl.printList();
  416. sl.enqueue(445);sl.printList();
  417. sl.insertBeforePos(3,225);sl.printList();
  418. sl.insertAfterPos(2,775);sl.printList();
  419. sl.dequeue();sl.printList();
  420. sl.enqueue(7);sl.printList();
  421. sl.enqueue(75);sl.printList();
  422. sl.dequeue();sl.printList();
  423. sl.dequeue();sl.printList();
  424. sl.dequeue();sl.printList();
  425. sl.dequeue();sl.printList();
  426. return 0;
  427. }
  428. /*© 2019 GitHub, Inc.
  429. Terms
  430. Privacy
  431. Security
  432. Status
  433. Help
  434. Contact GitHub
  435. Pricing
  436. API
  437. Training
  438. Blog
  439. About
  440. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement