Link to home
Start Free TrialLog in
Avatar of beelsepub
beelsepub

asked on

Problem with defining a classfunction for Linked list

I'm making a object-oriented linked list in c++, and I'm having problems with asigning new value to my head pointer:

#include "Lista.h"
#include <cstddef>
#include <cassert>

LankadLista::LankadLista(): size(0), head(NULL){}

LankadLista::~LankadLista(){
     //while (!KollaLista())
          //remove(1);
}

void LankadLista::LaggTill(int index, dataTyp nyNod)
{
     int nyLangd = ReturElement() + 1;

     Nod *nyPek = new Nod;  
     size = nyLangd;

     nyPek->data = nyNod;

     if (index == 1)
     {    
          nyPek->next = head;
          head = nyPek;
     }
     else
     {
          Nod *prev = SokElement(index-1);
          nyPek->next = prev->next;
          prev->next = nyPek;
     }
}
/*void Tabort(){
}
void Sortera(){
}
void skrivUt(){
}*/
int LankadLista::SokElement(int index)
{
     /*if ((index < 1) || (index > ReturElement()))
          return NULL;
     else
     {
          Nod *cur = head;
          for (int skip = 1; skip < index; ++skip)
               cur = cur->next;
          return cur;
     }*/
}
bool LankadLista::KollaLista() const
{
     return bool(size==0);
}
int LankadLista::ReturElement()
{
     return size;
}

The problem is in the Laggtill(insert) function, head = nyPek. "cannot convert 'LankadLista::Nod*' to 'Nod*' in assignment"
Here is the classfile:

#include <iostream>
using namespace std;

typedef int dataTyp;
typedef struct Nod *pek;

class LankadLista {

public:
     LankadLista();
     ~LankadLista();

     void LaggTill(int index, dataTyp NyNod);
     //void Tabort();
     //void Sortera();
     //void skrivUt();
     int SokElement(int index);
     bool KollaLista() const;
     int ReturElement();

private:

     struct Nod {
          dataTyp data;
          pek next;
     };

     int size;
     Nod *head;

};

I hope someone can help me, thanks.
//Olle
Avatar of Axter
Axter
Flag of United States of America image

Why are you making object-oriented linked list in c++?
C++ already has a built-in link list called std::list.

It looks like you're trying to reinvent the wheel.

Is there a paticular reason why you can't use std::list?
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of beelsepub
beelsepub

ASKER

Why I'm making a linked list object-oriented is because it's a part of my project in my c++ course.
I guess we're only doing it because we need to learn to think object-oriented for future courses.

Axter, I'll give it a try, thanks.
I solved it by deleting the typedef struct completely and write Nod *next; in the struct instead. But I'll accept your comment as answer since I didnt understand that typedef struct was the problem.