[英]How do I use this Linked List implementation?
I am learning data structures and algorithms for C++ from Goodrich. 我正在从Goodrich学习C ++的数据结构和算法。 They have given this LinkedList implementation.
他们已经给出了这个LinkedList实现。 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
在'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 () {
}
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. 在C ++ 11中,统一的初始化语法允许您使用
{}
但我离题了。 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. 编译器会看到一个类型名,然后是一个变量名,但会在名称
L
之前获取()
(BTW-可能应使用更长的名称),因此决定您必须进行函数调用,该函数调用应以分号结尾。 But it doesn't, it ends with L;
但是不是,它以
L;
结尾L;
so you get 所以你得到
expected ";" before 'L
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
}
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. StringLinkedList()是对类的构造函数的调用,可用于创建类的对象,但除非需要,否则无需指定构造函数的调用。 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;
发生的实际过程是构造函数创建一个临时对象,然后将其分配给您的对象变量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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.