Hi.

I'm learning by myself data structures with c++ but i got stuck

Here's my code:

#include<iostream>
#include<cstring>
#include<string>
#include<sstream>

using namespace std;

template<class T>

// node class
class nodeType
{
public:
  nodeType();
  nodeType(T);
  ~nodeType();

  nodeType *next;
  nodeType *previous;
  T data;
  //string getkey();
  string Id;

  //void borra_todo();
  void imprime();

  // getkey
  string getkey()
  {
    stringstream llave;
    llave << data.getkey();
    llave >> Id;
    return Id;
  }
};

// by defect
template<typename T>
nodeType<T>::nodeType()
{
  data = NULL;
  next = NULL;
  previous = NULL;
}

// by parameter
template<typename T>
nodeType<T>::nodeType(T data_)
{
  data = data_;
  next = NULL;
  previous = NULL;
}

// borra todo los nodos ...
// template<typename T>
// void node<T>::borra_todo()
// {
//   if (next) 
//     next->borra_todo();
//   
//   delete this;
// }


// node printing
template<typename T>
void nodeType<T>::imprime()
{
  cout << data << endl;
  // data.Print();
}

template<typename T>
nodeType<T>::~nodeType() 
{
  delete next;
  delete previous;
}

// list

template <class T>
class list
{
private:
  nodeType<T> *first;
  nodeType<T> *last;
  nodeType<T> *current;
  int m_num_nodes;

public:
  list();
  ~list();

  void add_head(T);
  void add_end(T);
  void add_sort(T);
  //void fill();
  void search(T);
  void del_by_data(T);
  void print();
};

template<typename T>
list<T>::list()
{
  m_num_nodes = 0;
  first = NULL;
  last = NULL;
}

//add in the beginning ... 
template<typename T>
void list<T>::add_head(T data_)
{
  nodeType<T> *new_node = new nodeType<T>(data_);
  nodeType<T> *temp = first;

  if (!first)
  {
    first = new_node;
  }
  else
  {
    new_node->next = first;
    first = new_node;

    while (temp)
    {
      temp = temp->next;
    }
  }
  m_num_nodes++;
}

// add to the last
template<typename T>
void list<T>::add_end(T data_)
{
  nodeType<T> *new_node = new nodeType<T> (data_);
  nodeType<T> *temp = first;

  if (!first)
  {
    first = new_node;
  }
  else
  {
    while (temp->next != NULL)
    {
      temp = temp->next;
    }
    temp->next = new_node;
  }
  m_num_nodes++;
}

// it is supposed that sorts items in the list ...
template<typename T>
void list<T>::add_sort(T data_)
{
  nodeType<T> *new_node = new nodeType<T> (data_);
  nodeType<T> *temp = first;

  if (!first)
  {
    first = new_node;
    last = new_node;
  }
  else
  {
    if (first->getkey() > new_node->getkey())
    {

    new_node->next = first;
    first = new_node;
    }
    else
    {
    while ((temp->next != NULL) && (new_node->getkey() > temp->next->getkey()))
    {
      temp = temp->next;
    }
    // .. ¿? ..
    new_node->next = temp->next;
    temp->next = new_node;
    temp->previous = new_node->previous;
    new_node->previous = temp;
    temp->next = new_node;
    }
  }
  m_num_nodes++;
 }


// ... llena elementos en la lista ...
//template<typename T>
//void list<T>::fill(char rfc[30], char nombre[30])
// void list<T>::fill()
// {
//   //char temprfc[30];
//   //char tempnombre[30];
//   string temprfc;
//   string tempnombre;
//   
//   temprfc = "DUDE010101R0";
//   tempnombre = "Dude";
//   //for (int i = 0;i <= 1;i++)
//   add_sort(temprfc, tempnombre);
//   temprfc = "AUDE010101R1";
//   tempnombre = "Commander";
//   add_sort(temprfc, tempnombre);
// }

// print list
template<typename T>
void list<T>::print()
{
  nodeType<T> *current;
  current = first;

  while (current != NULL)
  {
    current->imprime();
    current = current->next;
  }
}

// search the list
template<typename T>
void list<T>::search(T data_)
{
  nodeType<T> *temp = first;
  int cont=1;
  int cont2=0;

  while(temp)
  {
    if(temp->data,data_)
    {
      cout<<"Element found " << temp->data;
      cout << " in position: " << cont << endl;
      cont2++;
    }
    temp=temp->next;
    cont++;
  }
  if(cont2==0)
  {
    cout << "Element not found"<<endl;
  }
}


// ... delete by data ... ¿?
template<typename T>
void list<T>::del_by_data(T data_)
{
  nodeType<T> *temp = first;
  nodeType<T> *temp1 =first->next;

  int cont = 0;
  if (first->data ==  data_)
  {
    first = temp->next;
  }
  else
  {
    while (temp1)
    {
      if (temp1->data ==  data_)
      {
    nodeType<T> *aux_node = temp1;
    temp->next = temp1->next;
    delete aux_node;
    cont++;
    m_num_nodes--;
      }
      temp = temp->next;
      temp1 = temp1->next;
    }
  }
  if (cont == 0)
  {
    cout << "No data" << endl;
  }
}

// destroy the constructor
template<typename T>
list<T>::~list() {}

class Empleado
{
private:
  string RFC;
  string Nombre;

public:
  Empleado();
  Empleado(string r,  string nom);
  ~Empleado();

  void imprime();

  string getkey()
  {
    return RFC;
  }
};

Empleado::Empleado()
{
  RFC = "";
  Nombre = "";
}

Empleado::Empleado(string r, string nom)
{
  RFC = r;
  Nombre = nom;
}

void Empleado::imprime()
{
  cout << "RFC: " << RFC << endl;
  cout << "Nombre: " << Nombre << endl;
}



// -------
int main()
{
  list<Empleado> * list1 = new list<Empleado>();

  string element1;
  string element2;

  int dim, choice, pos;

  do{
    cout << "Select a choice.\n";
    cout << "1. Print list\n";
    cout << "2. Delete an element of the list\n";
    cout << "3. Search an element of the list\n";
    cout << "4. Exit\n";
    cin >> choice;
    switch(choice)
    {
      case 1:
      {
    cout << "Printing list:\n";
    //list1->fill();


    list1->add_sort(Empleado("DUDE010101R0", "Juan Peres"));
    list1->add_sort(Empleado("AUDE010101R1", "Ana Banana"));
    list1->print();
      }

      case 2:
      {
    cout << "Element to delete: DUDE010101R0";
    //cin >> element1;
    element1 = "DUDE010101R0";
    //list1->search(element1);
    list1->search("DUDE010101R0");
    element1 = "";
    break;
      }

      case 3:
      {
    cout << "Element to search: AUDE010101R1";
    //cin >> element1;
    //list1->search(&element1);
    list1->search("AUDE010101R1");
    element1 = "";
    break;
      }
    }
  }while(choice != 4);



  return 0;
}

I got the errors:
t3b.cpp: In function ‘int main()’:
t3b.cpp:369:30: error: no matching function for call to ‘list<Empleado>::search(const char [13])’
list1->search("DUDE010101R0");

t3b.cpp:369:30: note: candidate is:
t3b.cpp:232:6: note: void list<T>::search(T) [with T = Empleado]
void list<T>::search(T data_)

Any idea how to fix this? or what i'm doing wrong?
Thanks in advance

The search function is expecting an object of type Empleado not a string.

list1->search(Empleado("DUDE010101R0", "Juan Peres"));
list1->search(Empleado("AUDE010101R1", "Ana Banana"));

Clearing this error brings you to:

 if(temp->data,data_)
{
cout<<"Element found " << temp->data;

The if statement is illegal and cout doesn't know how to display temp->data

Edited 2 Years Ago by tinstaafl

Thanks for your reply ..

I fixed it, but now i'm stuck with the delete function :(

Here is my code:

#include<iostream>
#include<cstring>
#include<string>
#include<sstream>

using namespace std;

template<class T>

// node class
class nodeType
{
public:
  nodeType();
  nodeType(T);
  ~nodeType();

  nodeType *next;
  nodeType *previous;
  T data;
  //string getkey();
  string Id;

  //void borra_todo();
  void imprime();

  // getkey
  string getkey()
  {
    stringstream llave;
    llave << data.getkey();
    llave >> Id;
    return Id;
  }
};

// by defect
template<typename T>
nodeType<T>::nodeType()
{
  data = NULL;
  next = NULL;
  previous = NULL;
}

// by parameter
template<typename T>
nodeType<T>::nodeType(T data_)
{
  data = data_;
  next = NULL;
  previous = NULL;
}


// node printing
template<typename T>
void nodeType<T>::imprime()
{
  data.imprime();
}

template<typename T>
nodeType<T>::~nodeType() 
{
  delete next;
  delete previous;
}

// list

template <class T>
class list
{
private:
  nodeType<T> *first;
  nodeType<T> *last;
  nodeType<T> *current;
  int m_num_nodes;

public:
  list();
  ~list();

  void add_head(T);
  void add_end(T);
  void add_sort(T);
  void search(string r);
  void del_by_data(int n);
  void print();
};

template<typename T>
list<T>::list()
{
  m_num_nodes = 0;
  first = NULL;
  last = NULL;
}

//add in the beginning ... 
template<typename T>
void list<T>::add_head(T data_)
{
  nodeType<T> *new_node = new nodeType<T>(data_);
  nodeType<T> *temp = first;

  if (!first)
  {
    first = new_node;
  }
  else
  {
    new_node->next = first;
    first = new_node;

    while (temp)
    {
      temp = temp->next;
    }
  }
  m_num_nodes++;
}

// add to the last
template<typename T>
void list<T>::add_end(T data_)
{
  nodeType<T> *new_node = new nodeType<T> (data_);
  nodeType<T> *temp = first;

  if (!first)
  {
    first = new_node;
  }
  else
  {
    while (temp->next != NULL)
    {
      temp = temp->next;
    }
    temp->next = new_node;
  }
  m_num_nodes++;
}

// it is supposed that sorts items in the list ...
template<typename T>
void list<T>::add_sort(T data_)
{
  nodeType<T> *new_node = new nodeType<T> (data_);
  nodeType<T> *temp = first;

  if (!first)
  {
    first = new_node;
    last = new_node;
  }
  else
  {
    if (first->getkey() > new_node->getkey())
    {

    new_node->next = first;
    first = new_node;
    }
    else
    {
    while ((temp->next != NULL) && (new_node->getkey() > temp->next->getkey()))
    {
      temp = temp->next;
    }
    // .. ¿? ..
    new_node->next = temp->next;
    temp->next = new_node;
    temp->previous = new_node->previous;
    new_node->previous = temp;
    temp->next = new_node;
    }
  }
  m_num_nodes++;
 }

// print list
template<typename T>
void list<T>::print()
{
  nodeType<T> *current;
  current = first;

  while (current != NULL)
  {
    current->imprime();
    current = current->next;
  }
}

// ............................................
template<typename T>
void list<T>::search(string r)
{
  bool found = false;
  current = first;

  while (current != NULL && !found)
  {
    if (current->getkey() == r)
    {
      found = true;
      current->imprime();
    }
    else
    {
      current = current->next;
    }
  }
  //return found;
}
// --------------------------------------------

template<typename T>
void list<T>::del_by_data(int n)
{
  nodeType<T> *temp = first;
  //nodeType<T> *temp1 = first->next;

  int cont = 1;
  while (cont != n)
  {
    temp = temp->next;
    cont++;
    m_num_nodes--;
  }
  //m_num_nodes--;
  if (cont == 1)
  {
    cout << "No data" << endl;
    first = temp->next;
    temp->next->previous = NULL;
  }
  else
  {
    //last = temp->next;
    //temp->previous->next = NULL;
  }
}


// destroy the constructor
template<typename T>
list<T>::~list() {}


class Empleado
{
private:
  string RFC;
  string Nombre;

public:
  Empleado();
  Empleado(string r,  string nom);
  ~Empleado();

  void imprime();

  string getkey()
  {
    return RFC;
  }
};

Empleado::Empleado()
{
  RFC = "";
  Nombre = "";
}

Empleado::Empleado(string r, string nom)
{
  RFC = r;
  Nombre = nom;
}

void Empleado::imprime()
{
  cout << "RFC: " << RFC << endl;
  cout << "Nombre: " << Nombre << endl;
}

Empleado::~Empleado() {}

// -------
int main()
{
  list<Empleado> * list1 = new list<Empleado>();

  string element1;
  string element2;

  int dim, choice, pos;

  do{
      cout << "Select a choice.\n";
      cout << "1. Print list\n";
      cout << "2. Delete an element of the list\n";
      cout << "3. Search an element of the list\n";
      cout << "4. Exit\n";
      cout << "Option: ";
      cin >> choice;
      cout << endl;

      switch(choice)
      {
        case 1:
        {
          cout << "Printing list:\n";
          list1->add_sort(Empleado("DUDE010101R0", "Juan Peres"));
          list1->add_sort(Empleado("AUDE010101R1", "Ana Banana"));
          list1->print();
          break;
        }      
        case 2:
        {
          cout << "Element to delete: DUDE010101R0\n";
          list1->del_by_data(2);
          element1 = "";
          break;
        }

        case 3:
        {
          cout << "Element to search: DUDE010101R0\n";
          list1->search("DUDE010101R0");
          element1 = "";
          break;
        }
    }
  }while(choice != 4);

  return 0;
}

Any suggestions or ideas on what i'm doing wrong with the delete function? o.O (?)

Thanks in advance.

Edited 2 Years Ago by bernardo.mclobo: code snippets

Hi, it's me again...

I have solved my problem, thanks for everything, you helped me to think (actually my head hurts @_@), but i solved my problem. Thanks for make me think...

Here's my code, hope it helps someone out there ..

#include<iostream>
#include<cstring>
#include<string>
#include<sstream>

using namespace std;

template<class T>

// node class
class nodeType
{
public:
  nodeType();
  nodeType(T);
  ~nodeType();

  nodeType *next;
  nodeType *previous;
  T data;
  string Id;

  void imprime();

  // getkey
  string getkey()
  {
    stringstream llave;
    llave << data.getkey();
    llave >> Id;
    return Id;
  }
};

// by defect
template<typename T>
nodeType<T>::nodeType()
{
  data = NULL;
  next = NULL;
  previous = NULL;
}

// by parameter
template<typename T>
nodeType<T>::nodeType(T data_)
{
  data = data_;
  next = NULL;
  previous = NULL;
}


// node printing
template<typename T>
void nodeType<T>::imprime()
{
  data.imprime();
}

template<typename T>
nodeType<T>::~nodeType() 
{
  delete next;
  delete previous;
}

// list
template <class T>
class list
{
private:
  nodeType<T> *first;
  nodeType<T> *last;
  nodeType<T> *current;
  int m_num_nodes;

public:
  list();
  ~list();

  void add_head(T);
  void add_end(T);
  void add_sort(T);
  void search(string r);
  void del_by_data(int n);
  void print();
};

template<typename T>
list<T>::list()
{
  m_num_nodes = 0;
  first = NULL;
  last = NULL;
}

//add in the beginning ... 
template<typename T>
void list<T>::add_head(T data_)
{
  nodeType<T> *new_node = new nodeType<T>(data_);
  nodeType<T> *temp = first;

  if (!first)
  {
    first = new_node;
  }
  else
  {
    new_node->next = first;
    first = new_node;

    while (temp)
    {
      temp = temp->next;
    }
  }
  m_num_nodes++;
}

// add to the last
template<typename T>
void list<T>::add_end(T data_)
{
  nodeType<T> *new_node = new nodeType<T> (data_);
  nodeType<T> *temp = first;

  if (!first)
  {
    first = new_node;
  }
  else
  {
    while (temp->next != NULL)
    {
      temp = temp->next;
    }
    temp->next = new_node;
  }
  m_num_nodes++;
}

// it is supposed that sorts items in the list ...
template<typename T>
void list<T>::add_sort(T data_)
{
  nodeType<T> *new_node = new nodeType<T> (data_);
  nodeType<T> *temp = first;

  if (!first)
  {
    first = new_node;
    last = new_node;
  }
  else
  {
    if (first->getkey() > new_node->getkey())
    {

    new_node->next = first;
    first = new_node;
    }
    else
    {
    while ((temp->next != NULL) && (new_node->getkey() > temp->next->getkey()))
    {
      temp = temp->next;
    }
    // .. ¿? ..
    new_node->next = temp->next;
    temp->next = new_node;
    temp->previous = new_node->previous;
    new_node->previous = temp;
    temp->next = new_node;
    }
  }
  m_num_nodes++;
 }

// print list
template<typename T>
void list<T>::print()
{
  nodeType<T> *current;
  current = first;

  while (current != NULL)
  {
    current->imprime();
    current = current->next;
  }
}

// ............................................
template<typename T>
void list<T>::search(string r)
{
  bool found = false;
  current = first;

  while (current != NULL && !found)
  {
    if (current->getkey() == r)
    {
      found = true;
      current->imprime();
    }
    else
    {
      current = current->next;
    }
  }
  //return found;
}
// --------------------------------------------

template<typename T>
void list<T>::del_by_data(int n)
{
  if (n < 0 || n > m_num_nodes)
  {
    cout << "Error" << endl;
  }
  m_num_nodes--;
  nodeType<T> *temp = first;
  if (n == 0)
  {
    temp = first;
    first = first->next;
    delete temp;
  }
  else
  {
    nodeType<T> *temp = first;
    temp = first->next;
    first->next = first->next->next;
  }
}

// destroy the constructor
template<typename T>
list<T>::~list() {}

class Empleado
{
private:
  string RFC;
  string Nombre;

public:
  Empleado();
  Empleado(string r,  string nom);
  ~Empleado();

  void imprime();

  string getkey()
  {
    return RFC;
  }
};

Empleado::Empleado()
{
  RFC = "";
  Nombre = "";
}

Empleado::Empleado(string r, string nom)
{
  RFC = r;
  Nombre = nom;
}

void Empleado::imprime()
{
  cout << "RFC: " << RFC << endl;
  cout << "Nombre: " << Nombre << endl;
}

Empleado::~Empleado() {}

// -------
int main()
{
  list<Empleado> * list1 = new list<Empleado>();

  string element1;
  string element2;

  int dim, choice, pos;

  do{
      cout << "Select a choice.\n";
      cout << "1. Print list\n";
      cout << "2. Delete an element of the list\n";
      cout << "3. Search an element of the list\n";
      cout << "4. Exit\n";
      cout << "Option: ";
      cin >> choice;
      cout << endl;

      switch(choice)
      {
        case 1:
        {
          cout << "Printing list:\n";
          list1->add_sort(Empleado("DUDE010101R0", "Juan Peres"));
          list1->add_sort(Empleado("AUDE010101R1", "Ana Banana"));
          list1->print();
          break;
        }

        case 2:
        {
          cout << "Element to delete: DUDE010101R0\n";
          list1->del_by_data(2);
          element1 = "";
          break;
        }

        case 3:
        {
          cout << "Element to search: DUDE010101R0\n";
          list1->search("DUDE010101R0");
          element1 = "";
          list1->print();
          break;
        }
      }
  }while(choice != 4);

  return 0;
}

Hope this helps somebody out there.

Please remember to mark this solved. This helps others who are searching to know you have an answered question.

This question has already been answered. Start a new discussion instead.