Click here to Skip to main content
14,699,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am writing a code for generic list in c++ : in this lise i have a geniric node (which mean the data in it could be any type ) , and a class iterator which the iterator that it's job is to point to a specefic node in the list , also the iterator could point to any other list so in it's class i added a pointer to the current list .
my problem is with the insert function, where i insert a new node to the list,
and in this function if the iterator poiints to the end of the list the we add the new node to the end of the list else we insert the node one place before the node that the iterator currently points to , and if the iteratot points to a different node then thats an error ..

my problem is that the function inserts only the first node .. what could be the problem? i can't seem to figure it out :\

What I have tried:

#include <iostream>
#include <assert.h>

class List {

public:
    List();
    List(const List&);
    ~List();
    List<T>& operator=(const List& list);
    template <class E>
    class ListNode {
        private:
             E data;
             ListNode(const E& t, ListNode<E> *next):data(t), next(next){}
             ~ListNode(){delete next;}
             ListNode<E> *next;
        public:
            friend class List<E>;  //??
            friend class Iterator;   // ??
            E getData() const{
               return this->data;
            }
            ListNode<E>* getNext() const{
               return this->next;
            }
            };
    class Iterator {
        List<T>* list;
        ListNode<T>* current;
        Iterator( List<T>* list=NULL, ListNode<T>* current=NULL);
        friend class List<T>;
        friend class ListNode<T>;
    public:
     ///// functions for iterator
    };
    Iterator begin() ;
    Iterator end() ;
    void insert(const T& data, Iterator iterator=0); // what is wrong with you //!! just insert two more elements i want to sleep
    void remove(Iterator iterator);
    class Predicate{
    private:
      T target;
    public:
      Predicate(T i) : target(i) {}
      bool operator()(const T& i) const {
        return i == target;
      }
    };
    Iterator find(const Predicate& predicate);
    class Compare{
    private:
      T target;
    public:
      Compare(T i) : target(i) {}
      bool operator()(const T& i) const {
        return i < target;
      }
    };
    void sort(const Compare& compare);
    bool operator==(const List& list);
    bool operator!=(const List& list);
    int getSize() const;
    bool empty() const;

    int compareLinkedList(ListNode<T> *node1,  ListNode<T> *node2);

private:
   ListNode<T> *head;
   ListNode<T> *tail;
int size;
};



// my lovely insert function (not really :'((((  )
template <typename T>
void List<T>::insert(const T& data, Iterator iterator){
    if(iterator.list!=this){
        throw mtm::ListExceptions::ElementNotFound();
        return;
    }
    ListNode<T> *newNode = new ListNode<T>(data, NULL);
    if(iterator.current==this->end().current){
        newNode->next=this->end().current;
        Iterator temp=end();
        temp--;
        temp.current->next=newNode;
           }
    else {
    if (head == NULL) {
    head = newNode;
    }
   else {
       Iterator temp1=iterator;
        temp1--;
        temp1.current->next=newNode;
        temp1++;
        newNode->next=temp1.current;

   }

   }
    size++;
}



and how i tested the list :


static void listExample(){
    List<int> list;
    list.insert(1, list.end());
    list.insert(2, list.end());
    list.insert(3, list.end());

    int counter = 1;
  for (List<int>::Iterator it = list.begin(); it != list.end(); ++it) {   // ????
    ASSERT_EQUALS(counter++, *it);
  }
Posted
Updated 3-Jul-17 0:40am
v2
Comments
Learnignc 2-Jul-17 17:17pm
   
helppppp plzzzzzzz anyoneeeeeeeeeeeeeeeeee

1 solution

If you want to insert an item in the middle of the list, you must decide where, normally an "insert at position".

For that
1. you enumarate to the inserted position,
2. add the new entry as follower of the item before
3. set the old item as follower of the new item

Some learning stuff: fundamental theory with pictures about lists in C.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900