class linkedlist{
class listNode{
int data;
listNode* next;
void listNode(int data,listNode* next){
this->data=data;
this->next=next;
}
};
int main(){
// in here I want to creat a listnode, but i dont know how to do it
insert(listNode,5);
}
我需要在函数中调用 listnodemain()
我知道我可以使用linkedlist list;
然后list.listNode
,但我需要一个节点来填充insert()
函数。
您可以简单地创建一个 listNode 变量,如下所示
int main(){
linkedlist::listNode aNode(5, NULL);
// or
linkedlist::listNode *pNode = new linkenlist::listNode(5,NULL);
....
}
listNode
是在类内部定义的类linkedList
。这意味着您需要在使用所描述的符号之前输入适当的命名空间。此外,您需要调用所需类的构造函数。老实说,您最好阅读一些C++教程,因为您的示例有更多错误。
有效的very simple
实现是:
#include <iostream>
class linkedList {
public:
class listNode {
public:
int data;
listNode* next;
listNode(int data_) : data(data_), next(nullptr) {}
};
linkedList() : head(nullptr), tail(nullptr) {}
void insert(listNode* new_node) {
if (head == tail && tail == nullptr) {
head = tail = new_node;
}
else {
tail->next = new_node;
tail = new_node;
}
}
listNode* getFront() { return head; };
listNode* getBack() { return tail; };
listNode* head;
listNode* tail;
};
int main(){
linkedList list;
list.insert(new linkedList::listNode(5));
list.insert(new linkedList::listNode(6));
list.insert(new linkedList::listNode(7));
std::cout << list.getFront()->data << std::endl; // 5
std::cout << list.getBack()->data << std::endl; // 7
return 0;
}
在此处输入图像描述
enter code here
class linkedlist{
public:
class listNode{
public:
int data;
listNode* next;
listNode(int data,listNode* next){
this->data=data;
this->next=next;
}
void printNode(listNode* node,ofstream& outFile){
outFile<<"( "<<node->data<<","<<node<<","<<node->next<<","<<node->next-
>data<<")"<<endl;
}
};
// listNode* listHead;
// listNode* listHead(){
// return new listNode(-9999,NULL);
// }
listNode* findSpot(listNode* listHead,listNode* newNode){
listNode* spot=listHead;
while(spot->next!=NULL && spot->next->data<newNode->data)
spot=spot->next;
return spot;
}
void listInsert(listNode* listHead,listNode* newNode){
listNode* spot=findSpot(listHead,newNode);
newNode->next=spot->next;
spot->next=newNode;
}
void printList(listNode* listHead,ofstream& outFile){
listNode* c=listHead;
outFile<<"listHead -> ";
while(c->next!=NULL){
outFile<<"( "<<c->data<<","<<c<<","<<c->next<<","<<c->next->data<<") -> ";
c=c->next;
}
outFile<<" NULL "<<endl;
}
void constructLL(linkedlist::listNode* listHead,ifstream& inFile,ofstream& outFile2){
int a;
cout<<listHead->data<<endl;
while(inFile>>a){
listNode* newNode=new listNode(a,NULL);
listInsert(listHead,newNode);
printList(listHead,outFile2);
}
}
listNode* findMiddleNode(listNode* listHead,ofstream& outFile2){
listNode* walk1=listHead;
listNode* walk2=listHead;
listHead->printNode(walk1, outFile2);
while(walk2!=NULL && walk2->next!=NULL){
walk1=walk1->next;
walk2=walk2->next->next;
}
return walk1;
}
};
int main(int argc ,char* argv[]) {
ifstream inFile(argv[1]);
ofstream outFile1(argv[2]);
ofstream outFile2(argv[3]);
// linkedlist list;
// list.listHead();
cout<<"123";
linkedlist:: listNode listHead(-9999,NULL);
// list.sethead(-9999);
// listHead=new listNode(-9999,NULL);
// cout<<list.gethead();
constructLL(listHead,inFile,outFile2);
// printList(listHead,outFile1);
// listNode* middleNode=findMiddleNode(listHead,outFile2);
// listHead->printNode(middleNode,outFile1);
inFile.close();
outFile1.close();
outFile2.close();
return 0;
}