线性表A,B顺序存储合并
有两张非递增有序的线性表A,B,采用顺序存储结构,两张表合并用c表存,要求C为非递减有序的,然后删除C表中值相同的多余元素。元素类型为整型
#include<iostream>
#include<cstring>
using namespace std;
typedef struct f{
int data;
f* next;
}node,*Node;
void build(f *p){
int x;
while(cin>>x,~x){
f *now=new node();
now->data=x;
now->next=p->next;
p->next=now;
}
}
void build(Node p1,Node p2,Node pre){
p1=p1->next;
p2=p2->next;
while(p1!=NULL&&p2!=NULL){
if(p1->data<=p2->data){
Node now=new node();
now->data=p1->data;
now->next=pre->next;
pre->next=now;
pre=pre->next;
p1=p1->next;
}
else{
Node now=new node();
now->data=p2->data;
now->next=pre->next;
pre->next=now;
pre=pre->next;
p2=p2->next;
}
}
while(p1!=NULL){
Node now=new node();
now->data=p1->data;
now->next=pre->next;
pre->next=now;
pre=pre->next;
p1=p1->next;
}
while(p2!=NULL){
Node now=new node();
now->data=p2->data;
now->next=pre->next;
pre->next=now;
pre=pre->next;
p2=p2->next;
}
}
void flush(Node p){
if(p)p=p->next;
Node pre=p;
if(p)p=p->next;
while(p!=NULL){
if(p->data==pre->data){
pre->next=p->next;
Node k=p;
p=p->next;
delete k;
}
else{
pre=p;
p=p->next;
}
}
}
void show(Node p){
p=p->next;
while(p!=NULL){
cout<<p->data;
p=p->next;
if(p!=NULL) cout<<',';
}
}
int main(){
Node p1=new node();
build(p1);
Node p2=new node();
p2->next=NULL;
build(p2);
Node p3=new node();
p3->next=NULL;
build(p1,p2,p3);
flush(p3);
show(p3);
return 0;
}