,我是面向对象编程的新手,我试图完成这个hw任务,包括从链接列表中插入和删除数据。我认为我的节点创建工作,但我不知道为什么我不能显示它。在继续之前,我想确保节点实际上是被添加的。任何帮助都将不胜感激!!
#include <iostream>
using namespace std;
//void addNode(int num);
//void displayList();
struct node{
//Where the data within the node is intialized
int data;
//Where the node advancement pointer is intialized
node *next;
};
class list{
private:
//Where the head and tail pointers are intialized, these will hold the first and last node (in order to not lose track)
node *head,*tail;
public:
//The constructor (for the class)
list()
{
//Where the node header is set to NULL
head=NULL;
//Where the node tail is set to NULL
tail=NULL;
}
void addNode(int num)
{
//A temp node is made by calling the node struct
node *temp=new node;
//The user entered number is added to the data portion of the node in the list
temp->data=num;
//The new tail is made and pointed to
temp->next=NULL;
if(head == NULL)
{
//If the data being entered is the first node
//First andlast nodes is the data being entered
head = temp;
tail = temp;
}
else
{
//Set node after tail (last node) equal to the data being entered
tail->next = temp;
//Set the tail (last node) equal to the node after temp
tail = tail->next;
}
}
void displayList()
{
node *displayNode=new node;
displayNode=head;
if(displayNode!=NULL)
{
cout<<"display list";
cout<<displayNode->data<<endl;
displayNode=displayNode->next;
}
}
};
int main() {
//Creating arguments for the list class
list first;
list second;
list third;
//Where the class member functions are called with the data 1, 2, and 3
first.addNode(1);
second.addNode(2);
third.addNode(3);
//Whre the display calss member function is called
list print;
print.displayList();
}发布于 2019-10-06 22:52:42
为了打印列表,需要在while函数中使用具有相同条件的displayList()。在列表上运行的while循环将指向一个node->next值(这意味着列表的结束)。
在main()中,只需初始化一个列表并将所有值添加到其中。
此外,代码样式的建议是以大写字母开头,并为每个新单词设置大写字母--区分函数和类是有用的,例如:
class MyClass
{
};
struct MyStruct
{
};代码如下所示:
#include <iostream>
using namespace std;
//void addNode(int num);
//void displayList();
struct Node{
//Where the data within the node is intialized
int data;
//Where the node advancement pointer is intialized
Node *next;
};
class List{
private:
//Where the head and tail pointers are intialized, these will hold the first and last node (in order to not lose track)
Node *head,*tail;
public:
//The constructor (for the class)
List()
{
//Where the node header is set to NULL
head=NULL;
//Where the node tail is set to NULL
tail=NULL;
}
void addNode(int num)
{
//A temp node is made by calling the node struct
Node *temp=new Node;
//The user entered number is added to the data portion of the node in the list
temp->data=num;
//The new tail is made and pointed to
temp->next=NULL;
if(head == NULL)
{
//If the data being entered is the first node
//First andlast nodes is the data being entered
head = temp;
tail = temp;
}
else
{
//Set node after tail (last node) equal to the data being entered
tail->next = temp;
//Set the tail (last node) equal to the node after temp
tail = tail->next;
}
}
void displayList()
{
Node *displayNode=new Node;
displayNode=head;
while(displayNode!=NULL)
{
cout<<"display list";
cout<<displayNode->data<<endl;
displayNode=displayNode->next;
}
}
};
int main() {
//Creating arguments for the list class
List first;
//Where the class member functions are called with the data 1, 2, and 3
first.addNode(1);
first.addNode(2);
first.addNode(3);
//Whre the display calss member function is called
first.displayList();
}https://stackoverflow.com/questions/58261772
复制相似问题