Advertisement
ntr31954

list.cc

Mar 22nd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include "list.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. ListPtr listCreate(){
  6.     ListPtr AuxPtr = nullptr;
  7.     AuxPtr = new List;
  8.     listInit(AuxPtr);
  9.     return AuxPtr;
  10. }
  11.  
  12. static std::string listToString (ListPtr l, char c) {
  13.   std::string s;
  14.  
  15.   for (ListNodePtr n = l->head; n != nullptr; n = n->next)
  16.     s += c + listNodeToString (n);
  17.  
  18.   return s;
  19. }
  20. ListNodePtr listNodeCreate(Element e){
  21.     ListNodePtr AuxPtr = nullptr;
  22.     AuxPtr = new ListNode;
  23.     AuxPtr->key = e;
  24.     return AuxPtr;
  25. }
  26.  
  27. void listInit(ListPtr l){
  28.     l->head = nullptr;
  29. }
  30.  
  31. bool listInsert(ListPtr l, Element x, Position i){
  32.  
  33.     Position counter = 1;
  34.     if(i<1)
  35.         return false;
  36.  
  37.     ListNodePtr AuxPtr2 = l->head, AuxPtr = nullptr;
  38.  
  39.     if(counter == i){
  40.         AuxPtr = listNodeCreate(x);
  41.         AuxPtr->next = l->head;
  42.         l->head = AuxPtr;
  43.         return true;
  44.     }
  45.     while(AuxPtr2 != nullptr){
  46.         counter++;
  47.         if(counter==i){
  48.             AuxPtr = listNodeCreate(x);
  49.             AuxPtr->next = AuxPtr2->next;
  50.             AuxPtr2->next = AuxPtr;
  51.             return true;
  52.         }
  53.         if(AuxPtr2->next == nullptr && counter == i-1){
  54.             AuxPtr = listNodeCreate(x);
  55.             AuxPtr = AuxPtr2->next;
  56.             return true;
  57.         }
  58.         AuxPtr2 = AuxPtr2->next;
  59.     }
  60.     return false;
  61. }
  62.  
  63. Position listLocate(ListPtr l, Element x){
  64.     ListNodePtr AuxPtr2 = l->head;
  65.     Position counter = 1;
  66.     while(AuxPtr2 != nullptr){
  67.         if(AuxPtr2->key == x)
  68.             return counter;
  69.         else{
  70.             AuxPtr2 = AuxPtr2->next;
  71.             counter++;
  72.         }
  73.     }
  74.     return NOPOS;
  75. }
  76.  
  77. ListNodePtr listRetrieve(ListPtr l, Position i){
  78.     ListNodePtr AuxPtr2 = l->head;
  79.     Position counter = 1;
  80.     while(AuxPtr2 != nullptr){
  81.         if(counter == i){
  82.             cout<<AuxPtr2->key<<endl;
  83.             return AuxPtr2;
  84.         }else{
  85.             AuxPtr2 = AuxPtr2->next;
  86.             counter++;
  87.         }
  88.     }
  89.     return nullptr;
  90. }
  91.  
  92. bool listRemove(ListPtr l, Position p){
  93.  
  94.     if(p < 1)
  95.         return false;
  96.  
  97.     ListNodePtr AuxPtr21 = l->head;
  98.     Position counter = 1;
  99.  
  100.     if(counter == p){
  101.         l->head = l->head->next;
  102.         return true;
  103.     }
  104.     while(AuxPtr21->next != nullptr){
  105.         if(counter==p){
  106.             AuxPtr21->next = AuxPtr21->next->next;
  107.             delete AuxPtr21;
  108.             return true;
  109.         }
  110.         if(counter > 1)
  111.             AuxPtr21 = AuxPtr21->next;
  112.  
  113.         counter++;
  114.     }
  115.     return false;
  116. }
  117.  
  118. ulong listSize(ListPtr l){
  119.     ListNodePtr AuxPtr2 = l->head;
  120.     Position counter = 1;
  121.     if(AuxPtr2==nullptr)
  122.         return counter = 0;
  123.     while(AuxPtr2->next != nullptr){
  124.         AuxPtr2 = AuxPtr2->next;
  125.         counter++;
  126.     }
  127.     return counter;
  128. }
  129.  
  130. ListNodePtr listFirst(ListPtr l){
  131.     ListNodePtr AuxPtr2 = l->head;
  132.     return AuxPtr2;
  133. }
  134.  
  135. void listMakeNull(ListPtr l){
  136.     ulong max = listSize(l);
  137.     for(Position i = max; i >= 1; i--){
  138.         listRemove(l,i);
  139.     }
  140.     listInit(l);
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement