Haval1

complete linked list

Apr 1st, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 36.96 KB | None | 0 0
  1. // completeLinkedList.cpp : Defines the entry point for the console application.
  2. /*
  3.     Copyright 2019,
  4.     Auther : Omar M. Omar,
  5.     All rights reserved.
  6.     Start it in 2019-3-27  12:11AM
  7.     End in 2019-3-29  7:41PM
  8.     Last Edition in 2019-4-5    11:14AM
  9. */
  10.  
  11. #include "stdafx.h"
  12. #include "iostream"
  13. #include "string"
  14. using namespace std;
  15. //All variable
  16. int choice, i, item, pos, lsize, a[10];
  17. /*=====================
  18. SINGLE LINK LIST    ===
  19. =======================
  20. */
  21. //Default Struct (Single Link List)
  22. struct node
  23. {
  24.     int data;
  25.     node *next;
  26. };
  27.  
  28. //structure for insert char (s_homeWork 5)
  29. struct nodechar
  30. {
  31.     char data;
  32.     nodechar *next;
  33. };
  34.  
  35. //structure for insert emp data (s_homeWork 4)
  36. struct nodeEmp
  37. {
  38.     string name;
  39.     int age;
  40.     nodeEmp *next;
  41. };
  42.  
  43. //SINGLE LIST CLASS have all labs functions
  44. class singleList
  45. {
  46. public:
  47.     void addEnd(int, node *&, node *&);
  48.     void addBegin(int, node *&, node *&);
  49.     void addByPosition(int, node *&, node *&, int);
  50.     void addByData(int, node *&, node *&, int);
  51.     void Display(node *&);
  52.     void deleteEnd(node *&, node *&);
  53.     void deleteBegin(node *&);
  54.     void deleteByPosition(int, node *&, node *&);
  55.     void deleteByData(int, node *&, node *&);
  56.  
  57. }single;
  58.  
  59. void labs();
  60. void singleHomeWork();
  61.  
  62. //SINGLE LIST (first home work functions)
  63. void s_homeWork1();
  64. void s_increment(node *&);
  65. void delete_odd(node *&);
  66.  
  67. //SINGLE LIST (second home work functions)
  68. void s_homeWork2();
  69. void addEndchar(char, nodechar *&, nodechar*&);
  70. void delete4th(nodechar *&, nodechar *&);
  71. void split(nodechar *&, nodechar *&);
  72. void displaychar(nodechar *&head);
  73.  
  74. //SINGLE LIST (third home work functions)
  75. void s_homeWork3();
  76. void mergelist(node*&,node*&, node*&, node*&, node*&);
  77.  
  78. //SINGLE LIST (forth(4th) home work functions)
  79. void s_homeWork4();
  80. void displayEmp(nodeEmp *&);
  81. void createListEmp(nodeEmp*&, nodeEmp*&, int, string);
  82.  
  83. //SINGLE LIST (fifth home work functions)
  84. void s_homeWork5();
  85. int even_number(node *&);
  86.  
  87. /*=========================
  88.     DOUBLE LINK LIST    ===
  89.     =======================
  90. */
  91.  
  92. //Default Struct (Double Link List)
  93. struct doubleNode
  94. {
  95.     int data;
  96.     doubleNode *next;
  97.     doubleNode *perv;
  98. };
  99.  
  100. //DOUBLE LIST CLASS have all labs functions
  101. class doubleList
  102. {
  103. public:
  104.     void doubleAddEnd(int, doubleNode *&, doubleNode *&);
  105.     void doubleAddBegin(int, doubleNode *&, doubleNode *&);
  106.     void doubleAddByPosition(int, doubleNode *&, doubleNode *&, int);
  107.     void doubleAddByData(int, doubleNode *&, doubleNode *&, int);
  108.     void doubleDisplay(doubleNode *&);
  109.     void doubleDeleteEnd(doubleNode *&, doubleNode *&);
  110.     void doubleDeleteBegin(doubleNode *&);
  111.     void doubleDeleteByPosition(int, doubleNode *&, doubleNode *&);
  112.     void doubleDeleteByData(int, doubleNode *&, doubleNode *&);
  113.  
  114. }doubleL;
  115.  
  116. void doubleLab();
  117. void doubleHomeWork();
  118.  
  119. //DOUBLE LINKED LIST (first HW)
  120. void doubleHW1();
  121.  
  122. //DOUBLE LINKED LIST (second HW)
  123. void doubleHW2();
  124. void swapList(doubleNode *&, doubleNode *&);
  125.  
  126. //DOUBLE LINKED LIST (third HW)
  127. void doubleHW3();
  128. void duplicate(doubleNode *&);
  129.  
  130. //DOUBLE LINKED LIST (forth HW)
  131. void doubleHW4();
  132. void deleteAllByKey(doubleNode *&,doubleNode *&,int);
  133. //main function
  134. void main()
  135. {
  136.     node *head, *tail;
  137.     tail = head = NULL;
  138.    
  139.     do
  140.     {
  141.         cout << "\t\t\t\t->->-> Complete Linked List Program <-<-<-\n\n";
  142.         cout << "\t\t\t\t1 ) Single Linked List\n";
  143.         cout << "\t\t\t\t2 ) Double Linked List\n";
  144.         cout << "\t\t\t\t3 ) Circular Linked List\n";
  145.         cout << "\t\t\t\t4 ) Exit System\n\n";
  146.         cout << "\t\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
  147.         cout << "\t\t\t\t\tEnter your Choice ->->-> : ";
  148.         cin >> choice;
  149.         cout << "\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n";
  150.         switch (choice)
  151.         {
  152.         case 1:
  153.             cout << "\n\t\t\t\t->->-> Welcome to SINGLE linked list <-<-<-\n\n";
  154.             cout << "\t\t\t\t1 ) To see All labs Example\n";
  155.             cout << "\t\t\t\t2 ) To see All home work\n";
  156.             cout << "\t\t\t\t3 ) Back to main menu\n\n";
  157.             cout << "\t\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
  158.             cout << "\t\t\t\t\tEnter your Choice ->->-> : ";
  159.             cin >> choice;
  160.             cout << "\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n";
  161.             switch (choice)
  162.             {
  163.             case 1:
  164.                 labs();
  165.                 break;
  166.             case 2:
  167.                 singleHomeWork();
  168.                 break;
  169.             case 3:
  170.                 break;
  171.             default:
  172.                 break;
  173.             }//End of SINGLE LIST SWITCH
  174.             break;
  175.         case 2:
  176.             cout << "\n\t\t\t\t->->-> Welcome to DOUBLE linked list <-<-<-\n\n";
  177.             cout << "\t\t\t\t1 ) To see All labs Example\n";
  178.             cout << "\t\t\t\t2 ) To see All home work \n";
  179.             cout << "\t\t\t\t3 ) Back to main menu\n\n";
  180.             cout << "\t\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  181.             cout << "\t\t\t\t\tEnter your Choice ->->-> : ";
  182.             cin >> choice;
  183.             cout << "\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n";
  184.             switch (choice)
  185.             {
  186.             case 1:
  187.                 doubleLab();
  188.                 break;
  189.             case 2:
  190.                 doubleHomeWork();
  191.                 break;
  192.             case 3:
  193.                 break;
  194.             default:
  195.                 break;
  196.             }//End of DOUBLE LIST SWITCH
  197.             break;
  198.         default:
  199.             for (i = 0;i < 40;i++)
  200.                 cout << "\n";
  201.             cout << "BYE BYE\n";
  202.             break;
  203.         }//main switch
  204.     } while (choice != 4);
  205. }//end of main
  206.  
  207.  //SINGLE LIST (labs function)
  208. //add End function
  209. void singleList::addEnd(int newData, node *&head, node *&tail)
  210. {
  211.     node *newNode = new node;
  212.     newNode->data = newData;
  213.     newNode->next = NULL;
  214.  
  215.     if (head == NULL)
  216.     {
  217.         head = tail = newNode;
  218.     }
  219.     else
  220.     {
  221.         tail->next = newNode;
  222.         tail = newNode;
  223.     }
  224. }
  225. //SINGLE LIST (labs function)
  226. //add Begin function
  227. void singleList::addBegin(int newData, node *&head, node *&tail)
  228. {
  229.     node *newNode = new node;
  230.     newNode->data = newData;
  231.  
  232.     if (head == NULL)
  233.     {
  234.         newNode->next = NULL;
  235.         head = tail = newNode;
  236.     }
  237.     else
  238.     {
  239.         newNode->next = head;
  240.         head = newNode;
  241.     }
  242. }
  243. //SINGLE LIST (labs function)
  244. //add By position function
  245. void singleList::addByPosition(int newData, node *&head, node *&tail, int position)
  246. {
  247.     int positionCounter = 1;
  248.  
  249.     node *newNode = new node;
  250.     newNode->data = newData;
  251.  
  252.     if (head == NULL)
  253.     {
  254.         newNode->data = NULL;
  255.         head = tail = newNode;
  256.     }
  257.  
  258.     node *tempNode = head;
  259.  
  260.     while (tempNode->next != NULL)
  261.     {
  262.         positionCounter++;
  263.         tempNode = tempNode->next;
  264.  
  265.         if (positionCounter == position)
  266.         {
  267.             newNode->next = tempNode->next;
  268.             tempNode->next = newNode;
  269.             return;
  270.         }
  271.     }
  272.  
  273.     cout << "\nPosition " << position << " doesn\'t exist\n\n";
  274. }
  275. //SINGLE LIST (labs function)
  276. //add by Data function
  277. void singleList::addByData(int newData, node *&head, node *&tail, int searchData)
  278. {
  279.  
  280.     node *newNode = new node;
  281.     newNode->data = newData;
  282.  
  283.     if (head == NULL)
  284.     {
  285.         newNode->data = NULL;
  286.         head = tail = newNode;
  287.     }
  288.  
  289.     node *tempNode = head;
  290.  
  291.     while (tempNode->data != searchData)
  292.     {
  293.         if (tempNode->next == NULL)
  294.         {
  295.             cout << "Data ( " << searchData << " ) is not exsist..\n\n";
  296.             return;
  297.         }
  298.         tempNode = tempNode->next;
  299.  
  300.     }
  301.  
  302.     newNode->next = tempNode->next;
  303.     tempNode->next = newNode;
  304. }
  305. //SINGLE LIST (labs function)
  306. //Display Function
  307. void singleList::Display(node *&head)
  308. {
  309.     if (head == NULL)
  310.     {
  311.         cout << "List is Empty!!\n";
  312.         return;
  313.     }
  314.  
  315.     node *tempNode;
  316.     tempNode = head;
  317.  
  318.     cout << "\nThe list contains: " << endl;
  319.     while (tempNode != NULL)
  320.     {
  321.         cout << "---| " << tempNode->data << endl;
  322.         tempNode = tempNode->next;
  323.     }
  324.  
  325.     cout << "\n\n";
  326. }
  327. //SINGLE LIST (labs function)
  328. //delete End function
  329. void singleList::deleteEnd(node *&head, node *&tail)
  330. {
  331.     if (head == NULL)
  332.     {
  333.         cout << "List is empty!!!  Deletion is not possible\n";
  334.         return;
  335.     }
  336.  
  337.     node *tempNode;
  338.     tempNode = head;
  339.  
  340.     while (tempNode->next != tail)
  341.     {
  342.         if (tempNode->next == NULL)
  343.         {
  344.             head = NULL;
  345.             delete tempNode;
  346.             return;
  347.         }
  348.  
  349.         tempNode = tempNode->next;
  350.     }
  351.  
  352.     tail = tempNode;
  353.     tempNode = tempNode->next;
  354.     tail->next = NULL;
  355.     delete tempNode;
  356. }
  357. //SINGLE LIST (labs function)
  358. //Delete begin function
  359. void singleList::deleteBegin(node *&head)
  360. {
  361.     if (head == NULL)
  362.     {
  363.         cout << "List is empty!!!  Deletion is not possible\n";
  364.         return;
  365.     }
  366.  
  367.     node *tempNode;
  368.     tempNode = head;
  369.  
  370.     if (tempNode->next == NULL)
  371.     {
  372.         head = NULL;
  373.         delete tempNode;
  374.     }
  375.     else
  376.     {
  377.         head = head->next;
  378.         delete tempNode;
  379.     }
  380. }
  381. //SINGLE LIST (labs function)
  382. //Delete By position function
  383. void singleList::deleteByPosition(int position, node *&head, node *&tail)
  384. {
  385.     int positionCounter = 1;
  386.  
  387.     if (head == NULL)
  388.     {
  389.         cout << "List is empty!!!  Deletion is not possible\n";
  390.         return;
  391.     }
  392.  
  393.     node *tempNode1, *tempNode2 = new node;
  394.     tempNode1 = head;
  395.  
  396.     if (head->next == NULL)
  397.     {
  398.         head = NULL;
  399.         delete tempNode1;
  400.         return;
  401.     }
  402.  
  403.  
  404.     while (tempNode1 != tail)
  405.     {
  406.  
  407.         if (positionCounter == position)
  408.         {
  409.             if (tempNode1 == head)
  410.             {
  411.                 head = head->next;
  412.                 delete tempNode1;
  413.                 return;
  414.             }
  415.             else if (tempNode1 == tail)
  416.             {
  417.                 tempNode2->next = NULL;
  418.                 tail = tempNode2;
  419.                 delete tempNode1;
  420.                 return;
  421.             }
  422.             else
  423.             {
  424.                 tempNode2->next = tempNode1->next;
  425.                 delete tempNode1;
  426.                 return;
  427.             }
  428.         }
  429.  
  430.         tempNode2 = tempNode1;
  431.         tempNode1 = tempNode1->next;
  432.  
  433.         positionCounter++;
  434.  
  435.     }
  436.  
  437.     cout << "Given node not found in the list!  Deletion not possible\n\n";
  438. }
  439. //SINGLE LIST (labs function)
  440. //Delete By data function
  441. void singleList::deleteByData(int searchData, node *&head, node *&tail)
  442. {
  443.     if (head == NULL)
  444.     {
  445.         cout << "List is empty!!!  Deletion is not possible\n";
  446.         return;
  447.     }
  448.  
  449.     node *tempNode1, *tempNode2 = new node;
  450.     tempNode1 = head;
  451.  
  452.     while (tempNode1->data != searchData)
  453.     {
  454.         tempNode2 = tempNode1;
  455.         tempNode1 = tempNode1->next;
  456.  
  457.         if (tempNode1 == NULL)
  458.         {
  459.             cout << "The data is not exsist!  Deletion is not possible!!!\n\n";
  460.             return;
  461.         }
  462.     }
  463.  
  464.     if (head->next == NULL && tempNode1 == head)
  465.     {
  466.         head = NULL;
  467.         delete tempNode1;
  468.         return;
  469.     }
  470.     else if (tempNode1 == head)
  471.     {
  472.         head = head->next;
  473.         delete tempNode1;
  474.         return;
  475.     }
  476.     else if (tempNode1 == tail)
  477.     {
  478.         tempNode2->next = NULL;
  479.         tail = tempNode2;
  480.         delete tempNode1;
  481.         return;
  482.     }
  483.     else
  484.     {
  485.         tempNode2->next = tempNode1->next;
  486.         delete tempNode1;
  487.         return;
  488.     }
  489. }
  490. //SINGLE LIST (home work function)|| HW 1 || increment function
  491. void s_increment(node *&head)
  492. {
  493.     node *temp = head;
  494.     node *prev = new node;
  495.     node *curr = new node;
  496.     curr = head;
  497.     int temppos = 0, pos1 = 3, pos2 = 5;
  498.     if (temp == NULL)
  499.     {
  500.         cout << "Please Enter some data to increment it \n";
  501.     }
  502.     while (curr->next != NULL && temppos != pos1)
  503.     {
  504.         prev = curr;
  505.         curr = curr->next;
  506.         temppos++;
  507.     }
  508.     prev->data += 20;
  509.     cout << "3rd position is increment it : " << prev->data << endl;
  510.     while (curr->next != NULL && temppos != pos2)
  511.     {
  512.         prev = curr;
  513.         curr = curr->next;
  514.         temppos++;
  515.     }
  516.     prev->data += 20;
  517.     cout << "5d position is increment it : " << prev->data << endl;
  518.     while (temp != NULL)
  519.     {
  520.         cout << "data is : " << temp->data << endl;
  521.         temp = temp->next;
  522.     }
  523. }
  524.  
  525. //SINGLE LIST (home work function)|| HW 1 || Delete odd nodes function
  526. void delete_odd(node *&head) {
  527.     if (head == NULL) {
  528.         /*When given linked list are empty.*/
  529.         printf("\n No element in this linked list.");
  530.     }
  531.     else
  532.     {
  533.         node*temp, *helper;
  534.         helper = temp = head;
  535.         /*Find and remove given linked list node*/
  536.         while (temp) {
  537.             /*Odd nodes*/
  538.             if (temp->data % 2 != 0) {
  539.                 /*Remove first node of linked list*/
  540.                 if (temp == head) {
  541.                     helper = temp;
  542.                     /*assign address of next node memory block*/
  543.                     head = head->next;
  544.                     temp = head;
  545.                     /*Before delete node assign that
  546.                     next pointer value is NULL*/
  547.                     helper->next = NULL;
  548.                     /*Free node*/
  549.                     delete helper;
  550.                     helper = NULL;
  551.                 }
  552.                 else {
  553.  
  554.                     helper->next = temp->next;
  555.                     temp->next = NULL;
  556.                     /*Free node*/
  557.                     delete temp;
  558.                     temp = helper->next;
  559.                 }
  560.             }
  561.             else {
  562.                 helper = temp;
  563.                 /*assign address of next node memory block*/
  564.                 temp = temp->next;
  565.             }
  566.  
  567.         }
  568.     }
  569. }
  570. //SINGLE LIST (home work function)|| HW 2 || insert Char in list function
  571. void addEndchar(char newData, nodechar *&head, nodechar *&tail)
  572. {
  573.     nodechar *newNode = new nodechar;
  574.     newNode->data = newData;
  575.     newNode->next = NULL;
  576.  
  577.     if (head == NULL)
  578.     {
  579.         head = tail = newNode;
  580.     }
  581.     else
  582.     {
  583.         tail->next = newNode;
  584.         tail = newNode;
  585.     }
  586. }
  587. //SINGLE LIST (home work function)|| HW 2 || Delete 4th nodes function
  588. void delete4th(nodechar *&head, nodechar *&tail)
  589. {
  590.     nodechar *temp1, *temp2;
  591.     int i = 1;
  592.     if (head == NULL)
  593.     {
  594.         cout << "The required value does not found in this list\n";
  595.         return;
  596.     }
  597.     else
  598.     {
  599.         temp1 = head;
  600.         temp2 = head;
  601.     }
  602.     while (i != 4)
  603.     {
  604.         i++;
  605.         temp1 = temp1->next;
  606.     }
  607.     while (temp2->next != temp1 && temp2 != temp1)
  608.     {
  609.         temp2 = temp2->next;
  610.     }
  611.     if (temp1 == head) // At the begin
  612.     {
  613.         head = head->next;
  614.         cout << "--| " << temp1->data << " was delete it\n";
  615.         delete temp1;
  616.     }
  617.     else if (temp1->next == NULL || temp1 == tail) // at the end
  618.     {
  619.         temp2->next = NULL;
  620.         tail = temp2;
  621.         cout << "--| " << temp1->data << " was delete it\n";
  622.         delete temp1;
  623.     }
  624.     else
  625.     {
  626.         temp2->next = temp1->next;
  627.         cout << "--| " << temp1->data << " was delete it\n";
  628.         delete temp1;
  629.     }
  630. }
  631. //SINGLE LIST (home work function)|| HW 2 || Display char list function
  632. void displaychar(nodechar *&head)
  633. {
  634.     nodechar *temp;
  635.     temp = head;
  636.     while (temp != NULL)
  637.     {
  638.         cout << "--| " << temp->data << endl;
  639.         temp = temp->next;
  640.     }
  641. }
  642. //SINGLE LIST (home work function)|| HW 2 || Split list function
  643. void split(nodechar *&head, nodechar *&tail)
  644. {
  645.     nodechar *temp, *head2, *tail2;
  646.     temp = head;
  647.     if (head == NULL)
  648.         cout << "add some data to split it\n";
  649.     return;
  650.     int count = 1;
  651.     while (count != 5)
  652.     {
  653.         count++;
  654.         temp = temp->next;
  655.     }
  656.     tail = temp;
  657.     head2 = temp->next;
  658.     tail2 = head2;
  659.     while (tail2->next != NULL)
  660.     {
  661.         tail2 = tail2->next;
  662.     }
  663.     displaychar(head);
  664.     cout << endl;
  665.     displaychar(head2);
  666. }
  667.  
  668. //SINGLE LIST (home work function)|| HW 3 || Merge list function
  669. void mergelist(node *& head,node*& tail, node*& head2, node*& tail2, node*& head3)
  670. {
  671.     tail->next = head2;
  672.     head2 = tail;
  673.     tail2->next = head3;
  674.     head3 = tail2;
  675.     single.Display(head);
  676. }
  677.  
  678. //SINGLE LIST (home work function)|| HW 4 || Display Emp list function
  679. void displayEmp(nodeEmp *&head)
  680. {
  681.     nodeEmp *temp;
  682.     temp = head;
  683.     if (head == NULL)
  684.         cout << "list is empty\n";
  685.     else
  686.     {
  687.         while (temp != NULL)
  688.         {
  689.             cout << "Name is : " << temp->name << endl;
  690.             cout << "Age is : " << temp->age << endl;
  691.             temp = temp->next;
  692.         }
  693.     }
  694. }
  695. //SINGLE LIST (home work function)|| HW 4 || Insert Emp list function
  696. void createListEmp(nodeEmp*& head, nodeEmp*& tail, int age, string name)
  697. {
  698.     nodeEmp *newNode = new nodeEmp;
  699.     newNode->next = NULL;
  700.     newNode->age = age;
  701.     newNode->name = name;
  702.     if (head == NULL)
  703.     {
  704.         head = tail = newNode;
  705.     }
  706.     else
  707.     {
  708.         tail->next = newNode;
  709.         tail = newNode;
  710.     }
  711. }
  712. //SINGLE LIST (home work function)|| HW 5 || Find even number in list function
  713. int even_number(node *&head)
  714. {
  715.     int i = 0;
  716.     node *temp;
  717.     temp = head;
  718.     if (head == NULL)
  719.     {
  720.         cout << "List is empty\n";
  721.         return 0;
  722.     }
  723.     else
  724.     {
  725.         while (temp != NULL)
  726.         {
  727.             if (temp->data % 2 == 0)
  728.             {
  729.                 a[i] = temp->data;
  730.                 i++;
  731.             }
  732.             temp = temp->next;
  733.         }
  734.     }
  735.     return i;
  736. }
  737.  
  738. //SINGLE LIST (Labs function)
  739. void labs()
  740. {
  741.     node *head, *tail;
  742.     tail = head = NULL;
  743.     singleList single;
  744.     int choice, item, pos, i, lsize;
  745.     do
  746.     {
  747.         cout << "\n\t\t\t\t->-> All Operation in Single List <-<-\n\n";
  748.         cout << "\t\t\t\t1 - create single link list\n";
  749.         cout << "\t\t\t\t2 - Add item in the End list\n";
  750.         cout << "\t\t\t\t3 - Add item in the Begin list\n";
  751.         cout << "\t\t\t\t4 - Add item By Position in list\n";
  752.         cout << "\t\t\t\t5 - Add item By Data in list \n";
  753.         cout << "\t\t\t\t6 - Display list item\n";
  754.         cout << "\t\t\t\t7- Delete item in the End list\n";
  755.         cout << "\t\t\t\t8 - Delete item in the Begin list\n";
  756.         cout << "\t\t\t\t9 - Delete item By Position list\n";
  757.         cout << "\t\t\t\t10 - Delete item By Data in list\n";
  758.         cout << "\t\t\t\t11 - Exit System\n";
  759.         cout << "\t\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
  760.         cout << "\t\t\t\t\tEnter your Choice ->->-> : ";
  761.         cin >> choice;
  762.         cout << "\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n";
  763.         switch (choice)
  764.         {
  765.         case 1:
  766.             cout << "Enter size of list : ";
  767.             cin >> lsize;
  768.             for (i = 0;i < lsize;i++)
  769.             {
  770.                 cout << "Enter item : ";
  771.                 cin >> item;
  772.                 single.addEnd(item, head, tail);
  773.             }
  774.             break;
  775.         case 2:
  776.             cout << "Enter item : ";
  777.             cin >> item;
  778.             single.addEnd(item, head, tail);
  779.             break;
  780.         case 3:
  781.             cout << "Enter item : ";
  782.             cin >> item;
  783.             single.addBegin(item, head, tail);
  784.             break;
  785.         case 4:
  786.             cout << "Enter the position : ";
  787.             cin >> pos;
  788.             cout << "Enter the item : ";
  789.             cin >> item;
  790.             single.addByPosition(item, head, tail, pos);
  791.             break;
  792.         case 5:
  793.             cout << "Enter the data : ";
  794.             cin >> pos;
  795.             cout << "Enter item : ";
  796.             cin >> item;
  797.             single.addByData(item, head, tail, pos);
  798.             break;
  799.         case 6:
  800.             single.Display(head);
  801.             break;
  802.         case 7:
  803.             single.deleteEnd(head, tail);
  804.             break;
  805.         case 8:
  806.             single.deleteBegin(head);
  807.             break;
  808.         case 9:
  809.             cout << "Enter the position : ";
  810.             cin >> pos;
  811.             single.deleteByPosition(pos, head, tail);
  812.             break;
  813.         case 10:
  814.             cout << "Enter the Data to delete : ";
  815.             cin >> pos;
  816.             single.deleteByData(pos, head, tail);
  817.             break;
  818.         default:
  819.             cout << "\n\n\t\tBye Bye\n\n========================================n\n";
  820.             break;
  821.         }
  822.     } while (choice != 11);
  823. }//end of lab
  824.  
  825.  
  826.  //SINGLE LIST (home work functions)
  827. void singleHomeWork()
  828. {
  829.     node *tail, *head;
  830.     tail = head = NULL;
  831.     do
  832.     {
  833.         cout << "\n\t\t\t\t->-> Home Work to test your self <-<-\n\n";
  834.         cout << "\t\t\t\t1- write a segment of code of C++ progrm (using function) for each of the following\n\t\t\t\ta) create a linked list of seven nodes(int)\n"
  835.             "\t\t\t\tb) Increment the value of the 3th and 5th node by 20\n\t\t\t\tc) Delete all the nodes where its data field is odd in linked list\n";
  836.         cout << "\n\t\t\t\t2 - Let (L) be a link list of size (10) char. write a \n\t\t\t\tprogem that execute the following steps in sequence\n\t\t\t\ta) crate this list\n\t\t\t\tb) insert three nodes in end of list"
  837.             "\n\t\t\t\tc) Delete 4th element \n\t\t\t\td)Split L into equal two lists\n";
  838.         cout << "\n\t\t\t\t3 - Merge any three linked list (h1,h2,h3) ,\n\t\t\t\tThese linked list are same type and different size\n";
  839.         cout << "\n\t\t\t\t4 - write a complete program to contruct single linked list of (8) nodes \n\t\t\t\t(Each node contains three elemnt [name,age and pointer]).\n\t\t\t\tthen write function to print the number of persons that ages larger than 20 year\n";
  840.         cout << "\n\t\t\t\t5 - write complete program to construct single linked list of (10) nodes,\n\t\t\t\tthen write function to copy the even values to array of dimension,\n\t\t\t\tprint the array in main fucntion\n";
  841.         cout << "\n\t\t\t\t6 - Exit System\n";
  842.         cout << "\t\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
  843.         cout << "\t\t\t\t\tEnter your Choice ->->-> : ";
  844.         cin >> choice;
  845.         cout << "\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n";
  846.         switch (choice)
  847.         {
  848.         case 1:
  849.             s_homeWork1();
  850.             break;
  851.         case 2:
  852.             s_homeWork2();
  853.             break;
  854.         case 3:
  855.             s_homeWork3();
  856.             break;
  857.         case 4:
  858.             s_homeWork4();
  859.             break;
  860.         case 5:
  861.             s_homeWork5();
  862.             break;
  863.         default:
  864.             cout << "\n\n\t\tBye Bye\n\n========================================n\n";
  865.             break;
  866.         }
  867.     } while (choice != 6);
  868. }
  869. //SINGLE LIST (home work function)|| HW 1
  870. void s_homeWork1()
  871. {
  872.     node *head, *tail;
  873.     head = tail = NULL;
  874.     singleList single;
  875.     char choice;
  876.     do
  877.     {
  878.         cout << "a) Create a linked list of seven nodes (int)\n";
  879.         cout << "b) Increment the value of the 3th and 5th node by 20\n";
  880.         cout << "c) Delete all the nodes where its data field is odd in linked list\n";
  881.         cout << "d) Exit System\n";
  882.         cout << "========================================\n";
  883.         cout << "Enter your Choice : ";
  884.         cin >> choice;
  885.         cout << "========================================\n";
  886.         switch (choice)
  887.         {
  888.         case 'a':
  889.             for (i = 0;i < 7;i++)
  890.             {
  891.                 cout << "Enter item : ";
  892.                 cin >> item;
  893.                 single.addEnd(item, head, tail);
  894.             }
  895.             break;
  896.         case 'b':
  897.             s_increment(head);
  898.             break;
  899.         case 'c':
  900.             delete_odd(head);
  901.             break;
  902.         default:
  903.             break;
  904.         }
  905.     } while (choice != 'd');
  906. }
  907.  
  908. //SINGLE LIST (home work function)|| HW 2
  909. void s_homeWork2()
  910. {
  911.     nodechar *head, *tail;
  912.     head = tail = NULL;
  913.     int i;
  914.     char item;
  915.     char choice;
  916.     do
  917.     {
  918.         cout << "a) Create this list\n";
  919.         cout << "b) Insert three nodes in end of list\n";
  920.         cout << "c) Delete 4th element\n";
  921.         cout << "d) Split the list for two list\n";
  922.         cout << "e) Exit System\n";
  923.         cout << "========================================\n";
  924.         cout << "Enter your Choice : ";
  925.         cin >> choice;
  926.         cout << "========================================\n";
  927.         switch (choice)
  928.         {
  929.         case 'a':
  930.             for (i = 0;i < 10;i++)
  931.             {
  932.                 cout << "Enter Character : ";
  933.                 cin >> item;
  934.                 addEndchar(item, head, tail);
  935.             }
  936.             break;
  937.         case 'b':
  938.             for (i = 0;i < 3;i++)
  939.             {
  940.                 cout << "Enter Character : ";
  941.                 cin >> item;
  942.                 addEndchar(item, head, tail);
  943.             }
  944.             break;
  945.         case 'c':
  946.             delete4th(head,tail);
  947.             break;
  948.         case 'd':
  949.             split(head, tail);
  950.             break;
  951.         default:
  952.             break;
  953.         }
  954.     } while (choice != 'e');
  955. }
  956. //SINGLE LIST (home work function)|| HW 3
  957. void s_homeWork3()
  958. {
  959.     node *head, *tail, *head2, *tail2, *head3, *tail3;
  960.     head = tail = head2 = tail2 = head3 = tail3 = NULL;
  961.     do
  962.     {
  963.         cout << "1) insert data in three list\n";
  964.         cout << "2) Merge all list in one list\n";
  965.         cout << "3) Exit System\n";
  966.         cout << "========================================\n";
  967.         cout << "Enter your Choice : ";
  968.         cin >> choice;
  969.         cout << "========================================\n";
  970.         switch (choice)
  971.         {
  972.         case 1:
  973.             cout << "\nEnter Data for Fisrt list\n\n";
  974.             for (i = 0;i < 3;i++)
  975.             {
  976.                 cout << "Enter item : ";
  977.                 cin >> item;
  978.                 single.addEnd(item, head, tail);
  979.             }
  980.             cout << "\nEnter Data for Second list\n\n";
  981.             for (i = 0;i < 4;i++)
  982.             {
  983.                 cout << "Enter item : ";
  984.                 cin >> item;
  985.                 single.addEnd(item, head2, tail2);
  986.             }
  987.             cout << "\nEnter Data for Third list\n\n";
  988.             for (i = 0;i < 5;i++)
  989.             {
  990.                 cout << "Enter item : ";
  991.                 cin >> item;
  992.                 single.addEnd(item, head3, tail3);
  993.             }
  994.             break;
  995.         case 2:
  996.             cout << "\n\tAfter Merge All\n";
  997.             mergelist(head,tail, head2, tail2, head3);
  998.             break;
  999.         default:
  1000.             break;
  1001.         }
  1002.     } while (choice != 3);
  1003. }
  1004. //SINGLE LIST (home work function)|| HW 4
  1005. void s_homeWork4()
  1006. {
  1007.     nodeEmp *head, *tail;
  1008.     tail = head = NULL;
  1009.     int age;
  1010.     string name;
  1011.     do
  1012.     {
  1013.         cout << "1 - Create list\n";
  1014.         cout << "2 - Display list\n";
  1015.         cout << "3 - Exit System\n";
  1016.         cout << "========================================\n";
  1017.         cout << "Enter your Choice : ";
  1018.         cin >> choice;
  1019.         cout << "========================================\n";
  1020.         switch (choice)
  1021.         {
  1022.         case 1:
  1023.             for (i = 0;i<2;i++)
  1024.             {
  1025.                 cout << "Enter name : ";
  1026.                 cin >> name;
  1027.                 cout << "Enter age : ";
  1028.                 cin >> age;
  1029.                 createListEmp(head, tail, age, name);
  1030.             }
  1031.             break;
  1032.         case 2:
  1033.             displayEmp(head);
  1034.             break;
  1035.         default:
  1036.             break;
  1037.         }
  1038.     } while (choice != 3);
  1039. }
  1040.  
  1041. //SINGLE LIST (home work function)|| HW 5
  1042. void s_homeWork5()
  1043. {
  1044.     node *head, *tail;
  1045.     tail = head = NULL;
  1046.     int x;
  1047.     do
  1048.     {
  1049.         cout << "1 - create list \n";
  1050.         cout << "2 - show the even number\n";
  1051.         cout << "3 - exit\n";
  1052.         cout << "========================================\n";
  1053.         cout << "Enter your Choice : ";
  1054.         cin >> choice;
  1055.         cout << "========================================\n";
  1056.         switch (choice)
  1057.         {
  1058.         case 1:
  1059.             for (i = 0;i<10;i++)
  1060.             {
  1061.                 cout << "Enter the item : ";
  1062.                 cin >> item;
  1063.                 single.addEnd(item, head, tail);
  1064.             }
  1065.             break;
  1066.         case 2:
  1067.             x = even_number(head);
  1068.             for (i = 0;i<x;i++)
  1069.             {
  1070.                 cout << "even data is : " << a[i] << endl;
  1071.             }
  1072.             break;
  1073.         default:
  1074.             break;
  1075.         }
  1076.     } while (choice != 3);
  1077. }
  1078.  
  1079. /*=====================
  1080. DOUBLE LINK LIST    ===
  1081. =======================
  1082. */
  1083. void doubleLab()
  1084. {
  1085.     doubleNode *head, *tail;
  1086.     tail = head = NULL;
  1087.     int choice, item, pos, i, lsize;
  1088.     do
  1089.     {
  1090.         cout << "1 - create double link list\n";
  1091.         cout << "2 - Add item in the End list\n";
  1092.         cout << "3 - Add item in the Begin list\n";
  1093.         cout << "4 - Add item By Position in list\n";
  1094.         cout << "5 - Add item By Data in list \n";
  1095.         cout << "6 - Display list item\n";
  1096.         cout << "7- Delete item in the End list\n";
  1097.         cout << "8 - Delete item in the Begin list\n";
  1098.         cout << "9 - Delete item By Position list\n";
  1099.         cout << "10 - Delete item By Data in list\n";
  1100.         cout << "11 - Exit System\n";
  1101.         cout << "========================================\n";
  1102.         cout << "Enter your Choice : ";
  1103.         cin >> choice;
  1104.         cout << "========================================\n";
  1105.         switch (choice)
  1106.         {
  1107.         case 1:
  1108.             cout << "Enter size of list : ";
  1109.             cin >> lsize;
  1110.             for (i = 0;i < lsize;i++)
  1111.             {
  1112.                 cout << "Enter item : ";
  1113.                 cin >> item;
  1114.                 doubleL.doubleAddBegin(item, head, tail);
  1115.             }
  1116.             break;
  1117.         case 2:
  1118.             cout << "Enter item : ";
  1119.             cin >> item;
  1120.             doubleL.doubleAddEnd(item, head, tail);
  1121.             break;
  1122.         case 3:
  1123.             cout << "Enter item : ";
  1124.             cin >> item;
  1125.             doubleL.doubleAddBegin(item, head, tail);
  1126.             break;
  1127.         case 4:
  1128.             cout << "Enter the position : ";
  1129.             cin >> pos;
  1130.             cout << "Enter the item : ";
  1131.             cin >> item;
  1132.             doubleL.doubleAddByPosition(item, head, tail, pos);
  1133.             break;
  1134.         case 5:
  1135.             cout << "Enter the data : ";
  1136.             cin >> pos;
  1137.             cout << "Enter item : ";
  1138.             cin >> item;
  1139.             doubleL.doubleAddByData(item, head, tail, pos);
  1140.             break;
  1141.         case 6:
  1142.             doubleL.doubleDisplay(head);
  1143.             break;
  1144.         case 7:
  1145.             doubleL.doubleDeleteEnd(head, tail);
  1146.             break;
  1147.         case 8:
  1148.             doubleL.doubleDeleteBegin(head);
  1149.             break;
  1150.         case 9:
  1151.             cout << "Enter the position : ";
  1152.             cin >> pos;
  1153.             doubleL.doubleDeleteByPosition(pos, head, tail);
  1154.             break;
  1155.         case 10:
  1156.             cout << "Enter the Data to delete : ";
  1157.             cin >> pos;
  1158.             doubleL.doubleDeleteByData(pos, head, tail);
  1159.             break;
  1160.         default:
  1161.             cout << "\n\n\t\tBye Bye\n\n========================================n\n";
  1162.             break;
  1163.         }
  1164.     } while (choice != 11);
  1165. }
  1166. //Lab functions
  1167. void doubleList::doubleAddEnd(int item, doubleNode *&head, doubleNode *&tail)
  1168. {
  1169.     doubleNode *newNode = new doubleNode;
  1170.     newNode->next = NULL;
  1171.     newNode->data = item;
  1172.     if (head == NULL)
  1173.     {
  1174.         newNode->perv = NULL;
  1175.         head = tail = newNode;
  1176.     }
  1177.     else
  1178.     {
  1179.         newNode->perv = tail;
  1180.         tail->next = newNode;
  1181.         tail = newNode;
  1182.     }
  1183. }
  1184.  
  1185. //Lab functions
  1186. void doubleList::doubleAddBegin(int item, doubleNode *&head, doubleNode *&tail)
  1187. {
  1188.     doubleNode *newNode = new doubleNode;
  1189.     newNode->perv = NULL;
  1190.     newNode->data = item;
  1191.     if (head == NULL)
  1192.     {
  1193.         newNode->next = NULL;
  1194.         head = tail = newNode;
  1195.     }
  1196.     else
  1197.     {
  1198.         newNode->next = head;
  1199.         head->perv = newNode;
  1200.         head = newNode;
  1201.     }
  1202. }
  1203.  
  1204. //Lab functions
  1205. void doubleList::doubleAddByPosition(int item, doubleNode *&head, doubleNode *&tail, int pos)
  1206. {
  1207.     doubleNode *newNode = new doubleNode;
  1208.     newNode->next = NULL;
  1209.     newNode->data = item;
  1210.     doubleNode *temp;
  1211.     temp = head;
  1212.     int tempos = 1;
  1213.     if (head == NULL)
  1214.     {
  1215.         cout << "\nEmpty list, location not found\n";
  1216.     }
  1217.     else
  1218.     {
  1219.         while (tempos != pos)
  1220.         {
  1221.             if (temp == NULL)
  1222.             {
  1223.                 cout << "The required location not found\n";
  1224.             }
  1225.             else
  1226.             {
  1227.                 temp = temp->next;
  1228.             }
  1229.             tempos++;
  1230.         }
  1231.     }
  1232.     if (temp->next == NULL)
  1233.     {
  1234.         temp->next = newNode;
  1235.         newNode->perv = temp;
  1236.         newNode->next = NULL;
  1237.         tail = newNode;
  1238.     }
  1239.     else
  1240.     {
  1241.         newNode->perv = temp;
  1242.         newNode->next = temp->next;
  1243.         temp->next->perv = newNode;
  1244.         temp->next = newNode;
  1245.     }
  1246. }
  1247.  
  1248. //lab function
  1249. void doubleList::doubleAddByData(int item, doubleNode *&head, doubleNode *&tail, int value)
  1250. {
  1251.     doubleNode *newNode = new doubleNode;
  1252.     newNode->next = NULL;
  1253.     newNode->data = item;
  1254.     doubleNode *temp;
  1255.     temp = head;
  1256.     if (head == NULL)
  1257.     {
  1258.         cout << "\nEmpty list, location not found\n";
  1259.     }
  1260.     else
  1261.     {
  1262.         while (temp->data != value)
  1263.         {
  1264.             if (temp == NULL)
  1265.             {
  1266.                 cout << "The required location not found\n";
  1267.             }
  1268.             else
  1269.             {
  1270.                 temp = temp->next;
  1271.             }
  1272.         }
  1273.     }
  1274.     if (temp->next == NULL)
  1275.     {
  1276.         temp->next = newNode;
  1277.         newNode->perv = temp;
  1278.         newNode->next = NULL;
  1279.         tail = newNode;
  1280.     }
  1281.     else
  1282.     {
  1283.         newNode->perv = temp;
  1284.         newNode->next = temp->next;
  1285.         temp->next->perv = newNode;
  1286.         temp->next = newNode;
  1287.     }
  1288. }
  1289.  
  1290. //lab function
  1291. void doubleList::doubleDisplay(doubleNode *&head)
  1292. {
  1293.     doubleNode *temp = new doubleNode;
  1294.     temp = head;
  1295.     if (head == NULL)
  1296.     {
  1297.         cout << "Empty list\n";
  1298.     }
  1299.     else
  1300.     {
  1301.         while (temp != NULL)
  1302.         {
  1303.             cout << "--| " << temp->data << endl;
  1304.             temp = temp->next;
  1305.         }
  1306.     }
  1307.     cout << endl;
  1308. }
  1309.  
  1310. //lab function
  1311. void doubleList::doubleDeleteEnd(doubleNode *&head, doubleNode *&tail)
  1312. {
  1313.     if (head == NULL)
  1314.     {
  1315.         cout << "Empty list\n";
  1316.         return;
  1317.     }
  1318.     else
  1319.     {
  1320.         doubleNode *temp;
  1321.         temp = tail;
  1322.         if (temp->next == NULL && temp->perv == NULL)
  1323.         {
  1324.             head = tail = NULL;
  1325.             delete temp;
  1326.         }
  1327.         else
  1328.         {
  1329.             tail = tail->perv;
  1330.             tail->next = NULL;
  1331.             delete temp;
  1332.         }
  1333.     }
  1334. }
  1335.  
  1336. //lab function
  1337. void doubleList::doubleDeleteBegin(doubleNode *&head)
  1338. {
  1339.     if (head == NULL)
  1340.     {
  1341.         cout << "Empty list\n";
  1342.         return;
  1343.     }
  1344.     else
  1345.     {
  1346.         doubleNode *temp;
  1347.         temp = head;
  1348.         if (temp->next == NULL)
  1349.         {
  1350.             head = NULL;
  1351.             delete temp;
  1352.         }
  1353.         else
  1354.         {
  1355.             head = head->next;
  1356.             head->perv = NULL;
  1357.             delete temp;
  1358.         }
  1359.     }
  1360. }
  1361.  
  1362. //lab function
  1363. void doubleList::doubleDeleteByPosition(int pos, doubleNode *&head, doubleNode *&tail)
  1364. {
  1365.     doubleNode *temp;
  1366.     temp = head;
  1367.     int tempos = 1;
  1368.     if (head == NULL)
  1369.     {
  1370.         cout << "Empty list\n";
  1371.         return;
  1372.     }
  1373.     else
  1374.     {
  1375.         while (tempos != pos)
  1376.         {
  1377.             if (temp = NULL)
  1378.             {
  1379.                 cout << "The required position not found\n";
  1380.                 return;
  1381.             }
  1382.             else
  1383.             {
  1384.                 temp = temp->next;
  1385.             }
  1386.             tempos++;
  1387.         }
  1388.     }
  1389.     if (temp->next == NULL && temp->perv == NULL)
  1390.     {
  1391.         head = tail = NULL;
  1392.         delete temp;
  1393.     }
  1394.     else if (temp == head)
  1395.     {
  1396.         head = head->next;
  1397.         head->perv = NULL;
  1398.         delete temp;
  1399.     }
  1400.     else if (temp->next == NULL)
  1401.     {
  1402.         tail = tail->perv;
  1403.         tail->next = NULL;
  1404.         delete temp;
  1405.     }
  1406.     else
  1407.     {
  1408.         temp->perv->next = temp->next;
  1409.         temp->next->perv = temp->perv;
  1410.         delete temp;
  1411.     }
  1412. }
  1413.  
  1414. //lab function
  1415. void doubleList::doubleDeleteByData(int value, doubleNode *&head, doubleNode *&tail)
  1416. {
  1417.     doubleNode *temp;
  1418.     temp = head;
  1419.     if (head == NULL)
  1420.     {
  1421.         cout << "Empty list\n";
  1422.         return;
  1423.     }
  1424.     else
  1425.     {
  1426.         while (temp->data != value)
  1427.         {
  1428.             if (temp = NULL)
  1429.             {
  1430.                 cout << "The required position not found\n";
  1431.                 return;
  1432.             }
  1433.             else
  1434.             {
  1435.                 temp = temp->next;
  1436.             }
  1437.         }
  1438.     }
  1439.     if (temp->next == NULL && temp->perv == NULL)
  1440.     {
  1441.         head = tail = NULL;
  1442.         delete temp;
  1443.     }
  1444.     else if (temp == head)
  1445.     {
  1446.         head = head->next;
  1447.         head->perv = NULL;
  1448.         delete temp;
  1449.     }
  1450.     else if (temp->next == NULL)
  1451.     {
  1452.         tail = tail->perv;
  1453.         tail->next = NULL;
  1454.         delete temp;
  1455.     }
  1456.     else
  1457.     {
  1458.         temp->perv->next = temp->next;
  1459.         temp->next->perv = temp->perv;
  1460.         delete temp;
  1461.     }
  1462. }
  1463. //DOUBLE LIST (home work function)|| HW 2
  1464. //Swap between two list content
  1465. void swapList(doubleNode *&head, doubleNode *&head2)
  1466. {
  1467.     int temp;
  1468.     doubleNode *temp1, *temp2,*temp3,*temp4;
  1469.     temp1 = temp3 = head;
  1470.     temp2 = temp4 = head2;
  1471.     if (head == NULL || head2 == NULL)
  1472.     {
  1473.         cout << "one of list is empty please Enter some data....\n";
  1474.         return;
  1475.     }
  1476.     else
  1477.     {
  1478.         for (i = 0;i < 5;i++)
  1479.         {
  1480.             temp = temp1->data;
  1481.             temp1->data = temp2->data;
  1482.             temp2->data = temp;
  1483.             temp1 = temp1->next;
  1484.             temp2 = temp2->next;
  1485.         }
  1486.         cout << "\nfirst list\n";
  1487.         doubleL.doubleDisplay(temp3);
  1488.         cout << "\nsecond list\n";
  1489.         doubleL.doubleDisplay(temp4);
  1490.     }
  1491. }
  1492.  
  1493. //DOUBLE LIST (home work function)|| HW 3
  1494. //Remove all duplicate item in list
  1495. void duplicate(doubleNode *&head)
  1496. {
  1497.     doubleNode *temp, *temp1,*find,*temp3;
  1498.     temp3 = temp=head;
  1499.  
  1500.     // pick elements one by one
  1501.     while (temp != NULL) {
  1502.         temp1 = temp->next;
  1503.         while (temp1 != NULL) {
  1504.             if (temp->data == temp1->data) {
  1505.                 find = temp1->next;
  1506.                 delete temp1;
  1507.                 temp1 = find;
  1508.                 temp->next = find;
  1509.                 find->perv = temp;
  1510.             }
  1511.             else
  1512.                 temp1 = temp1->next;
  1513.         }
  1514.         temp = temp->next;
  1515.     }
  1516.     doubleL.doubleDisplay(temp3);
  1517. }
  1518.  
  1519. //DOUBLE LIST (home work function)|| HW 4
  1520. //Remove all occurrences by given key
  1521. void deleteAllByKey(doubleNode *&head,doubleNode *&tail,int key)
  1522. {
  1523.     doubleNode *temp,*helper,*root;
  1524.     temp = helper = root = head;
  1525.     if (head == NULL)
  1526.     {
  1527.         cout << "Empty list\n";
  1528.         return;
  1529.     }
  1530.     else
  1531.     {
  1532.         while (temp != NULL)
  1533.         {
  1534.             if (temp->data == key)
  1535.             {
  1536.                 if (temp == root)
  1537.                 {
  1538.                     helper = temp;
  1539.                     temp = temp->next;
  1540.                     temp->perv = NULL;
  1541.                     head = temp;
  1542.                     delete helper;
  1543.  
  1544.                 }
  1545.                 else if (temp == tail)
  1546.                 {
  1547.                     helper = temp;
  1548.                     tail = helper->perv;
  1549.                     tail->next = NULL;
  1550.                     delete helper;
  1551.                     temp = NULL;
  1552.                 }
  1553.                 else
  1554.                 {
  1555.                     helper->next = temp->next;
  1556.                     temp->next = NULL;
  1557.                     delete temp;
  1558.                     temp = helper->next;
  1559.                     helper->next->perv = helper;
  1560.                 }
  1561.             }
  1562.             else
  1563.             {
  1564.                 helper = temp;
  1565.                 temp = temp->next;
  1566.             }
  1567.         }
  1568.     }
  1569.     doubleL.doubleDisplay(head);
  1570. }
  1571. /*==========
  1572. DOUBLE HW ==
  1573. ============*/
  1574. //home work choices function
  1575. void doubleHomeWork()
  1576. {
  1577.     do
  1578.     {
  1579.         cout << "1) Write C++ progem to create n- node double linked list and then prints the contents of list\n";
  1580.         cout << "2) Write a program to input 5 nodes in two different int double linked list (H1 and H2), Swap the element of the nodes in list H1 with list H2\n";
  1581.         cout << "3) Assume if you have doubly linked list containing n nodes, Write only function to remove duplicate nodes from the list\n";
  1582.         cout << "4) Write complete program to construct double linked list of (8) nodes, then delete all occurrences of a given key (x) in doubly linked list\n";
  1583.         cout << "5) Exit system\n";
  1584.         cout << "========================================\n";
  1585.         cout << "Enter your Choice : ";
  1586.         cin >> choice;
  1587.         cout << "========================================\n";
  1588.         switch (choice)
  1589.         {
  1590.         case 1:
  1591.             doubleHW1();
  1592.             break;
  1593.         case 2:
  1594.             doubleHW2();
  1595.             break;
  1596.         case 3:
  1597.             doubleHW3();
  1598.             break;
  1599.         case 4:
  1600.             doubleHW4();
  1601.             break;
  1602.         default:
  1603.             break;
  1604.         }
  1605.     } while (choice != 5);
  1606. }
  1607.  
  1608. //DOUBLE LIST (home work function)|| HW 1
  1609. void doubleHW1()
  1610. {
  1611.     doubleNode *head, *tail;
  1612.     tail = head = NULL;
  1613.     do
  1614.     {
  1615.         cout << "1)Create list\n";
  1616.         cout << "2)Display list\n";
  1617.         cout << "3)Back\n";
  1618.         cout << "========================================\n";
  1619.         cout << "Enter your Choice : ";
  1620.         cin >> choice;
  1621.         cout << "========================================\n";
  1622.         switch (choice)
  1623.         {
  1624.         case 1:
  1625.             cout << "Enter size of list : ";
  1626.             cin >> lsize;
  1627.             for (i = 0;i < lsize;i++)
  1628.             {
  1629.                 cout << "Enter element : ";
  1630.                 cin >> item;
  1631.                 doubleL.doubleAddEnd(item, head, tail);
  1632.             }
  1633.             break;
  1634.         case 2:
  1635.             doubleL.doubleDisplay(head);
  1636.             break;
  1637.         default:
  1638.             break;
  1639.         }
  1640.     } while (choice != 3);
  1641. }
  1642.  
  1643. //DOUBLE LIST (home work function)|| HW 2
  1644. void doubleHW2()
  1645. {
  1646.     doubleNode *tail, *head,*tail2,*head2;
  1647.     head = tail = head2 = tail2 = NULL;
  1648.     do
  1649.     {
  1650.         cout << "1)Create First list\n";
  1651.         cout << "2)Create Second list\n";
  1652.         cout << "3)Swap between two list\n";
  1653.         cout << "4)Back\n";
  1654.         cout << "========================================\n";
  1655.         cout << "Enter your Choice : ";
  1656.         cin >> choice;
  1657.         cout << "========================================\n";
  1658.         switch (choice)
  1659.         {
  1660.         case 1:
  1661.             for (i = 0;i < 5;i++)
  1662.             {
  1663.                 cout << "Enter item : ";
  1664.                 cin >> item;
  1665.                 doubleL.doubleAddEnd(item, head, tail);
  1666.             }
  1667.             break;
  1668.         case 2:
  1669.             for (i = 0;i < 5;i++)
  1670.             {
  1671.                 cout << "Enter item : ";
  1672.                 cin >> item;
  1673.                 doubleL.doubleAddEnd(item, head2, tail2);
  1674.             }
  1675.             break;
  1676.         case 3:
  1677.             swapList(head, head2);
  1678.             break;
  1679.         default:
  1680.             break;
  1681.         }
  1682.     } while (choice != 4);
  1683. }
  1684.  
  1685. //DOUBLE LIST (home work function)|| HW 3
  1686. void doubleHW3()
  1687. {
  1688.     doubleNode *tail, *head;
  1689.     head = tail = NULL;
  1690.     do
  1691.     {
  1692.         cout << "1)Create List\n";
  1693.         cout << "2)Remove duplicate item\n";
  1694.         cout << "3)Back\n";
  1695.         cout << "========================================\n";
  1696.         cout << "Enter your Choice : ";
  1697.         cin >> choice;
  1698.         cout << "========================================\n";
  1699.         switch (choice)
  1700.         {
  1701.         case 1:
  1702.             cout << "Enter list size : ";
  1703.             cin >> lsize;
  1704.             for (i = 0;i < lsize;i++)
  1705.             {
  1706.                 cout << "Enter item : ";
  1707.                 cin >> item;
  1708.                 doubleL.doubleAddEnd(item, head, tail);
  1709.             }
  1710.             break;
  1711.         case 2:
  1712.             duplicate(head);
  1713.             break;
  1714.         default:
  1715.             break;
  1716.         }
  1717.     } while (choice != 3);
  1718. }
  1719.  
  1720. //DOUBLE LIST (home work function)|| HW 4
  1721. void doubleHW4()
  1722. {
  1723.     doubleNode *head, *tail;
  1724.     head = tail = NULL;
  1725.     int key;
  1726.     do
  1727.     {
  1728.         cout << "1)Create list\n";
  1729.         cout << "2)Delete all occurrences of a given key\n";
  1730.         cout << "3)Back\n";
  1731.         cout << "========================================\n";
  1732.         cout << "Enter your Choice : ";
  1733.         cin >> choice;
  1734.         cout << "========================================\n";
  1735.         switch (choice)
  1736.         {
  1737.         case 1:
  1738.             for (i = 0;i < 4;i++)
  1739.             {
  1740.                 cout << "Enter item : ";
  1741.                 cin >> item;
  1742.                 doubleL.doubleAddEnd(item, head, tail);
  1743.             }
  1744.             break;
  1745.         case 2:
  1746.             cout << "Enter the key : ";
  1747.             cin >> key;
  1748.             deleteAllByKey(head,tail,key);
  1749.             break;
  1750.         default:
  1751.             break;
  1752.         }
  1753.     } while (choice != 3);
  1754. }
Advertisement
Add Comment
Please, Sign In to add comment