数据结构05——单链表的删除

题目:




#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);
}

猜你喜欢

转载自blog.csdn.net/chengchencheng/article/details/79717873