【发布时间】:2019-10-06 22:37:31
【问题描述】:
我是面向对象编程的新手,我正在尝试完成这个涉及从链表中插入和删除数据的硬件任务。我想我已经创建了节点,但我不知道为什么我不能显示它。我想确保在继续之前确实添加了节点。任何帮助将不胜感激!
#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();
}
【问题讨论】:
标签: oop c++11 singly-linked-list