How do I use this Linked List implementation?

rishiag

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 () {
}
doctorlove

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pop method for doubly linked list implementation in C, do I need to use free

From Dev

How to use a Linked-List implementation with this Hash Function to avoid collisions?

From Dev

How do I reverse through a linked list

From Dev

How do I Sort the names in a Linked List?

From Dev

How do I select the right List implementation?

From Dev

Laravel use of PHP Linked List implementation SplDoublyLinkedList();

From Dev

How to clone a linked list with a head/tail implementation?

From Dev

How do I code a function to test if a linked list is sorted

From Dev

How do I fix that infinite loop on a custom double linked list?

From Dev

How do I implement generics <E> in a linked List?

From Dev

How do I implement SelectionSort and InsertionSort on a linked list in Python?

From Dev

How do I get insertion sort to work with a doubly linked list

From Dev

How do I add an object to a linked list in alphabetical order in Java?

From Dev

How do I print the next element in a linked list to a CSV file?

From Dev

How do I remove the first node in a linked list with a remove method

From Dev

How do I properly delete nodes of linked list in C++

From Dev

How do I remove the only node in a linked list in java?

From Dev

How do I implement SelectionSort and InsertionSort on a linked list in Python?

From Dev

How do I get insertion sort to work with a doubly linked list

From Dev

How do I implement generics <E> in a linked List?

From Dev

How do I search through a Java Linked List

From Dev

How Do I Fix This Sorted Linked List Insertion?

From Dev

How can I do a recursive print using class linked list

From Dev

How do I properly deallocate memory allocated with a linked list?

From Dev

How do I get the first character of a string from a linked list?

From Dev

How do I sort a linked list in a alphabetical order in c

From Dev

How do I store this linked list in an object array?

From Dev

How do I use Azure Key Vault secret in linked template

From Dev

How do I add a linked document to a linked list using the Java API for OrientDB?

Related Related

  1. 1

    Pop method for doubly linked list implementation in C, do I need to use free

  2. 2

    How to use a Linked-List implementation with this Hash Function to avoid collisions?

  3. 3

    How do I reverse through a linked list

  4. 4

    How do I Sort the names in a Linked List?

  5. 5

    How do I select the right List implementation?

  6. 6

    Laravel use of PHP Linked List implementation SplDoublyLinkedList();

  7. 7

    How to clone a linked list with a head/tail implementation?

  8. 8

    How do I code a function to test if a linked list is sorted

  9. 9

    How do I fix that infinite loop on a custom double linked list?

  10. 10

    How do I implement generics <E> in a linked List?

  11. 11

    How do I implement SelectionSort and InsertionSort on a linked list in Python?

  12. 12

    How do I get insertion sort to work with a doubly linked list

  13. 13

    How do I add an object to a linked list in alphabetical order in Java?

  14. 14

    How do I print the next element in a linked list to a CSV file?

  15. 15

    How do I remove the first node in a linked list with a remove method

  16. 16

    How do I properly delete nodes of linked list in C++

  17. 17

    How do I remove the only node in a linked list in java?

  18. 18

    How do I implement SelectionSort and InsertionSort on a linked list in Python?

  19. 19

    How do I get insertion sort to work with a doubly linked list

  20. 20

    How do I implement generics <E> in a linked List?

  21. 21

    How do I search through a Java Linked List

  22. 22

    How Do I Fix This Sorted Linked List Insertion?

  23. 23

    How can I do a recursive print using class linked list

  24. 24

    How do I properly deallocate memory allocated with a linked list?

  25. 25

    How do I get the first character of a string from a linked list?

  26. 26

    How do I sort a linked list in a alphabetical order in c

  27. 27

    How do I store this linked list in an object array?

  28. 28

    How do I use Azure Key Vault secret in linked template

  29. 29

    How do I add a linked document to a linked list using the Java API for OrientDB?

HotTag

Archive