Advertisement
jeff69

NEW AIR

Jul 15th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.59 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include <vector>
  5. using namespace std;
  6.  
  7.     struct node
  8.  
  9.     {
  10.  
  11.         int info;
  12.  
  13.         struct node *next;
  14.  
  15.         struct node *prev;
  16.  
  17.     }*start,*last;
  18.  
  19.  
  20.  
  21.     /*
  22.  
  23.      Class Declaration
  24.  
  25.      */
  26.  
  27.     class double_llist
  28.  
  29.     {
  30.  
  31.         public:
  32.  
  33.             void create_list(int value);
  34.  
  35.             void add_after(int value, int position);
  36.  
  37.             void delete_element_first(int value);
  38.             void delete_element_last();
  39.  
  40.             int search_element(int value);
  41.  
  42.             void display_dlist();
  43.  
  44.  
  45.             double_llist()
  46.  
  47.             {
  48.  
  49.                 start = NULL;
  50.                 last= NULL;
  51.  
  52.             }
  53.  
  54.     };
  55.  
  56.  
  57.  
  58.     /*
  59.  
  60.      * Main: Conatins Menu
  61.  
  62.      */
  63.  
  64.     int main()
  65.  
  66.     {
  67.         struct node *q = start;
  68.  
  69.         int choice, element, position;
  70.  
  71.         double_llist dl;
  72.  
  73.         while (1)
  74.  
  75.         {
  76.  //خيارات ستفيد منها المستخدم لمعرفة ايلامور التي يجب ان يفعلها
  77.             cout<<endl<<"----------------------------"<<endl;
  78.  
  79.             cout<<endl<<"Operations on Doubly linked list"<<endl;
  80.  
  81.             cout<<endl<<"----------------------------"<<endl;
  82.  
  83.             cout<<"1.Create Node"<<endl;
  84.  
  85.  
  86.             cout<<"2.Add after position"<<endl;
  87.  
  88.             cout<<"3.Delete first "<<endl;
  89.                cout<<"4.Delete last "<<endl;
  90.                cout<<"5.search element"<<endl;
  91.  
  92.             cout<<"6.Display"<<endl;
  93.  
  94.  
  95.  
  96.             cout<<"7.Quit"<<endl;
  97.  
  98.  
  99.  
  100.  
  101.  
  102.             cout<<"Enter your choice : ";
  103.  
  104.             cin>>choice;
  105.  
  106.             switch ( choice )
  107.  
  108.             {
  109.  
  110.             case 1:
  111.  
  112.                 cout<<"Enter the element: ";
  113.  
  114.                 cin>>element;
  115.  
  116.                 dl.create_list(element);
  117.  
  118.                 cout<<endl;
  119.  
  120.                 break;
  121.  
  122.  
  123.  
  124.             case 2:
  125.  
  126.                 cout<<"Enter the element: ";
  127.  
  128.                 cin>>element;
  129.  
  130.                 cout<<"Insert Element after postion: ";
  131.  
  132.                 cin>>position;
  133.  
  134.                 dl.add_after(element, position);
  135.  
  136.                 cout<<endl;
  137.  
  138.                 break;
  139.  
  140.             case 3:
  141.  
  142.                 if (start == NULL)
  143.  
  144.                 {
  145.  
  146.                     cout<<"List empty,nothing to delete"<<endl;
  147.  
  148.                     break;
  149.  
  150.                 }
  151.  
  152.  
  153.                 dl.delete_element_first(start->info);
  154.  
  155.                 cout<<endl;
  156.  
  157.                 break;
  158.                 case 4 :
  159.                 if (start == NULL)
  160.  
  161.                 {
  162.  
  163.                     cout<<"List empty,nothing to delete"<<endl;
  164.  
  165.                     break;
  166.  
  167.                 }
  168.  
  169.  
  170.                 dl.delete_element_last();
  171.  
  172.                 cout<<endl;
  173.                 break;
  174.  
  175.               case 5:
  176.                 int value;
  177.                 cout<<"Enter the value please";
  178.                 cin>>value;
  179.                 cout<<dl.search_element(value)<<endl;;
  180.             break;
  181.  
  182.             case 6:
  183.  
  184.                 dl.display_dlist();
  185.  
  186.                 cout<<endl;
  187.  
  188.                 break;
  189.  
  190.  
  191.  
  192.  
  193.             case 7:
  194.  
  195.                 exit(1);
  196.  
  197.             default:
  198.  
  199.                 cout<<"Wrong choice"<<endl;
  200.  
  201.             }
  202.  
  203.         }
  204.  
  205.         return 0;
  206.  
  207.     }
  208.  
  209.  
  210.  
  211.     /*
  212.  
  213.      * Create Double Link List
  214.  
  215.      */
  216.  
  217.     void double_llist::create_list(int value)// تابع لإنشاء السجل
  218.  
  219.     {
  220.  
  221.         struct node *s, *temp;
  222.  
  223.         temp = new(struct node);
  224.  
  225.         temp->info = value;
  226.  
  227.         temp->next = NULL;
  228.  
  229.         if (start == NULL)
  230.  
  231.         {
  232.  
  233.             temp->prev = NULL;
  234.  
  235.             start = temp;
  236.  
  237.         }
  238.  
  239.         else
  240.  
  241.         {
  242.  
  243.             s = start;
  244.  
  245.             while (s->next != NULL)
  246.  
  247.                 s = s->next;
  248.  
  249.             s->next = temp;
  250.  
  251.             temp->prev = s;
  252.  
  253.         }
  254.  
  255.     }
  256.  
  257.  
  258.  
  259.  
  260.     void double_llist::add_after(int value, int pos)// تابع نقوم من خلاله بإضافة قيمة إلى موقع محدد نقوم من خلاله بعمليه تأخير للعناصر التي تسبق الموقع وتقديم للعناصر التي تليه
  261.  
  262.     {
  263.  
  264.         if (start == NULL)
  265.  
  266.         {
  267.  
  268.             cout<<"First Create the list."<<endl;
  269.  
  270.             return;
  271.  
  272.         }
  273.  
  274.         struct node *tmp, *q;
  275.  
  276.         int i;
  277.  
  278.         q = start;
  279.  
  280.         for (i = 0;i < pos - 1;i++)
  281.  
  282.         {
  283.  
  284.             q = q->next;
  285.  
  286.             if (q == NULL)
  287.  
  288.             {
  289.  
  290.                 cout<<"There are less than ";
  291.  
  292.                 cout<<pos<<" elements."<<endl;
  293.  
  294.                 return;
  295.  
  296.             }
  297.  
  298.         }
  299.  
  300.         tmp = new(struct node);
  301.  
  302.         tmp->info = value;
  303.  
  304.         if (q->next == NULL)
  305.  
  306.         {
  307.  
  308.             q->next = tmp;
  309.  
  310.             tmp->next = NULL;
  311.  
  312.             tmp->prev = q;
  313.  
  314.         }
  315.  
  316.         else
  317.  
  318.         {
  319.  
  320.             tmp->next = q->next;
  321.  
  322.             tmp->next->prev = tmp;
  323.  
  324.             q->next = tmp;
  325.  
  326.             tmp->prev = q;
  327.  
  328.         }
  329.  
  330.         cout<<"Element Inserted"<<endl;
  331.  
  332.     }
  333.  
  334.  
  335.  
  336.     /*
  337.  
  338.      * Deletion of element from the list
  339.  
  340.      */
  341.  
  342.     void double_llist::delete_element_first(int value)// تابع حذف العقدة الاولى
  343.  
  344.     {
  345.  
  346.         struct node *tmp, *q;
  347.  
  348.          /*first element deletion*/
  349.  
  350.         if (start->info == value)
  351.  
  352.         {
  353.  
  354.             tmp = start;
  355.  
  356.             start = start->next;
  357.  
  358.             start->prev = NULL;
  359.  
  360.             cout<<"Element Deleted"<<endl;
  361.  
  362.             free(tmp);
  363.  
  364.             return;
  365.  
  366.         }
  367.  
  368.         q = start;
  369.  
  370.         while (q->next->next != NULL)
  371.  
  372.         {
  373.  
  374.             /*Element deleted in between*/
  375.  
  376.             if (q->next->info == value)
  377.  
  378.             {
  379.  
  380.                 tmp = q->next;
  381.  
  382.                 q->next = tmp->next;
  383.  
  384.                 tmp->next->prev = q;
  385.  
  386.                 cout<<"Element Deleted"<<endl;
  387.  
  388.                 free(tmp);
  389.  
  390.                 return;
  391.  
  392.             }
  393.  
  394.             q = q->next;
  395.  
  396.         }
  397.  
  398.          /*last element deleted*/
  399.  
  400.         if (q->next->info == value)
  401.  
  402.         {
  403.  
  404.             tmp = q->next;
  405.  
  406.             free(tmp);
  407.  
  408.             q->next = NULL;
  409.  
  410.             cout<<"Element Deleted"<<endl;
  411.  
  412.             return;
  413.  
  414.         }
  415.  
  416.         cout<<"Element "<<value<<" not found"<<endl;
  417.  
  418.     }
  419.  
  420.  
  421.  
  422.     /*
  423.  
  424.      * Display elements of Doubly Link List
  425.  
  426.      */
  427.  
  428.     void double_llist::display_dlist()// تابع إظهار للعقدة كاملة
  429.  
  430.     {
  431.  
  432.         struct node *q;
  433.  
  434.         if (start == NULL)
  435.  
  436.         {
  437.  
  438.             cout<<"List empty,nothing to display"<<endl;
  439.  
  440.             return;
  441.  
  442.         }
  443.  
  444.         q = start;
  445.  
  446.         cout<<"The Doubly Link List is :"<<endl;
  447.  
  448.         while (q != NULL)
  449.  
  450.         {
  451.  
  452.             cout<<q->info<<" <-> ";
  453.  
  454.             q = q->next;
  455.  
  456.         }
  457.  
  458.         cout<<"NULL"<<endl;
  459.  
  460.     }
  461.     void double_llist::delete_element_last() // تابع حذف القيمة الاخيرة
  462.     {
  463.  
  464.         struct node *tmp, *q;
  465.  
  466.         tmp = start;
  467.         vector<node*> v;
  468.         while(tmp!=NULL)
  469.         {
  470.             v.push_back(tmp);
  471.             tmp=tmp->next;
  472.         }
  473.  
  474.         int N = v.size();
  475.         delete v[N-1];
  476.         v[N-1]=NULL;
  477.         if(N>1)v[N-2]->next = NULL;
  478.     }
  479.     int double_llist::search_element(int value)// تابع للبحث عن قيمة معينة
  480. {
  481.         struct node *q = start;
  482.  
  483.     int cnt = 0;
  484.  
  485.     while (q != NULL)
  486.     {
  487.         if(q->info==value)return cnt;
  488.         q = q->next;
  489.         cnt++;
  490.     }
  491.     return -1;
  492. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement