Don't like ads? PRO users don't see any ads ;-)
Guest

Something #2

By: a guest on May 5th, 2012  |  syntax: C++  |  size: 13.27 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // List.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "iostream"
  5.  
  6. using namespace std ;
  7.  
  8. typedef int Items ;
  9.  
  10. struct Node
  11. {
  12.         Items data ; // data
  13.         Node *next ; // pointer to next struct
  14.         Node *prev ; // pointer to previous struct
  15. } ;
  16.  
  17. class List
  18. {
  19. private:
  20.         Node *end ; // pointer to last struct
  21.         Node *start ; // pointer to first struct
  22. public:
  23.         List() ; // constructor
  24.         ~List() ; // destructor
  25.         int IsEmpty() ; // if it is empty - 1, else - 0
  26.         void Show() ; // show data
  27.         void Show_back() ; // show data from back
  28.         void Add_back(Items) ; // add data to back ( only with Add_back(void) )
  29.         void Add_back() ; // enter and add data to back
  30.         void Del_back() ; // delete object from back
  31.         void Add_front() ; // enter and add object to front
  32.         void Add_front(Items) ; // add object to front ( only with Add_front(void) )
  33.         void Del_front() ; // delete object from front
  34.         int Count() ; // return number of objects
  35.         void Set() ; // enter and set data in entering number
  36.         void Set(int) ; // set data in number ( only with Set(void))
  37.         void Del_number(int) ; // delete number
  38.         void Del_number() ; // enter number and delete them
  39.         void Del_data(Items) ; // delete data when it will be found ( only one time )
  40.         void Del_data() ; // enter and delete data, when it will be found ( only one time )
  41.         void Del_data_full() ; // enter and delete every object with entering data
  42.         void Add_sort() ; // enter and set data before bigger data
  43.         int Search_data(Items) ; // if data is found - 1 , else - 0
  44.         Node* Del_adr(Node*) ; // take struct, finding such object, remove them from list and return struct. It is 0, when there is no such struct
  45.         void Insert(Node*) ; // take struct and insert before biggest struct
  46.         int Sorted() ; // if data of list is sorted - 1, else - 0
  47.         void Sort() ; // it sort's objects in a list
  48. } ;
  49.  
  50. void main()
  51. {
  52.         List a ;
  53.  
  54.  
  55.         cin.get() ;
  56.         cin.get() ;
  57. }
  58.  
  59. List::List()
  60. {
  61.         start = 0 ; // inizialization
  62.         end = 0 ; // inizialization
  63. }
  64.  
  65. List::~List()
  66. {
  67.         if ( IsEmpty() ) return ; // if it is empty, we don't need to delete smth
  68.         Node *current = start ; // current is a kind of count, because it is a pointer
  69.         while ( current ) // while it isn't null
  70.         {
  71.                 current = start->next ; // move current to second object
  72.                 delete start ; // delete first
  73.                 start = current ; // and just move start-pointer
  74.         }
  75.         start = 0 ;
  76.         end = 0 ;
  77. }
  78.  
  79. int List::IsEmpty()
  80. {
  81.         if ( !start ) return 1 ; // if start is null, list is empty
  82.         return 0 ; // else
  83. }
  84.  
  85. void List::Show()
  86. {
  87.         if ( IsEmpty() ) // if it is empty
  88.         {
  89.                 cout << "This is empty..." << endl ;   
  90.                 return ;
  91.         }
  92.         Node *current = start ; // counter
  93.         while ( current )
  94.         {
  95.                 cout << "Data: " << current->data << endl ; // print every object in a list
  96.                 current = current->next ; // move to the next object
  97.         }
  98. }
  99.  
  100. void List::Show_back()
  101. {
  102.         if ( IsEmpty() ) return ; // if it is empty
  103.         Node *current = end ; // counter from back
  104.         while ( current )
  105.         {
  106.                 cout << "Data: " << current->data << endl ; // print every data
  107.                 current = current->prev ; // move to previous
  108.         }
  109. }
  110.  
  111. void List::Add_back(Items d)
  112. {
  113.         if ( IsEmpty() ) // if it is empty
  114.         {
  115.                 start = new Node ; // create start
  116.                 start->data = d ; // set data
  117.                 start->next = 0 ;
  118.                 start->prev = 0 ;
  119.                 end = start ; // end == start if list has one object
  120.                 return ;
  121.         }
  122.         Node *temp = new Node ; // create new struct
  123.         temp->data = d ; // set data
  124.         temp->next = 0 ;
  125.         temp->prev = end ; // previous is end object
  126.         end->next = temp ; // next for end is our temp
  127.         end = temp ; // move end to last member of a list
  128. }
  129.  
  130. void List::Add_back()
  131. {
  132.         Items *d = new Items ; // create data
  133.         cout << "Enter data: " ;
  134.         cin >> *d ;
  135.         Add_back(*d) ; // calling another method
  136. }
  137.  
  138. void List::Del_back()
  139. {
  140.         if ( IsEmpty() ) return ; // if it is empty
  141.         if ( start == end ) // if list has only one member
  142.         {
  143.                 delete start ;
  144.                 start = 0 ;
  145.                 end = 0 ;
  146.                 return ;
  147.         }
  148.         Node *current = start ; // counter
  149.         while ( current->next != end ) // while next object isn't end
  150.         {
  151.                 current = current->next ; // move
  152.         }
  153.         delete end ; // delete end
  154.         end = current ; // move it to previous member
  155.         end->next = 0 ; // inizialization of next for last member
  156.        
  157.         // another kind of deleting from back
  158.         /*
  159.         Node *current = end->prev ; // create pointer to previous member from end
  160.         delete end ; // delete end
  161.         end = current ; // move end to previous member (current)
  162.         end->next = 0 ; // inizialization of next for last member
  163.         */
  164. }
  165.  
  166. void List::Add_front(Items d)
  167. {
  168.         if ( IsEmpty() ) // if it is empty
  169.         {
  170.                 start = new Node ; // create first object
  171.                 start->data = d ; // set in data
  172.                 start->next = 0  ;
  173.                 start->prev = 0 ;
  174.                 end = start ; // if there is one object, start always equally end ( start == end )
  175.                 return ;
  176.         }
  177.         Node *temp = new Node ; // if start exist, create new object
  178.         temp->data = d ; // set in data
  179.         temp->next = start ; // bind it with start
  180.         start->prev = temp ; // start with this
  181.         start = temp ; // and just move start
  182. }
  183.  
  184. void List::Add_front()
  185. {
  186.         Items *d = new Items ; // create new data
  187.         cout << "Enter data: " ;
  188.         cin >> *d ;
  189.         Add_front(*d) ; // calling another method
  190. }
  191.  
  192. void List::Del_front()
  193. {
  194.         if ( IsEmpty() ) return ; // is it is empty
  195.        
  196.         if ( start == end ) // if there is one object
  197.         {
  198.                 delete start ;
  199.                 start = 0 ;
  200.                 end = 0 ;
  201.                 return ;
  202.         }
  203.        
  204.         Node *current = start->next ; // create new pointer to next object from start
  205.         delete start ; // delete it
  206.         start = current ; // and just move start
  207. }
  208.  
  209. int List::Count()
  210. {
  211.         if ( IsEmpty() ) return 0 ; // if it is empty
  212.         int k = 0 ; // counter of objects
  213.         Node *current = start ;
  214.         while ( current )
  215.         {
  216.                 k++ ;
  217.                 current = current->next ;
  218.         }
  219.         return k ;
  220. }
  221.  
  222. void List::Set()
  223. {
  224.         int *a = new int ; // create number
  225.         cout << "Enter position: " ;
  226.         cin >> *a ;
  227.         if ( ( *a < 1 ) || ( *a > Count()+1 ) ) // if it is 0 or less, if it more then ( number of objects + 1 ), because we can Add_back new object
  228.         {
  229.                 delete a ;
  230.                 cout << "Error position... " << endl ;
  231.                 return ;
  232.         }
  233.         if ( *a == 1 ) { Add_front() ; return ; } // if it is start - Add_front
  234.         if ( *a == Count()+1 ) { Add_back() ; return ; } // if it is end+1 - Add_back
  235.         Set( *a ) ; // calling another method
  236. }
  237.  
  238. void List::Set(int a)
  239. {
  240.         int k = 1 ; // it is number of object
  241.         Node *current = start ;
  242.         while ( current )
  243.         {
  244.                 current = current->next ;
  245.                 k++ ;
  246.                 if ( k == a ) // if it is thah number
  247.                 {
  248.                         Node *temp = new Node ; // create new data
  249.                         cout << "Enter data: " ;
  250.                         cin >> temp->data ;
  251.                         current->prev -> next = temp ; // previos with temp
  252.                         temp->prev = current->prev ; // temp with previous
  253.                         temp->next = current ; // temp with next
  254.                         current->prev = temp ; // next with temp
  255.                         return ;
  256.                 }
  257.         }
  258. }
  259.  
  260. void List::Del_number()
  261. {
  262.         int *a = new int ; // create number
  263.         cout << "Enter position: " ;
  264.         cin >> *a ;
  265.         if ( ( *a < 1 ) || ( *a > Count() ) ) // if it is 0 or less, if it is unexisting number
  266.         {
  267.                 delete a ;
  268.                 cout << "Error position... " << endl ;
  269.                 return ;
  270.         }
  271.         if ( *a == 1 ) { Del_front() ; return ; } // if is is start - Del_front
  272.         if ( *a == Count() ) { Del_back() ; return ; } // if it is end - Del_back
  273.         Del_number( *a ) ; // calling another method
  274. }
  275.  
  276. void List::Del_number(int a)
  277. {
  278.         int k = 1 ; // k is a number of object
  279.         Node *current = start ;
  280.         while ( current )
  281.         {
  282.                 current = current->next ;
  283.                 k++ ;
  284.                 if ( k == a )
  285.                 {
  286.                         Node *buf = current->next ;
  287.                         buf->prev = current->prev ;
  288.                         current->prev -> next = buf ;
  289.                         buf->next = current->next->next ;
  290.                         delete current ;
  291.                         return ;
  292.                 }
  293.         }
  294. }
  295.  
  296. void List::Del_data()
  297. {
  298.         if ( IsEmpty() ) // if it is empty
  299.         {
  300.                 cout << "This is empty... " << endl ;
  301.                 return ;
  302.         }
  303.         Items *a = new Items ; // create data
  304.         cout << "Enter data: " ;
  305.         cin >> *a ;
  306.         if ( start->data == *a ) { Del_front() ; return ; } // if it is in start - Del_front
  307.         if ( end->data == *a ) { Del_back() ; return ; } // if it is in the end - Del_back
  308.         Del_data(*a) ; // another method
  309. }
  310.  
  311. void List::Del_data(Items a)
  312. {
  313.         Node *current = start->next ; // because we don't need to check start: it is in another method
  314.         int k = 2 ; // because we begin's from second object (start is first)
  315.         while ( current ) // for start->next to end
  316.         {
  317.                 if ( current->data == a ) { Del_number(k) ; return ; } // we delete it for number, because it is light :)
  318.                 k++ ; // k is number of object
  319.                 current = current->next ;
  320.         }
  321.         cout << "There is no " << a << " ..." << endl ;
  322. }
  323.  
  324. void List::Del_data_full()
  325. {
  326.         if ( IsEmpty() ) // if it is empty
  327.         {
  328.                 cout << "This is empty... " << endl ;
  329.                 return ;
  330.         }
  331.         Items *a = new Items ;
  332.         cout << "Enter data: " ;
  333.         cin >> *a ;
  334.        
  335.         while ( start->data == *a && start->next ) Del_front() ; // while first data is true - delete
  336.         if ( start->next == 0 && start->data == *a ) // if than it is one object with true-data - delete
  337.         {
  338.                 delete start ;
  339.                 start = end = 0 ;
  340.                 return ;
  341.         }
  342.        
  343.         while ( end->data == *a ) Del_back() ;  // while last data is true - delete
  344.  
  345.         Node *current = start->next ;
  346.         while ( current )
  347.         {
  348.                 while ( current->data == *a ) // while data is true - delete
  349.                 {
  350.                         Node *buf = current->next ; // we need buf to delete only one object
  351.                         buf->prev = current->prev ;
  352.                         current->prev -> next = buf ;
  353.                         delete current ;
  354.                         current = buf ;
  355.                 }
  356.                 current = current->next ;
  357.         }
  358. }
  359.  
  360. void List::Add_sort()
  361. {
  362.         Node *temp = new Node ; // new node for data
  363.         cout << "Enter data: " ;
  364.         cin >> temp->data ;
  365.        
  366.         if ( temp->data <= start->data ) // if it is smallest than start - Add_front()
  367.         {
  368.                 Add_front(temp->data) ;
  369.                 delete temp ;
  370.                 return ;
  371.         }
  372.        
  373.         Node *current = start->next ;
  374.         while ( current->next ) // while current isn't end
  375.         {
  376.                 if ( temp->data <= current->data )
  377.                 {
  378.                         current->prev -> next = temp ; // previos with temp
  379.                         temp->prev = current->prev ; // temp with previous
  380.                         temp->next = current ; // temp with next
  381.                         current->prev = temp ; // next with temp
  382.                         return ;
  383.                 }
  384.                 current = current->next ;
  385.         }
  386.        
  387.         Add_back(temp->data) ; // if it is end
  388.         delete temp ;
  389. }
  390.  
  391. int List::Search_data(Items a)
  392. {
  393.         Node *current = start ;
  394.         while ( current ) // while this list has records
  395.         {
  396.                 if ( current->data == a ) return 1 ; // if it is it - return
  397.                 current = current->next ; // move, every time
  398.         }
  399.         return 0 ; // if there is no data - null
  400. }
  401.  
  402. Node* List::Del_adr(Node *temp)
  403. {
  404.         if ( start == temp ) // if it is start
  405.         {
  406.                 start = start->next ; // just move start
  407.                 return temp ; // and return this object
  408.         }
  409.  
  410.         if ( end == temp ) // if it is end
  411.         {
  412.                 end = end->prev ; // just move end
  413.                 return temp ; // and return object
  414.         }
  415.         // template of delete
  416.         Node *current = start->next ; // it isn't start, because start is higher in code ^
  417.         while ( current->next ) // while current isn't end, because end is higher in code too ^
  418.         {
  419.                         if ( current == temp ) // is it is that
  420.                         {      
  421.                                 current->prev -> next = current->next ; // we cut it
  422.                                 current->next -> prev = current->prev ; // from list
  423.                                 return temp ;
  424.                         }      
  425.                         current = current->next ;
  426.         }
  427.         return 0 ;
  428. }
  429.  
  430. void List::Insert(Node *temp)
  431. {
  432.         if ( temp->data <= start->data ) // if it can be the first
  433.         {
  434.                 temp->next = start ; // next with start
  435.                 temp->prev = 0 ; // previous is null
  436.                 start->prev = temp ; // previous for start is temp
  437.                 start = temp ; // and first object is temp-struct
  438.                 return ;
  439.         }
  440.  
  441.         if ( temp->data >= end->data ) // if it can be the last
  442.         {
  443.                 temp->prev = end ; // previous for temp is end
  444.                 end->next = temp ; // next for end is temp
  445.                 temp->next = 0 ; // temp has no next
  446.                 end = temp ; // and the last object is temp
  447.                 return ;
  448.         }
  449.        
  450.         // if it can be before the last:
  451.         if ( ( temp->data <= end->data ) && ( end->prev->data <= temp->data ) )
  452.         {
  453.                         end->prev -> next = temp ; // end has previous object; ist's next is temp
  454.                         temp->prev = end->prev ; // previous for temp is previous for end
  455.                         temp->next = end ; // next for temp is end
  456.                         end->prev = temp ; // previous for end is temp
  457.                         return ;
  458.         }
  459.         // template of inserting
  460.         Node *current = start->next ; // it isn't start, becouse start is higher in code ^
  461.         while ( current->next ) // while it isn't end, becouse end is higher too ^
  462.         {
  463.                 if ( temp->data < current->data ) // if it can be here - insert
  464.                 {
  465.                         current->prev -> next = temp ; // current has previous; it's next is temp
  466.                         temp->prev = current->prev ; // previous for temp is previous for current
  467.                         temp->next = current ; // next for temp is current
  468.                         current->prev = temp ; // previous for current is temp
  469.                         return ;
  470.                 }
  471.                 current = current->next ;
  472.         }
  473. }
  474.  
  475. int List::Sorted()
  476. {
  477.         Node *current = start ;
  478.         while ( current->next ) // while it isn't end
  479.         {
  480.                 if ( current->data > current->next->data ) return 0 ; // if it isn't right
  481.                 current = current->next ;
  482.         }
  483.         return 1 ; // if it is right - sorted
  484. }
  485.  
  486. void List::Sort()
  487. {
  488.         Node *current = start ;
  489.         while ( !Sorted() ) // do while it isn't sorted
  490.         {
  491.                 if ( current->data > current->next->data )
  492.                 {      
  493.                         Node *buf = current ; // create a buf, because curent will be deleted
  494.                         current = current->next ;
  495.                         if ( current->next == 0 ) current = start ; // if current is end, it must be start
  496.                         Insert( Del_adr(buf) ) ;
  497.                         continue ; // because we don't need to change current pointer          
  498.                 }
  499.                 current = current->next ;
  500.                 if ( current->next == 0 ) current = start ;
  501.         }
  502. }