0

I am learning data structures and algorithms for C++ from Goodrich. They have given this LinkedList implementation. I understand the code but I am not but I am not able to use this in main class. How do I construct an instance and make insert, delete? For example I tried to create an instance of the class as follows:

StringLinkedList() L;

But it is showing the error: expected ";" before 'L

#include <iostream>
#include <string>

using namespace std;

class StringNode {
private:
    string elem;
    StringNode* next;

    friend class StringLinkedList;
};

class StringLinkedList{
public:
    StringLinkedList();
    ~StringLinkedList();
    bool empty() const;
    const string& front() const;
    void addFront(const string& e);
    void removeFront();
private:
    StringNode* head;
};

StringLinkedList::StringLinkedList()
    :head(NULL) {}
StringLinkedList::~StringLinkedList()
    {while (!empty()) removeFront();}
bool StringLinkedList::empty() const
    {return head==NULL;}
const string& StringLinkedList::front() const
    {return head->elem;}
void StringLinkedList::addFront(const string& e){
    StringNode* v = new StringNode;
    v->elem=e;
    v->next = head;
    head=v;
}
void StringLinkedList::removeFront(){
    StringNode* old=head;
    head = old->next;
    delete old;
}


int main () {
}
9
  • 3
    "but I am not but I am not able to use this in main class" - what is the exact problem you have with it? please show some example code of how you use it. Commented Aug 20, 2013 at 8:52
  • Ok. I tried to construct an instance of the class using constructor like this: StringLinkedList() L; But it is showing the error expected ";" before 'L'. I don't know any other way of creating the instance. Commented Aug 20, 2013 at 8:53
  • please edit the question when providing more details, don't add that information to the comments Commented Aug 20, 2013 at 8:54
  • 1
    This "linked list implementation" looks more like a stack implemented via linked list to me: You can only access the head of the list, at any time, and there aren't even any methods for iterating through it. Commented Aug 20, 2013 at 8:56
  • 1
    Do you know how to instantiate any class in C++? Maybe that is the problem. Commented Aug 20, 2013 at 8:59

3 Answers 3

3

Brackets () indicate a function call. If you want to declare a variable the syntax is

Typename variable_name;

Optionally, you may need to pass parameters to a constructor

Typename variable_name(param);

In C++11 the uniform initialisation syntax allows you to use {} but I digress. Either way they come after the variable name. In your case, this works:

StringLinkedList L;


When you say

StringLinkedList() L;

the compiler sees a typename, then expects a variable name, but gets () before the name L (BTW - it might deserve a longer name) so decided you must be making a function call, which should end with a semicolon. But it doesn't, it ends with L; so you get

expected ";" before 'L
Sign up to request clarification or add additional context in comments.

4 Comments

+1 Answer reduced to the real problem. Well explained I think.
+1 - It's great to see someone taking the time to answer a question that is clearly for a beginner, rather than complaining about them not understanding what they're trying to learn.
@Merlin069 many users of SO have the bad habit of attack this kind of noob questions as if they are vultures, instead of answering the cuestion realizing that a few time ago they were noobs too.
@Manu343726 exactly. Before entering SO, egos should be left at the door!
1

You can create an instance and add and remove items like this:

int main () {
    StringLinkedList list; // construct an instance
    list.addFront("foo");  // Add "foo"
    list.addFront("bar");  // Add "bar"
    list.removeFront();    // Remove "bar"
    // List is automatically deleted now
}

6 Comments

Thanks. Why doesn't it work if I add the brackets next to method name like StringLinkedList() L;?
@user1425223 Why should that work? It doesn't make sense syntactically.
@user1425223 you mean declaring the variable with the following sentence?: StringLinkedList() list;
@Manu343726 yes, that's what I meant.
It won't work because it's not valid syntax in C++. Why should it work?
|
0

The method that you use for creation of object isn't correct i dont know what do you mean by

StringLinkedList() L;

the StringLinkedList() is a call for the constructor of the class and it can be used for creating objects of class but no need to specify a constructor call unless needed. You can use the constructor if you are creating object and also want to initialise data members of objects for its creation.

StringLinkedList L=StringLinkedList();

is the correct method but you need to write only

StringLinkedList L;

as it will automatically call the default constructor. The actual process happening is the constructor create a temporary object and then assign it to your object variable L; but it will be done automatically

I dont see any problem in declaring the objects of linked list class like this.

int main ()
{
    StringLinkedList s1;
    s1.addFront("hai");
    s1.addFront("dude");
    s1.removeFront();
    cout<<s1.front();
}

1 Comment

Note that StringLinkedList L = StringinkedList() is not the same as StringLinkedList L;. The first is a declaration of a linked list wich uses the copy constructor to initialize the variable with the temporal object created by the explicit call to the constructor in the right side. The second is a variable declaration wich simply calls the default ctor. On the other hand, any decent C++ compiler could elide the copy in the first case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.