functioning 2019-11-08 11:44 采纳率: 0%
浏览 598

用C++实现将一串数字输入一个链表再将这个链表中的奇偶数分开存储在两个链表中,最后输出

我是这样写的,最后输出不出来东西,求大佬帮忙
#include
using namespace std;
struct node
{
int date;
node * next;
};
node * createList(int n);
void printList(node *a);
int main()
{
int n;
node * a = NULL;
node * A = NULL;
node * B = NULL;
node * m = NULL;
node * i = NULL;
cin>>n;
if(n > 0)
a = createList(n);
node *p;
p = a;
int x;
x = 1;
int y;
y = 1;
node *Atail = NULL;
node *Btail = NULL;
while(p->next != NULL)
{

    if(p->date%2 == 1 && x == 1)
    {   

        B = new node; 
        Btail = new node; 
        B->date = p->date;
        Btail = B;
        x = x+1;
    }
    else if (p->date%2 == 1 && x != 1)
    {
        node *temp = new node;


        temp->date = p->date;
        Btail->next = temp;
        Btail = temp;
    }

    else if(p->date%2 == 0 && y == 1)
    {   

        A = new node; 
        Atail = new node; 
        A->date = p->date;
        Atail = A;
        y = y+1;
    }
    else
    {
        node *temp = new node;
        temp->date = p->date;
        Atail->next = temp;
        Atail = temp;
    }
    p = p->next;
}
Atail->next = NULL;
Btail->next = NULL;
printList(A);
cout<<endl;
printList(B);
return 0;

}
void printList(node *a)
{
node * c = a;
while (c->next != NULL)
{
cout<< c->date<<' ';
c = c->next;
}
}
node * createList(int n)
{
node * temp = NULL, * tail = NULL, * head = NULL;
int num;
cin>>num;
head = new node;
if (head == NULL)
{
return NULL;
}
else
{
head->date = num;

    tail = head;
}
for (int i = 0; i<n-1;i++)
{
    cin>>num;
    temp = new node;
    temp->date = num;
    tail->next = temp;
    tail = temp;
}
tail->next = NULL;
return head;

}

  • 写回答

1条回答 默认 最新

  • qtchen_1988 2019-11-08 12:24
    关注

    c++,可以考虑使用stl

    #include <iostream>
    #include <list>
    using namespace std;
    
    void print(list<int> lit)
    {
        list<int>::iterator iter = lit.begin();
        for(;iter != lit.end();iter++)
            std::cout << *iter << " ";
        std::cout << endl;
    }
    
    int main()
    {
        int n;
        std::cin >> n;
        list<int> srcList,evenList,oddList;
        for(int i=0;i<n;i++)
        {
            int temp;
            std::cin >> temp;
            srcList.push_back(temp);
        }
        list<int>::iterator iter = srcList.begin();
        for(;iter != srcList.end();iter++)
            if(*iter % 2 == 0)
                evenList.push_back(*iter);
            else
                oddList.push_back(*iter);
    
        std::cout<<"even list:";
        print(evenList);
        std::cout<<"odd list:";
        print(oddList);
    
        return 0;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决