Inserting a value into a linked list using iterator position

Im trying to insert a new element into a Linked list but instead of inserting the value my program replaces the value with the new value in that location for example my code output would be

H,e,l,l,o
H,!,l,l,o

but I want it to be

H,e,l,l,o
H,e,!,l,l,o

how can I fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <initializer_list>
#include <iostream>

template <typename T>
class List {
  struct Node;  
  class It;
 public:
  List(std::initializer_list<T> ele) {
    this->operator=(ele);  
  }

  void InsertB(T ele) {
    Node* node = new Node{ele};
    if (this->size == 0) {
      this->start = this->fin = node;
    } else {
      this->fin->next = node;
      node->prev = this->fin;
      this->fin = node;
    }
    ++this->size;
  }

  void InsertF(T ele) {
    Node* node = new Node{ele};
    if (this->size == 0) {
      this->start = this->fin = node;
    } else {
      node->next = this->start;
      this->start->prev = node;
      this->start = node;
    }
    ++this->size;
  }

  List& operator=(std::initializer_list<T> ele) {
    this->size = 0;
    for (auto&& val : ele) this->InsertB(val);
    return *this;
  }

  friend std::ostream& operator<<(std::ostream& out, const List& list) {
    for (auto val = list.start; val; val = val->next) {
      out << val->data;
      if (val->next) out << ",";
    }
    return out;
  }

  template <class It>
  List(It begin, It end) {
    for (; begin != end; ++begin) this->InsertB(*begin);
  }

  
  It begin() {
    return It(start);
  }
  
  It end() {
    return It(NULL);
  }

  It InsertIt(It pos, const T& ele) {
   if(pos == begin()) {
     InsertF(ele);
     return begin();
   } else if(pos == end()) {
     InsertB(ele);
     return fin;
   } else {
     Node* temp = new Node{ele, pos.cur->next, pos.cur};

     pos.cur->prev->next = temp;
     pos.cur->prev = temp;

     ++size;
     return temp;
   }
  }

private:
  struct Node {
    T data;
    Node* next = nullptr;
    Node* prev = nullptr;

  };

  class It {
    friend class List;
    public:
    It& operator=(const It& that) {
      List<char> first(that.begin(), that.end());
    }

    It& operator++() {
      this->cur = cur->next;
      return *this;
    }

    It operator++(int) {
      It temp(*this);
      this->operator++();
      return temp;
    }

    T const *operator->() const { return &(**this); }

    T *operator->() { return &(**this); }

    T& operator*() const {return this->cur->data;}
    
    bool operator!=(It val) const { 
       return !(this->operator==(val));
    }

    bool operator==(It val) const { 
       return this->cur == val.cur;
    }

     private:

     Node* cur;
     It (Node *ptr) : cur(ptr) {}
  };

  Node* start = nullptr;
  Node* fin = nullptr;
  std::size_t size = 0;
};

int main() {
List<char> test = {'H', 'e', 'l', 'l', 'o'};
std::cout << test << std::endl;
List<char> test2 (test.begin(), test.end());
auto iter = test2.begin();
iter++;
test2.InsertIt(iter, '!');
std::cout << test2 << std::endl;
}
Last edited on
> I want it to be H,e,!,l,l,o

Since the iterator is 'pointing' to the second element 'e', it should be H,!,e,l,l,o
(Insert the new element at the position specified by the iterator)

1
2
3
4
5
6
7
8
9
10
} else {
    // Node* temp = new Node{ele, pos.cur->next, pos.cur}; // *** line 73  
    Node* temp = new Node{ ele /*data*/ , pos.cur/*next*/, pos.cur->prev/*prev*/ };

    pos.cur->prev->next = temp;
    pos.cur->prev = temp;

    ++size;
    return temp;
}
Insert appears to work correctly when I try it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main() {
    List<char> test = {'H', 'e', 'l', 'l', 'o'};
    std::cout << test << std::endl;
    List<char> test2 (test.begin(), test.end());
    auto iter = test2.begin(); // H,e,l,l,o
    iter++;
    test2.InsertIt(iter, '!');
    std::cout << test2 << std::endl; // H,!,e,l,l,o

    // **** added now **** //////////////////

    auto inserted = test2.InsertIt(++iter, '*');
    std::cout << test2 << std::endl; // H,!,e,*,l,l,o
    std::cout << *inserted << std::endl; // *

    test2.InsertIt( test2.begin(), '+');
    std::cout << test2 << std::endl; // +,H,!,e,*,l,l,o

    int n = 0 ;
    for( auto it = test2.begin() ; it != test2.end() ; ++it )
        it = test2.InsertIt( ++it, char( n++ + '0' ) );
    std::cout << test2 << std::endl; // +,0,H,1,!,2,e,3,*,4,l,5,l,6,o,7

    ////////////////////////////////////////////////////////////
}

http://coliru.stacked-crooked.com/a/3ed4e7905e96d9c4
Topic archived. No new replies allowed.