⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 b.c

📁 用 2个链表 实现 链表 A 和链表 B相减并清楚重复内容的程序
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
	int data;
	struct node *next;
};

void initx(struct node *h,int *a,int n);
void AsubB(struct node *HA,struct node *HB,struct node *HC);
void display(struct node *Head);

void main()
{
	struct node *HeadA=(struct node *)malloc(sizeof(struct node));
	struct node *HeadB=(struct node *)malloc(sizeof(struct node));
	struct node *HeadC=(struct node *)malloc(sizeof(struct node));

	int A[]={12,23,18,15,12,13,20,23,13,27,5,3,5,23,73};
	int B[]={14,12,26,23,14,15,12,26,23,56,4,2,7,324,8,34};

	HeadA->next=NULL;
	HeadB->next=NULL;
	HeadC->next=NULL;

	printf("A:  ");
	initx(HeadA,A,sizeof(A)/sizeof(int));
	printf("B:  ");
	initx(HeadB,B,sizeof(B)/sizeof(int));

//	display(HeadA);
//	display(HeadB);

	AsubB(HeadA,HeadB,HeadC);
	printf("A-B:");
	display(HeadC);
}

void initx(struct node *h,int *a,int n)    //制作链表并化简
{
	struct node *p=h;
	struct node *q=h;
	struct node *ql=NULL;
	int i;
	for(i=0;i<n;i++)
	{
		struct node *nd=(struct node *)malloc(sizeof(struct node));
		nd->data=*(a+i);		
		nd->next=NULL;
		p->next=nd;
		p=nd;
	}
	p=h->next;
	display(h);
	while(p!=NULL)                         //化简
	{
		ql=p;
		q=ql->next;
		while(q!=NULL)
		{
			if(p->data==q->data)
			{
				ql->next=q->next;
				free(q);
				q=ql->next;
			}
			else
			{
				ql=ql->next;
				q=ql->next;
			}
		}
		p=p->next;
	}

}

void AsubB(struct node *HA,struct node *HB,struct node *HC)  //A-B=C
{
	struct node *a=HA->next;
	struct node *b=HB->next;
	struct node *c=HC;
	int flag=0;
	
	while(a!=NULL)
	{
		while(b!=NULL)
		{
			if(a->data==b->data)
			{
				flag=1;
				break;
			}
			b=b->next;
		}
		if(flag==0)
		{
			c->next=a;
			c=c->next;
		}
		a=a->next;
		flag=0;
	}
}

void display(struct node *Head)                           //显示
{
	struct node *p=Head->next;
	while(p!=NULL)
	{
		printf("%d  ",p->data);
		p=p->next;
	}
	printf("\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -