C++ 如何打印Linkedlist的数据部分
基本上我想打印链表的数据部分,它基本上是一个整数指针,我在创建时给它分配了一个数组,我想打印它的所有值,怎么做??? 非常感谢。 这是我的代码 使用名称空间stdC++ 如何打印Linkedlist的数据部分,c++,visual-c++,C++,Visual C++,基本上我想打印链表的数据部分,它基本上是一个整数指针,我在创建时给它分配了一个数组,我想打印它的所有值,怎么做??? 非常感谢。 这是我的代码 使用名称空间std struct Node{ Node *next; int *data; }; class DataLine{ private: Node *first; public: DataLine(){ first=NULL; } void create_l
struct Node{
Node *next;
int *data;
};
class DataLine{
private:
Node *first;
public:
DataLine(){
first=NULL;
}
void create_list(){
Node *temp=new Node;
int i=2;
int dat[5]={12,13,14,13,16};
temp->data=dat;
temp->next=NULL;
if(first==NULL){
//cout<<"hello 1"<<endl;
first=temp;
}
else{
Node *curr=first; //We are now doing trevercing so we are assigning the first to the node because we donot want to move the first bacuse link break if we move the first
while(curr->next!=NULL) //searching end of list
{
curr=curr->next; //Moving to the next node
}
curr->next=temp; //insert node
temp=NULL; //Making the temp enpty to use it for the new purpose
//delete temp;
}
}
void print_list()
{
Node *prnt=first; //We are now again want trevercing so we agin run the node by the new node
while(prnt!=NULL) //Checking the loop will run till it get the null in the node means its address part and data part both are nUll
{
for(int i=0;i<5;i++)
cout<<" ***** The "<<" node is "<<*(prnt->data+i)<<endl; //Printing the data
prnt=prnt->next; //Moving to the next node
}
}
};
int main(){
DataLine dl;
dl.create_list();
dl.print_list();
_getch();
return 0;
}
struct节点{
节点*下一步;
int*数据;
};
类数据线{
私人:
节点*第一;
公众:
数据线(){
第一个=空;
}
void create_list(){
Node*temp=新节点;
int i=2;
int-dat[5]={12,13,14,13,16};
温度->数据=数据;
temp->next=NULL;
if(first==NULL){
//cout你的void print\u list(void)
的想法是正确的,但是你可以让它更干净,注意,但是我改变了你的输出,每行打印一个节点(如果你想的话,可以把它改回去)。在我看来,for
循环的结构对于链表来说是完美的,并且保持了链表代码在循环体中
void print_list(void) const
{
for (Node* p = first; p != NULL; p = p->next)
{
for (int i = 0; i < Node::unLength; ++i) std::cout << p->data[i] << ", ";
std::cout << std::endl;
}
}
我在这里还添加了一个数组长度常量,因为在代码周围浮动幻数是一种不好的做法,如果键入其中一个会发生什么
现在,在您的void create_list()
中,您可以执行以下操作:
void create_list()
{
Node* temp = new Node;
// Set the next node of temp
temp->next = NULL;
// Add some data to temp (we can't just assign the data pointer in C/C++)
int data[Node::unLength] = {0, 1, 2, 3, 4};
for (int i = 0; i < Node::unLength; ++i) temp->data[i] = data[i];
Node *p = first;
while (p != NULL) p = p->next;
p->next = temp;
}
您的想法是正确的,但是关于C/C++有一些在高级语言中不明显的细微之处,例如复制数组和变量的范围
如果你使用C++,我会建议不要担心链接列表,只需在C++ 11中创建一个<代码> STD::vector < /C> >,下面的工作可能是(未测试的):
#包括
#包括
int main(int argc,字符**argv)
{
std::vectormyData;
myData.push_back({0,1,2,3,4});
myData.push_back({0,1,2,3,4});
myData.push_back({0,1,2,3,4});
用于(常量自动&i:myData)
{
对于(intj:i)std::你能确定你的创建列表有效吗?我不这么认为。你到底想在创建列表()中实现什么。因为您仅将数组的基址分配给一个节点。因此它将仅为一个节点,即第一个。dat
是一个局部变量。它超出范围,并在create\u list
返回时立即销毁,此时first->data
将成为一个悬空指针。实际上,您没有任何有效数据可打印.创建工作正常
void create_list()
{
Node* temp = new Node;
// Set the next node of temp
temp->next = NULL;
// Add some data to temp (we can't just assign the data pointer in C/C++)
int data[Node::unLength] = {0, 1, 2, 3, 4};
for (int i = 0; i < Node::unLength; ++i) temp->data[i] = data[i];
Node *p = first;
while (p != NULL) p = p->next;
p->next = temp;
}
class DataLine
{
private:
Node* first;
public:
DataLine(void) : first(NULL) {}
~DataLine(void)
{
while (first != NULL)
{
Node *temp = first->next;
delete first;
first = temp;
}
}
#include <vector>
#include <array>
int main(int argc, char** argv)
{
std::vector< std::array<int, 5> > myData;
myData.push_back({0, 1, 2, 3, 4});
myData.push_back({0, 1, 2, 3, 4});
myData.push_back({0, 1, 2, 3, 4});
for (const auto& i : myData)
{
for (int j : i) std::cout << j << ", ";
std::cout << std::endl;
}
return 0;
}