dmkozyrev

list

Oct 29th, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <iostream>;
  2.  
  3. using namespace std;
  4.  
  5. class List {
  6.         struct Node{
  7.             int date_;
  8.             Node* next_;
  9.         };
  10.         Node* head;
  11.     public:
  12.         List();
  13.         void add(int value);
  14.         int size() const;
  15.         int get(int index);
  16.         void del(int index);
  17.         void disp();
  18.         ~List();
  19. };
  20.  
  21. List::List(){
  22.     head = 0;
  23. }
  24.  
  25. List::~List(){
  26.     Node* it = head;
  27.     while ( head != 0 ){
  28.         it = head -> next_;
  29.         delete head;
  30.         head = it;
  31.     }
  32. }
  33.  
  34. void List::add(int value){
  35.     Node* n = new Node;
  36.     n -> next_ = 0;
  37.     n -> date_ = value;
  38.    
  39.     if ( head == 0 ) {
  40.         head = n;
  41.     } else {
  42.         Node* it = head;
  43.        
  44.         while (it -> next_ != 0)
  45.             it = it -> next_;
  46.         it -> next_ = n;
  47.     }
  48. }
  49.  
  50. void List::del(int index){
  51.     Node* it = head;
  52.     if ( index == 0 ){
  53.         head = head -> next_;
  54.         delete it;
  55.     } else {
  56.         Node* n = it;
  57.         int i = 0;
  58.         while ( (i < index-2 ) && ( it -> next_ != 0) ){
  59.             it = it -> next_;
  60.             ++i;
  61.         }
  62.         n = it -> next_;
  63.         it -> next_ = n -> next_;
  64.         delete n;
  65.     }
  66. }
  67.  
  68. int List::size() const{
  69.     Node* it = head;
  70.     int i = 0;
  71.     while ( it != 0 ){
  72.         it = it -> next_;
  73.         ++i;
  74.     }
  75.     return i;
  76. }
  77.  
  78. int List::get(int index){
  79.     Node* it = head;
  80.     int i = 0;
  81.     while ( ( i < index ) && ( it -> next_ != 0 ) ){
  82.         it = it -> next_;
  83.         ++i;
  84.     }
  85.     return it -> date_;
  86. }
  87.  
  88. void List::disp(){
  89.     Node* it = head;
  90.     int i = 0;
  91.     while ( it != 0 ) {
  92.         cout << it -> date_ << " ";
  93.         ++i;
  94.         it = it -> next_;
  95.     }
  96.     cout << "size = " << i << endl;
  97. }
  98.  
  99. int main (){
  100.     List l;
  101.     for (int i = 1; i < 10000; i++){
  102.         l.add(i);
  103.         std::cout << i << " ";
  104.     }
  105. //  l.disp();
  106. //  l.del(0);
  107. //  l.disp();
  108.     int n;
  109.     std::cin >> n;
  110.     l.~List();
  111. }
Add Comment
Please, Sign In to add comment