Advertisement
jeff69

Mario's Homework new nigga

Jul 15th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.40 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. using namespace std;
  5.  
  6.     struct node
  7.  
  8.     {
  9.  
  10.         int info;
  11.  
  12.         struct node *next;
  13.  
  14.         struct node *prev;
  15.  
  16.     }*start;
  17.  
  18.  
  19.  
  20.     /*
  21.  
  22.      Class Declaration
  23.  
  24.      */
  25.  
  26.     class double_llist
  27.  
  28.     {
  29.  
  30.         public:
  31.  
  32.             void create_list(int value);
  33.  
  34.             void add_begin(int value);
  35.  
  36.             void add_after(int value, int position);
  37.  
  38.             void delete_element(int value);
  39.  
  40.             int search_element(int value);
  41.  
  42.             void display_dlist();
  43.  
  44.             void count();
  45.  
  46.             void reverse();
  47.  
  48.             double_llist()
  49.  
  50.             {
  51.  
  52.                 start = NULL;
  53.  
  54.             }
  55.  
  56.     };
  57.  
  58.  
  59.  
  60.     /*
  61.  
  62.      * Main: Conatins Menu
  63.  
  64.      */
  65. #define search sgdfg
  66. int double_llist::search_element(int value)
  67. {
  68.         struct node *q = start;
  69.  
  70.     int cnt = 0;
  71.  
  72.     while (q != NULL)
  73.     {
  74.         if(q->info==value)return cnt;
  75.         q = q->next;
  76.         cnt++;
  77.     }
  78.     return -1;
  79. }
  80.     int main()
  81.  
  82.     {
  83.  
  84.         int choice, element, position;
  85.  
  86.         double_llist dl;
  87.  
  88.         while (1)
  89.  
  90.         {
  91.  
  92.             cout<<endl<<"----------------------------"<<endl;
  93.  
  94.             cout<<endl<<"Operations on Doubly linked list"<<endl;
  95.  
  96.             cout<<endl<<"----------------------------"<<endl;
  97.  
  98.             cout<<"1.Create Node"<<endl;
  99.  
  100.             cout<<"2.Add at begining"<<endl;
  101.  
  102.             cout<<"3.Add after position"<<endl;
  103.  
  104.             cout<<"4.Delete"<<endl;
  105.  
  106.             cout<<"5.Display"<<endl;
  107.  
  108.             cout<<"6.Count"<<endl;
  109.  
  110.             cout<<"7.Reverse"<<endl;
  111.  
  112.             cout<<"8.Quit"<<endl;
  113.  
  114.             cout<<"9.search element"<<endl;
  115.  
  116.  
  117.  
  118.             cout<<"Enter your choice : ";
  119.  
  120.             cin>>choice;
  121.  
  122.             switch ( choice )
  123.  
  124.             {
  125.  
  126.             case 1:
  127.  
  128.                 cout<<"Enter the element: ";
  129.  
  130.                 cin>>element;
  131.  
  132.                 dl.create_list(element);
  133.  
  134.                 cout<<endl;
  135.  
  136.                 break;
  137.  
  138.             case 2:
  139.  
  140.                 cout<<"Enter the element: ";
  141.  
  142.                 cin>>element;
  143.  
  144.                 dl.add_begin(element);
  145.  
  146.                 cout<<endl;
  147.  
  148.                 break;
  149.  
  150.             case 3:
  151.  
  152.                 cout<<"Enter the element: ";
  153.  
  154.                 cin>>element;
  155.  
  156.                 cout<<"Insert Element after postion: ";
  157.  
  158.                 cin>>position;
  159.  
  160.                 dl.add_after(element, position);
  161.  
  162.                 cout<<endl;
  163.  
  164.                 break;
  165.  
  166.             case 4:
  167.  
  168.                 if (start == NULL)
  169.  
  170.                 {
  171.  
  172.                     cout<<"List empty,nothing to delete"<<endl;
  173.  
  174.                     break;
  175.  
  176.                 }
  177.  
  178.                 cout<<"Enter the element for deletion: ";
  179.  
  180.                 cin>>element;
  181.  
  182.                 dl.delete_element(element);
  183.  
  184.                 cout<<endl;
  185.  
  186.                 break;
  187.  
  188.             case 5:
  189.  
  190.                 dl.display_dlist();
  191.  
  192.                 cout<<endl;
  193.  
  194.                 break;
  195.  
  196.             case 6:
  197.  
  198.                 dl.count();
  199.  
  200.                 break;
  201.  
  202.             case 7:
  203.  
  204.                 if (start == NULL)
  205.  
  206.                 {
  207.  
  208.                     cout<<"List empty,nothing to reverse"<<endl;
  209.  
  210.                     break;
  211.  
  212.                 }
  213.  
  214.                 dl.reverse();
  215.  
  216.                 cout<<endl;
  217.  
  218.                 break;
  219.  
  220.             case 8:
  221.  
  222.                 exit(1);
  223.             case 9:
  224.                 int vv;
  225.                 cin>>vv;
  226.                 cout<<dl.search_element(vv)<<endl;;
  227.             break;
  228.             default:
  229.  
  230.                 cout<<"Wrong choice"<<endl;
  231.  
  232.             }
  233.  
  234.         }
  235.  
  236.         return 0;
  237.  
  238.     }
  239.  
  240.  
  241.  
  242.     /*
  243.  
  244.      * Create Double Link List
  245.  
  246.      */
  247.  
  248.     void double_llist::create_list(int value)
  249.  
  250.     {
  251.  
  252.         struct node *s, *temp;
  253.  
  254.         temp = new(struct node);
  255.  
  256.         temp->info = value;
  257.  
  258.         temp->next = NULL;
  259.  
  260.         if (start == NULL)
  261.  
  262.         {
  263.  
  264.             temp->prev = NULL;
  265.  
  266.             start = temp;
  267.  
  268.         }
  269.  
  270.         else
  271.  
  272.         {
  273.  
  274.             s = start;
  275.  
  276.             while (s->next != NULL)
  277.  
  278.                 s = s->next;
  279.  
  280.             s->next = temp;
  281.  
  282.             temp->prev = s;
  283.  
  284.         }
  285.  
  286.     }
  287.  
  288.  
  289.  
  290.     /*
  291.  
  292.      * Insertion at the beginning
  293.  
  294.      */
  295.  
  296.     void double_llist::add_begin(int value)
  297.  
  298.     {
  299.  
  300.         if (start == NULL)
  301.  
  302.         {
  303.  
  304.             cout<<"First Create the list."<<endl;
  305.  
  306.             return;
  307.  
  308.         }
  309.  
  310.         struct node *temp;
  311.  
  312.         temp = new(struct node);
  313.  
  314.         temp->prev = NULL;
  315.  
  316.         temp->info = value;
  317.  
  318.         temp->next = start;
  319.  
  320.         start->prev = temp;
  321.  
  322.         start = temp;
  323.  
  324.         cout<<"Element Inserted"<<endl;
  325.  
  326.     }
  327.  
  328.  
  329.  
  330.     /*
  331.  
  332.      * Insertion of element at a particular position
  333.  
  334.      */
  335.  
  336.     void double_llist::add_after(int value, int pos)
  337.  
  338.     {
  339.  
  340.         if (start == NULL)
  341.  
  342.         {
  343.  
  344.             cout<<"First Create the list."<<endl;
  345.  
  346.             return;
  347.  
  348.         }
  349.  
  350.         struct node *tmp, *q;
  351.  
  352.         int i;
  353.  
  354.         q = start;
  355.  
  356.         for (i = 0;i < pos - 1;i++)
  357.  
  358.         {
  359.  
  360.             q = q->next;
  361.  
  362.             if (q == NULL)
  363.  
  364.             {
  365.  
  366.                 cout<<"There are less than ";
  367.  
  368.                 cout<<pos<<" elements."<<endl;
  369.  
  370.                 return;
  371.  
  372.             }
  373.  
  374.         }
  375.  
  376.         tmp = new(struct node);
  377.  
  378.         tmp->info = value;
  379.  
  380.         if (q->next == NULL)
  381.  
  382.         {
  383.  
  384.             q->next = tmp;
  385.  
  386.             tmp->next = NULL;
  387.  
  388.             tmp->prev = q;
  389.  
  390.         }
  391.  
  392.         else
  393.  
  394.         {
  395.  
  396.             tmp->next = q->next;
  397.  
  398.             tmp->next->prev = tmp;
  399.  
  400.             q->next = tmp;
  401.  
  402.             tmp->prev = q;
  403.  
  404.         }
  405.  
  406.         cout<<"Element Inserted"<<endl;
  407.  
  408.     }
  409.  
  410.  
  411.  
  412.     /*
  413.  
  414.      * Deletion of element from the list
  415.  
  416.      */
  417.  
  418.     void double_llist::delete_element(int value)
  419.  
  420.     {
  421.  
  422.         struct node *tmp, *q;
  423.  
  424.          /*first element deletion*/
  425.  
  426.         if (start->info == value)
  427.  
  428.         {
  429.  
  430.             tmp = start;
  431.  
  432.             start = start->next;
  433.  
  434.             start->prev = NULL;
  435.  
  436.             cout<<"Element Deleted"<<endl;
  437.  
  438.             free(tmp);
  439.  
  440.             return;
  441.  
  442.         }
  443.  
  444.         q = start;
  445.  
  446.         while (q->next->next != NULL)
  447.  
  448.         {
  449.  
  450.             /*Element deleted in between*/
  451.  
  452.             if (q->next->info == value)
  453.  
  454.             {
  455.  
  456.                 tmp = q->next;
  457.  
  458.                 q->next = tmp->next;
  459.  
  460.                 tmp->next->prev = q;
  461.  
  462.                 cout<<"Element Deleted"<<endl;
  463.  
  464.                 free(tmp);
  465.  
  466.                 return;
  467.  
  468.             }
  469.  
  470.             q = q->next;
  471.  
  472.         }
  473.  
  474.          /*last element deleted*/
  475.  
  476.         if (q->next->info == value)
  477.  
  478.         {
  479.  
  480.             tmp = q->next;
  481.  
  482.             free(tmp);
  483.  
  484.             q->next = NULL;
  485.  
  486.             cout<<"Element Deleted"<<endl;
  487.  
  488.             return;
  489.  
  490.         }
  491.  
  492.         cout<<"Element "<<value<<" not found"<<endl;
  493.  
  494.     }
  495.  
  496.  
  497.  
  498.     /*
  499.  
  500.      * Display elements of Doubly Link List
  501.  
  502.      */
  503.  
  504.     void double_llist::display_dlist()
  505.  
  506.     {
  507.  
  508.         struct node *q;
  509.  
  510.         if (start == NULL)
  511.  
  512.         {
  513.  
  514.             cout<<"List empty,nothing to display"<<endl;
  515.  
  516.             return;
  517.  
  518.         }
  519.  
  520.         q = start;
  521.  
  522.         cout<<"The Doubly Link List is :"<<endl;
  523.  
  524.         while (q != NULL)
  525.  
  526.         {
  527.  
  528.             cout<<q->info<<" <-> ";
  529.  
  530.             q = q->next;
  531.  
  532.         }
  533.  
  534.         cout<<"NULL"<<endl;
  535.  
  536.     }
  537.  
  538.  
  539.  
  540.     /*
  541.  
  542.      * Number of elements in Doubly Link List
  543.  
  544.      */
  545.  
  546.     void double_llist::count()
  547.  
  548.     {
  549.  
  550.         struct node *q = start;
  551.  
  552.         int cnt = 0;
  553.  
  554.         while (q != NULL)
  555.  
  556.         {
  557.  
  558.             q = q->next;
  559.  
  560.             cnt++;
  561.  
  562.         }
  563.  
  564.         cout<<"Number of elements are: "<<cnt<<endl;
  565.  
  566.     }
  567.  
  568.  
  569.  
  570.     /*
  571.  
  572.      * Reverse Doubly Link List
  573.  
  574.      */
  575.  
  576.     void double_llist::reverse()
  577.  
  578.     {
  579.  
  580.         struct node *p1, *p2;
  581.  
  582.         p1 = start;
  583.  
  584.         p2 = p1->next;
  585.  
  586.         p1->next = NULL;
  587.  
  588.         p1->prev = p2;
  589.  
  590.         while (p2 != NULL)
  591.  
  592.         {
  593.  
  594.             p2->prev = p2->next;
  595.  
  596.             p2->next = p1;
  597.  
  598.             p1 = p2;
  599.  
  600.             p2 = p2->prev;
  601.  
  602.         }
  603.  
  604.         start = p1;
  605.  
  606.         cout<<"List Reversed"<<endl;
  607.  
  608.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement