#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<queue>
using namespace std;
//结点
typedef struct Lnode {
int data;
struct Lnode *next;
}Lnode, *Linklist;
//尾插
void Creatlist(Linklist &L, int &e) {
Lnode *p = new Lnode;
p->data = e;
p->next = NULL; //处理新加入节点
Lnode *cur = L;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = p;
p->next = NULL;
}
//初始化
void Initlist(Linklist &L) {
L = new Lnode;
L->next = NULL;
}
//分解链表
void Split(Linklist &L1) {
Lnode *c1 = L1;
queue <int> myqueue; //存储负数
while (c1->next)
{
if (c1->next->data > 0) {
c1 = c1->next;
}
else if (c1->next->data < 0) {
myqueue.push(c1->next->data);
c1->next = c1->next->next;
}
}
//输出
while (!myqueue.empty()) //输出交集
{
if (myqueue.size() > 1) {
cout << myqueue.front() << " ";
myqueue.pop();
}
else
{
cout << myqueue.front() << endl;
myqueue.pop();
}
}
c1 = L1;
while (c1->next->next)//正数
{
cout << c1->next->data << " ";
c1 = c1->next;
}
cout << c1->next->data << endl;
}
int main() {
int n;
while (scanf("%d",&n)!=EOF) {
if (n == 0) break;
Linklist L1; //原始数据->正数
Initlist(L1);
for (int i = 0; i < n; i++) { //输入原始数据
int e;
cin >> e;
Creatlist(L1, e);
}
Split(L1); //链表分解
}
}
BJFU 229链表的分解 【用原始链表L1存储正数 新建队列存储负数】
最新推荐文章于 2024-11-06 09:48:48 发布