当前位置:C++技术网 > 资讯 > 数据结构中单链表求集合数组的交集

数据结构中单链表求集合数组的交集

更新时间:2016-04-07 22:32:54浏览次数:1+次

我们新建一个头文件,将单链表应有的操作写在盖头文件命名为LinkList.h里面:

#include "stdlib.h"
#include "stdio.h"

typedef int DataType;

typedef struct Node{
	DataType data;
	struct Node *next;
}ListNode, *LinkList;

void InitList(LinkList *head)//初始化链表
{
	if((*head=(LinkList)malloc(sizeof(ListNode)))==NULL)
	{
		exit(-1);
	}
	(*head)->next=NULL;
}


int ListEmpty(LinkList head)
{
	if(head->next==NULL)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}


ListNode* Get(LinkList head, int i)
{
	ListNode *p;
	int j;
	if(ListEmpty(head))
	{
		return NULL;
	}
	if(i<1)
	{
		return NULL;
	}
	j=0;
	p=head;
	while(p->next!=NULL &&j<i)
	{
		p=p->next;
		j++;
	}
	if(j==i)
	{
		return p;
	}
	else
	{
		return NULL;
	}
}


ListNode* LocateElem(LinkList head, DataType e)
{
	ListNode *p;
	p=head->next;
	while(p)
	{
		if(p->data!=e)
		{
			p=p->next;
		}
		else
		{
			break;
		}
	}
	return p;
}


int LocatePos(LinkList head,DataType e)
{
	ListNode *p;
	int j;
	if(ListEmpty(head))
	{
		return 0;
	}
	p=head->next;
	j=1;
	while(p)
	{
		if(p->data==e)
		{
			return j;
		}
		else
		{
			p=p->next;
			j++;
		}
	}
	if(!p)
	{
		return 0;
	}
}


int InsertList(LinkList head,int i,int e)
{
	ListNode *p,*pre;
	int j=0;
	pre=head;


	while (pre->next!=NULL && j<i-1)//找到第i-1个结点
	{
		pre=pre->next;
		j++;
	}

	if(j!=i-1)
	{
		printf("插入位置出错!\n");
		return 0;
	}

	if((p=(ListNode *)malloc(sizeof(ListNode)))==NULL)
	{
		exit(-1);
	}
	p->data=e;
	p->next=pre->next;
	pre->next=p;
	return 1;
}


int DeleteList(LinkList head, int i, DataType *e)
{
	ListNode *pre,*p;
	int j;
	pre=head;
	j=0;
	while(pre->next!=NULL&&pre->next->next!=NULL&&j<i-1)
	{
		pre=pre->next;
		j++;
	}
	if(j!=i-1)
	{
		printf("删除位置错误!\n");
	}
	p=pre->next;
	*e=p->data;
	pre->next=p->next;
	free(p);
	return 1;
}


int ListLength(LinkList head)
{
	ListNode *p;
	int count=0;
	p=head;
	while(p->next!=NULL)
	{
		p=p->next;
		count++;
	}
	return count;
}
然后就是实现文件:
#include "windows.h"
#include "stdio.h"
#include "stdlib.h"

#include "LinkList.h"

void DelElem(LinkList A, LinkList B)
{
	int i,pos;
	DataType e;
	ListNode *p;
	for(i=1; i<=ListLength(B); i++)
	{
		p=Get(B,i);
		if(p)
		{
			pos=LocatePos(A,p->data);
			if(pos>0)
			{
				DeleteList(A,pos,&e);
			}
		}
	}
}
int main()
{
	int i;
    DataType a[]={2,3,6,7,9,14,56,45,65,67};
	DataType b[]={3,4,7,11,34,54,45,67};

	LinkList A,B;
	ListNode *p;

	InitList(&A);
	InitList(&B);

	for(i=1; i<=sizeof(a)/sizeof(int); i++)
	{
		if(InsertList(A,i,a[i-1])==0)
		{
			printf("位置不合法!\n");
			return 0;
		}
	}

	for(i=1; i<=sizeof(b)/sizeof(int); i++)
	{
		if(InsertList(B,i,b[i-1])==0)
		{
			printf("位置不合法!\n");
			return 0;
		}
	}

	printf("单链表A中有%d个元素\n",ListLength(A));
	for(i=1; i<=ListLength(A); i++)
	{
		p=Get(A,i);
		if(p)
		{
			printf("%4d",p->data);
		}
	}
	printf("\n");

	printf("单链表B中有%d个元素\n",ListLength(B));
	for(i=1; i<=ListLength(B); i++)
	{
		p=Get(B,i);
		if(p)
		{
			printf("%4d",p->data);
		}
	}
	printf("\n");

	DelElem(A,B);
	printf("将在A中出现B的元素删除后(A-B),现在A中还剩下%d个元素:\n",ListLength(A));

	for(i=1; i<=ListLength(A); i++)
	{
		p=Get(A,i);
		if(p)
		{
			printf("%4d",p->data);
		}
	}
	printf("\n");

	system("pause");
	return 0;
}
实现: