Oop 创建一个C++;单链表的实现
我对面向对象编程非常陌生,我正在尝试完成这个硬件任务,包括从链表中插入和删除数据。我想我已经创建了节点,但是我不明白为什么我不能显示它。在继续之前,我想确保节点确实已被添加。任何帮助都将不胜感激强>Oop 创建一个C++;单链表的实现,oop,c++11,singly-linked-list,Oop,C++11,Singly Linked List,我对面向对象编程非常陌生,我正在尝试完成这个硬件任务,包括从链表中插入和删除数据。我想我已经创建了节点,但是我不明白为什么我不能显示它。在继续之前,我想确保节点确实已被添加。任何帮助都将不胜感激 #include <iostream> using namespace std; //void addNode(int num); //void displayList(); struct node{ //Where the data within the node is intia
#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();
}
#包括
使用名称空间std;
//void addNode(int num);
//void displayList();
结构节点{
//其中节点内的数据被初始化
int数据;
//其中节点前进指针被初始化
节点*下一步;
};
班级名单{
私人:
//在头指针和尾指针被初始化的地方,它们将保持第一个和最后一个节点(为了不失去跟踪)
节点*头,*尾;
公众:
//构造函数(用于类)
列表()
{
//其中,节点标头设置为NULL
head=NULL;
//其中,节点尾部设置为NULL
tail=NULL;
}
void addNode(int num)
{
//通过调用节点结构创建临时节点
node*temp=新节点;
//用户输入的编号将添加到列表中节点的数据部分
温度->数据=num;
//这条新尾巴是用手指做成的
temp->next=NULL;
if(head==NULL)
{
//如果输入的数据是第一个节点
//第一个和最后一个节点是正在输入的数据
压头=温度;
尾=温度;
}
其他的
{
//将尾部后的节点(最后一个节点)设置为与输入的数据相等
尾部->下一步=温度;
//将尾部(最后一个节点)设置为等于temp之后的节点
tail=tail->next;
}
}
void displayList()
{
节点*显示节点=新节点;
显示节点=头部;
if(displayNode!=NULL)
{
cout要打印列表,您需要在displayList()
函数中以相同的条件使用。
在列表上运行的while循环直到节点->下一个
将指向一个NULL
值(这意味着这是列表的结尾)
在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();
}
#包括
使用名称空间std;
//void addNode(int num);
//void displayList();
结构节点{
//其中节点内的数据被初始化
int数据;
//其中节点前进指针被初始化
节点*下一步;
};
班级名单{
私人:
//在头指针和尾指针被初始化的地方,它们将保持第一个和最后一个节点(为了不失去跟踪)
节点*头,*尾;
公众:
//构造函数(用于类)
列表()
{
//其中,节点标头设置为NULL
head=NULL;
//其中,节点尾部设置为NULL
tail=NULL;
}
void addNode(int num)
{
//通过调用节点结构创建临时节点
Node*temp=新节点;
//用户输入的编号将添加到列表中节点的数据部分
温度->数据=num;
//这条新尾巴是用手指做成的
temp->next=NULL;
if(head==NULL)
{
//如果输入的数据是第一个节点
//第一个和最后一个节点是正在输入的数据
压头=温度;
尾=温度;
}
其他的
{
//将尾部后的节点(最后一个节点)设置为与输入的数据相等
尾部->下一步=温度;
//将尾部(最后一个节点)设置为等于temp之后的节点
tail=tail->next;
}
}
void displayList()
{
节点*显示节点=新节点;
显示节点=头部;
while(displayNode!=NULL)
{
库特