Description
已知A,B和C为三个非递减有序的线性表,现要求对A表作如下操作:删去那些既在B表中出现又在C表中出现的元素。试对顺序表编写实现上述操作的算法。
Input
第一行输入3个正整数m,n,p(m,n,p<=100),用空格分开,分别表示三个线性表中的元素个数,其后3行依次输入A,B,C表中的元素。
Output
输出实现上述操作后的A表。
-
Sample Input
8 5 6 1 2 3 4 5 6 6 7 2 3 5 9 12 2 4 5 6 12 13
-
Sample Output
1 3 4 6 6 7
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int num;
struct node* llink;
struct node* rlink;
}node;
node* init(int x){
int i;
node *head,*p,*q;
head=(node*)malloc(sizeof(node));
head->llink=NULL;
q=head;
while(x--){
p=(node*)malloc(sizeof(node));
scanf("%d",&(p->num));
q->rlink=p;
p->llink=q;
q=p;
}
q->rlink=NULL;
return head;
}
node* dele(node* x,node* y,node *z){
node *p,*q,*u,*r,*t,*head;
head=x;
p=x->rlink;
q=y->rlink;
u=z->rlink;
while(q){
while(u){
while(p){
if((p->num)==(q->num)){
if(p->rlink==NULL){
r=p->llink;
r->rlink=NULL;
p=r;
}
else if(p->llink==NULL){
p=p->rlink;
head->rlink=p;
}
else{
r=p->llink;
t=p->rlink;
r->rlink=t;
t->llink=r;
p=t;
}
}
p=p->rlink;
}
u=u->rlink;
p=head->rlink;
}
p=head->rlink;
u=z->rlink;
q=q->rlink;
}
return head;
}
node* output(node *p){
node *q;
q=p->rlink;
printf("%d",(q->num));
q=q->rlink;
while(q){
printf(" %d",(q->num));
q=q->rlink;
}
printf("\n");
}
int main(){
int m,n,s;
node *head1,*head2,*head3;
scanf("%d%d%d",&m,&n,&s);
head1=init(m);
head2=init(n);
head3=init(s);
head1=dele(head1,head2,head3);
output(head1);
}
左右不分真的好可怕~
复习:
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int data;
struct Node *llink;
struct Node *rlink;
}Node,*PNode;
void init(PNode head, int m){
int i;
PNode p, q;
head->llink = NULL;
p = head;
for(i = 0; i < m; i++){
q = (PNode)malloc(sizeof(Node));
scanf("%d", &q->data);
q->llink = p;
p->rlink = q;
p = q;
}
p->rlink = NULL;
}
void dele(PNode head1, PNode head2, PNode head3){
PNode p, q, g, t, r;
p = head1->rlink;
q = head2->rlink;
g = head3->rlink;
while(q){
while(g){
if(q->data == g->data){
while(p){
if(p->data == q->data){
if(p->rlink == NULL){
t = p->llink;
t->rlink = NULL;
p = t;
}
else if(p->llink == NULL){
t = p->rlink;
head1->rlink = t;
p = t;
}
else{
t = p->llink;
r = p->rlink;
t->rlink = r;
r->llink = t;
p = r;
}
}
p = p->rlink;
}
}
g = g->rlink;
p = head1->rlink;
}
q = q->rlink;
g = head3->rlink;
p = head1->rlink;
}
}
void output(PNode head){
PNode p;
p = head->rlink;
while(p){
printf("%d ", p->data);
p = p->rlink;
}
}
int main(){
int x, y, z;
PNode head1, head2, head3;
head1 = (PNode)malloc(sizeof(Node));
head2 = (PNode)malloc(sizeof(Node));
head3 = (PNode)malloc(sizeof(Node));
scanf("%d%d%d", &x, &y, &z);
init(head1, x);
init(head2, y);
init(head3, z);
dele(head1, head2, head3);
output(head1);
return 0;
}