Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建一个列表,然后从中创建两个列表,一个用于偶数,另一个用于奇数? 我在大学的第一年是一个初学者程序员,我在C++中使用单链表,我试着不用使用类就编写程序。 要创建一个用户输入的单链表并打印它,那么我想输入偶数 在新列表中打印此新列表和另一个新列表中的_C++ - Fatal编程技术网

如何创建一个列表,然后从中创建两个列表,一个用于偶数,另一个用于奇数? 我在大学的第一年是一个初学者程序员,我在C++中使用单链表,我试着不用使用类就编写程序。 要创建一个用户输入的单链表并打印它,那么我想输入偶数 在新列表中打印此新列表和另一个新列表中的

如何创建一个列表,然后从中创建两个列表,一个用于偶数,另一个用于奇数? 我在大学的第一年是一个初学者程序员,我在C++中使用单链表,我试着不用使用类就编写程序。 要创建一个用户输入的单链表并打印它,那么我想输入偶数 在新列表中打印此新列表和另一个新列表中的,c++,C++,如何创建一个列表,然后从中创建两个列表,一个用于偶数,另一个用于奇数? 我在大学的第一年是一个初学者程序员,我在C++中使用单链表,我试着不用使用类就编写程序。 要创建一个用户输入的单链表并打印它,那么我想输入偶数 在新列表中打印此新列表和另一个新列表中的奇数,然后 也把它打印出来。 我从这里开始,我希望有人能帮助我 #include <iostream> using namespace std; struct node { int data; node* next

如何创建一个列表,然后从中创建两个列表,一个用于偶数,另一个用于奇数? 我在大学的第一年是一个初学者程序员,我在C++中使用单链表,我试着不用使用类就编写程序。 要创建一个用户输入的单链表并打印它,那么我想输入偶数 在新列表中打印此新列表和另一个新列表中的奇数,然后 也把它打印出来。 我从这里开始,我希望有人能帮助我

#include <iostream>
using namespace std;
struct node {

    int data;
    node* next;
};
struct Even_node {

    int even_data;
    Even_node* even_next;
};
void creat(node*& head, node*& tail)
{
    int num;
    cout << "enter number , (0) to quiet\n";
    cin >> num;

    while (num != 0) {

        node* nptr = new node;
        nptr->data = num;
        if (head == nullptr)

            head = nptr;

        else

            tail->next = nptr;

        tail = nptr;
        tail->next = nullptr;
        cout << "enter number again or 0 to quiet\n";
        cin >> num;
    }
}

void print(node* head)
{
    cout << "the list is:\t";
    while (head != nullptr) {
        cout << head->data << "\t";
        head = head->next;
    }
    cout << endl;
}
main()
{
    node *head = nullptr, *tail = nullptr;
    creat(head, tail);
    print(head);
}
#包括
使用名称空间std;
结构节点{
int数据;
节点*下一步;
};
结构偶数节点{
整数偶数数据;
偶数节点*偶数下一步;
};
无效创建(节点*&头部、节点*&尾部)
{
int-num;
cout>num;
while(num!=0){
node*nptr=新节点;
nptr->data=num;
if(head==nullptr)
水头=nptr;
其他的
tail->next=nptr;
tail=nptr;
tail->next=nullptr;
cout>num;
}
}
无效打印(节点*头)
{

我可以先解决问题吗

  • 删除了动态内存分配和内存泄漏
  • 它是
    int main
#包括
#包括
使用std::cout;
使用std::cin;
结构节点{
int数据;
std::unique_ptr next;
};
结构列表{
std::唯一的ptr头;
节点*尾部;
};
无效创建(列表和列表)
{
int-num;
cout>num;
while(num!=0){
std::unique_ptr nptr=std::make_unique();
nptr->data=num;
如果(!l.head){
l、 头部=标准:移动(nptr);
l、 tail=l.head.get();
}否则{
l、 尾部->下一步=标准::移动(nptr);
l、 tail=l.tail->next.get();
}
cout>num;
}
}
无效打印(常量列表和列表)
{
自动节点=l.head.get();

您对哪一部分有问题?非常类似,如果您想要两个单独的列表,您可能希望在结尾处创建一个插入列表()函数,该函数引用一个头指针和一个值。对于两个单独的列表,您应该有两个不同的头指针。请记住:避免
new
/
delete
。如果无法避免,则每次调用
new
都需要一个调用
delete
。您可以使用智能指针避免
new
/
删除
类似于<>代码> UNQuyJPPT/<代码>。“不使用类”。结构和类在C++中几乎相同。它们在默认可访问性上不同。
#include <iostream>
#include <memory>
using std::cout;
using std::cin;

struct node {
    int data;
    std::unique_ptr<node> next;
};

struct list {
    std::unique_ptr<node> head;
    node *tail;
};

void creat(list &l)
{
    int num;
    cout << "enter number , (0) to quiet\n";
    cin >> num;

    while (num != 0) {
        std::unique_ptr<node> nptr = std::make_unique<node>();
        nptr->data = num;
        if (!l.head) {
            l.head = std::move(nptr);
            l.tail = l.head.get();
        } else {
            l.tail->next = std::move(nptr);
            l.tail = l.tail->next.get();
        }
        cout << "enter number again or 0 to quiet\n";
        cin >> num;
    }
}

void print(const list &l)
{
    auto node = l.head.get();
    cout << "the list is:\t";
    while (node != nullptr) {
        cout << node->data << "\t";
        node = node->next.get();
    }
    cout << '\n';
}

int main()
{
    list l;
    creat(l);
    print(l);
}