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 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿