简体   繁体   English

我如何创建单个列表,然后从中创建两个单个列表,其中一个用于偶数,另一个用于奇数?

[英]How i can create a single list , then create two single lists from it one of them for even numbers and another for odd numbers?

I'm a beginner programmer in my first years in college, I'm working with single linked lists in c++, and I'm trying to write a program without using classes to create a single linked list input from a user and print it, then I want to put the even numbers in a new list and print this new list and the odd numbers in another new list and print it too.我在大学的第一年是一名初学者程序员,我正在使用 C++ 中的单链表工作,并且我正在尝试编写一个程序而不使用类来创建来自用户的单链表输入并打印它,然后我想将偶数放在一个新列表中并打印这个新列表和另一个新列表中的奇数并打印它。 I began with this, I wish if someone can help me.我从这个开始,我希望有人能帮助我。

#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);
}

First I fixed the problems首先我解决了问题

#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);
}

Now you can create a second list , call it even , iterate through the first list and copy all even elements into the second list.现在您可以创建第二个list ,将其命名为even ,遍历第一个list并将所有偶数元素复制到第二个列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从用户那里获取多个数字并将偶数相加并乘以奇数? - How can I get multiple numbers from a user and add even numbers together and multiply odd numbers? 如何将输出数字除以奇数和偶数? - How can I divide the output numbers in odd and even numbers? 我可以使用单个“ default_random_engine”来创建多个正态分布的数字集吗? - Can I use a single `default_random_engine` to create multiple normally distributed sets of numbers? 如何创建可以创建多个链接列表的单个 function - How Can I Create A Single function that can create multiple Linked Lists 如何将数字从一个文本文件复制到另一个文本文件但使它们成为下一个数字? - How do I copy numbers from one text file to another but make them the next number? 如何让我的 function 只接受奇数进入我的数组,而拒绝偶数? C++ - How can I make my function only accept odd numbers into my array, and reject even numbers? C++ 如何分隔数组中的奇数和偶数? - How do I segregate odd and even numbers in an array? 按偶数和奇数排序 - Sort by Even and Odd numbers 如何从文件中读取数字并在数组中使用它们? - How can I read numbers from an file and use them in an array? 如何在 C++ 中创建一个数字列表,以便它可以选择一个随机数? - How to Create a list of numbers in C++ such that it can choose a random number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM