一步一步复习数据结构和算法基础-链表(3)

先前复习了链表的基本操作之后,就想对链表的应用进行扩展,

先前链表的基本操作时这些扩展的基础,自己复习一下先

基本函数:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define true 1
#define false 0

typedef struct node
{
	int data;
	struct node *next;
}list,*linklist;

linklist InitList(linklist plist);
linklist CreatListWithHead(linklist plist);
void InsertListAtFront(linklist plist,int number,int goal);
void InsertListAtBack(linklist plist,int number,int goal);
void DeleteListAtFront(linklist plist,int number);
void DeleteListAtBack(linklist plist,int number);
void DeleteListAtPresent(linklist plist,int number);
int FindANumberInList(linklist plist,int number);


/*创建有头结点的链表*/
//creat a list without head and print in order
linklist CreatListWithHead(linklist plist)
{
	int number;
	linklist p;
	linklist temp = plist;

	/*输入数字到文件结尾 EOF(ctrl+z)*/
	while(scanf("%d",&number) != EOF)
	{
		p = (linklist)malloc(sizeof(list));
		if(!p){printf("malloc error.\n");exit(1);}

		p->data = number;
		temp->next = p;
		temp = p;
	}
	temp->next = NULL;
	return plist;
}

/*打印链表(有头结点)*/
void PrintListWithHead(linklist plist)
{
	linklist temp = plist->next;
	while(temp)
	{
		printf("%d ",temp->data);
		temp = temp->next;
	}
}

/*在链表中插入一个元素(指定位置的前面)*/
void InsertListAtFront(linklist plist,int number,int goal)
{
	linklist p = plist->next;
	linklist q = plist;
	linklist temp;
	while(p)
	{
		if(p->data == number)break;
		p = p->next;
		q = q->next;
	}
	if(p == NULL){printf("number does not exist.\n");exit(1);}
	temp = (linklist)malloc(sizeof(list));
	temp->data = goal;
	temp->next = q->next;
	q->next = temp;
}

/*在链表中插入一个元素(指定位置的后面)*/
void InsertListAtBack(linklist plist,int number,int goal)
{
	linklist p = plist->next;
	linklist temp;
	while(p)
	{
		if(p->data == number)break;
		p = p->next;
	}
	if(p == NULL){printf("number does not exist.\n");exit(1);}
	temp = (linklist)malloc(sizeof(list));
	temp->data = goal;
	temp->next = p->next;
	p->next = temp;
}

/*删除指定位置的节点*/
void DeleteListAtPresent(linklist plist,int number)
{
	linklist p = plist->next;linklist q=plist;
	linklist temp;
	while(p)
	{
		if(p->data == number)break;
		p = p->next;
		q = q->next;
	}
	if(plist->next == p){printf("you can not delete the head of list.

\n");exit(1);}
	if(p == NULL){printf("postion dose not exist.\n");exit(1);}
	temp = (linklist)malloc(sizeof(list));
	temp->data = p->data;
	temp->next = p->next;
	q->next = temp->next;
	free(temp);
}

/*删除指定位置后面的节点*/
void DeleteListAtBack(linklist plist,int number)
{
	linklist p = plist->next;
	linklist temp;
	while(p)
	{
		if(p->data == number)break;
		p = p->next;
	}
	if(p->next == NULL){printf("this is the end of list.\n");exit(1);}
	if(p == NULL){printf("postion dose not exist.\n");exit(1);}
	temp = (linklist)malloc(sizeof(list));
	temp->data = p->next->data;
	temp->next = p->next->next;
	p->next = temp->next;
	free(temp);
}

/*在链表之中查询指定的节点*/
int FindANumberInList(linklist plist,int number)
{
	linklist p = plist->next;
	while(p)
	{
		if(p->data == number)
			return 1;
		p = p->next;
	}
	return 0;
}


 

有了这些基本操作更加复杂的操作也便于实现

1: 给出链表 A B , 求 A∪B

逻辑上理解不是很困难,自己的想法也很简单,以链表A作为基准,对于链表B中的每一个元素查看在A中是否有相同的元素,没有就将该元素

插入到链表A的尾部:

/*合并两个链表*/
linklist UnionList(linklist pa,linklist pb,linklist pc)
{
	linklist templist1 = pa->next,templist2 = pb->next,pd;
	pc = pa;
	pd = pa;
	while(pc->next)
	{
		pc = pc->next;
	}
	pb = pb->next;
	while(pb)
	{
		if(FindANumberInList(pa,pb->data) == 0)
		{
			InsertListAtBack(pd,pc->data,pb->data);
			pc = pc->next;
			pd = pa;
		}
		pb = pb->next;
	}
	return pa;
}


 2:求链表 A B交集:

 

/*链表交集*/
linklist MergeList(linklist pa,linklist pb)
{
	linklist listtemp = pb->next;
	linklist pc,pctemp,p;

	pc = (linklist)malloc(sizeof(list));
	pc->next = NULL;
	pctemp = pc;

	while(listtemp)
	{
		if(FindANumberInList(pa,listtemp->data) == 1)
		{
			p = (linklist)malloc(sizeof(list));
			p->data = listtemp->data;p->next = NULL;
			if(pctemp->next == NULL){pctemp->next = p;pctemp = p;} 
			else
			{
				InsertListAtBack(pc,pctemp->data,listtemp->data);
				pctemp = pctemp->next;
			}
		}
		listtemp = listtemp->next;
	}
	return pc;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值