协助单链表(c++)程序
Assistance with Singly Linked List (C++) program
我已经开始用c++学习链表,我已经写了一个程序来创建和显示列表。它编译得很好,但是执行失败。
代码如下:
#include <iostream>
using namespace std;
class node
{
public:
int data; // information contained by the node
node *next; // a pointer pointing to an instance of the class node.
};
class list
{
public:
node *head;
node *temp, *tp;
void createlist();
void displaylist();
};
void list :: createlist()
{
int size;
cout<<"Enter the size of the list you want to create.n";
cin>>size;
temp=head;
for(int k=1;k<=size;k++)
{
if(head == NULL)
{
head = new node[1];
cout<<"Enter the data for node number 1n";
cin>>head->data;
head->next=NULL;
}
else
{
temp->next=new node[1];
temp=temp->next;
cout<<"Enter the data for node number n"<<k;
cin>>temp->data;
temp->next=NULL;
}
}
}
void list::displaylist()
{
if (head==NULL)
{
cout<<"List is empty.";
}
else
{
while(tp!=NULL)
{
tp = head;
cout<<tp->data;
tp = tp->next;
}
}
}
int main()
{
list obj;
cout<<"Creating list...n";
obj.createlist();
cout<<"Displaying list...n";
obj.displaylist();
return 0;
}
#include <iostream>
using namespace std;
class node
{
public:
int data; // information contained by the node
node *next; // a pointer pointing to an instance of the class node.
};
class list
{
public:
node *head;
node *temp, *tp;
void createlist();
void displaylist();
};
void list::createlist()
{
int size;
cout<<"Enter the size of the list you want to create.n";
cin>>size;
temp=head;
for(int k=1;k<=size;k++)
{
if(head == NULL)
{
head = new node[1];
cout<<"Enter the data for node number 1n";
cin>>head->data;
head->next=NULL;
}
else
{
temp->next=new node[1];
temp=temp->next;
cout<<"Enter the data for node number n"<<k;
cin>>temp->data;
temp->next=NULL;
}
}
}
void list::displaylist()
{
if (head==NULL)
{
cout<<"List is empty.";
}
else
{
while(tp!=NULL)
{
tp = head;
cout<<tp->data;
tp = tp->next;
}
}
}
int main()
{
list obj;
cout<<"Creating list...n";
obj.createlist();
cout<<"Displaying list...n";
obj.displaylist();
return 0;
}
输出如下:
Creating list...
Enter the size of the list you want to create.
3
Enter the data for node number 1
1
因此,第二个节点的数据不被接受。我是链表的新手,想尝试用这种方法解决问题,我在这上面花了太多时间。我哪里做错了?
数据成员head和tp在对象obj创建时未初始化,并且具有任意值。所以程序有未定义的行为。此外,将临时变量作为类的tp或临时数据成员也是一个坏主意。它们必须是使用它们的成员函数的局部变量。
没有任何意义,因为你正在做分配一个节点数组。
head = new node[1];
/...
temp->next=new node[1];
写简单的
head = new node();
/...
temp->next=new node();
最好将类node定义为类list的内部类。
并且不要忘记写析构函数来释放分配的内存。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 使用mongocxx驱动程序时包含头文件问题
- 如何在c++程序中找到函数的地址