#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
using namespace std;
typedef long long ll;
typedef struct node
{
int data;
node* next;
} Node,*List;
node* jianli()//单项链表的建立
{
node* head=new Node;
head->next=NULL;
int n,x;
cin>>n;//输入节点个数
node *p=head;
for(int i=0; i<n; i++)
{
node* qq=new Node;
cin>>x;//输入该结点数据域
qq->data=x;
p->next=qq;
qq->next=NULL;
p=qq;
}
return head;
}
void Print(node *head)
{
Node *p=head;
if(head->next==NULL)
{
printf("NULL\n");
return;
}
while(p->next)
{
printf("%d ",p->next->data);
p=p->next;
}
cout<<endl;
}
void helan(node* head,int num)
{
node* he=head->next;
node* s=new node;
s->next=NULL;
node* b=new node;
b->next=NULL;
node* n1=new node;
n1->next=NULL;
node* s2=s;
node* b2=b;
node* n2=n1;
while(he)
{
if(he->data<num)
{
s2->next=he;
s2=he;
}
else if(he->data==num)
{
n2->next=he;
n2=he;
}
else
{
b2->next=he;
b2=he;
}
he=he->next;
}
node* dui=head;
if(s->next)
{
dui->next=s->next;
dui=s2;
}
if(n1->next)
{
dui->next=n1->next;
dui=n2;
}
if(b->next)
{
dui->next=b->next;
dui=b2;
}
dui->next=NULL;
}
int main()
{
node* head=jianli();
Print(head);
int num;
while(cin>>num)
{
helan(head,num);
Print(head);
}
return 0;
}
链表实现荷兰国旗
最新推荐文章于 2021-03-02 11:26:50 发布