#include<iostream>
using namespace std;


class linkedlist
{private:
       llink* firstelem;
.....................................
     struct llink                 
     {
    int elem;
    llink* nextelem;
     }
......................................//when i take the struct out the class       definition   ,  the  program 
works well.but just like this it doesn't working! i am confused .
...........................................................................
 public:
       linkedlist(void);
       ~linkedlist(void);
       void AddElement(int elem1);
       void DisplayList(void);
 };
linkedlist::linkedlist()
{
 firstelem=NULL;
}
linkedlist::~linkedlist(void)
{ }
void linkedlist::AddElement(int elem1)
{
   llink* newlink=new llink;
   newlink->elem=elem1;
   newlink->nextelem=firstelem;
   firstelem=newlink;
}
void linkedlist::DisplayList()
{ llink* currentelem=firstelem;
   while(currentelem!=NULL)
   {
       cout<<currentelem->elem<<"-";
       currentelem=currentelem->nextelem;
    }
     cout<<"END"<<endl;
}
int main()
{
  linkedlist TestList;
  TestList.AddElement(5);
  TestList.AddElement(54);
  TestList.AddElement(3);
  TestList.AddElement(25);
  TestList.DisplayList();
  return 0;
}

Recommended Answers

All 7 Replies

struct llink 
{
int elem;
llink* nextelem;
}
......................................//when i take the struct out the class definition , the program 
works well.but just like this it doesn't working! i am confused .
...........................................................................

You have to end a struct with ;

should be

struct llink 
{
int elem;
llink* nextelem;
};

thanks jhdobbins!
but the problme is still existent.
i don't know why.

hardly any variable you use is not defined...

also use TABSPACE for formatting CODE... its very hard to read without them.

and why on every function do you have (void) as a variable or variable type?? those are not needed

A struct is just like a class in that it has to be terminated with a semicolon. And you need to declare an instance of the struct after it is declared, not before:

#include<iostream>
using namespace std;

class linkedlist
{
private:
  struct llink
  {
    int elem;
    llink* nextelem;
  };

  llink* firstelem;
public:
  linkedlist(void);
  ~linkedlist(void);
  void AddElement(int elem1);
  void DisplayList(void);
};

linkedlist::linkedlist()
{
  firstelem=NULL;
}

linkedlist::~linkedlist(void)
{ }

void linkedlist::AddElement(int elem1)
{
  llink* newlink=new llink;
  newlink->elem=elem1;
  newlink->nextelem=firstelem;
  firstelem=newlink;
}

void linkedlist::DisplayList()
{
  llink* currentelem=firstelem;

  while(currentelem!=NULL)
  {
    cout<<currentelem->elem<<"-";
    currentelem=currentelem->nextelem;
  }

  cout<<"END"<<endl;
}

int main()
{
  linkedlist TestList;

  TestList.AddElement(5);
  TestList.AddElement(54);
  TestList.AddElement(3);
  TestList.AddElement(25);
  TestList.DisplayList();

  return 0;
}

>those are not needed
But not wrong. Some people prefer that as their style, even though it's "an abomination". ;)

thank you jhdobbins and narue.
is that means when i declare a instance of the class i need also declare a instance of the struct?
how can i do that?

>is that means when i declare a instance of the class i need also declare a instance of the struct?

No. You can have one (or more) lists and each list can have zero to N links/nodes/whatever. You have declared a list object in main(), which is fine, without declaring any links when you do this:

linkedlist TestList;

and each time you call AddElement you declare a new instance of llink when you do this:

llink* newlink=new llink;

thanks lerner!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.