链表的插入删除,交集

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef struct node  {
	int Nnember;
	struct node *pNext;
} Node,*pNode;

pNode pHead = NULL;
int data;
int num;
int choose;
int return_val;
int return_find;

//创建链表
//head为头结点 
pNode CreateList() {
	int i;
	int len;
	int val;
	pNode pHead = (pNode)malloc(sizeof(Node));
	pNode pTail = pHead;
	pTail->pNext = NULL;
	printf("请输入节点个数:");
	scanf("%d",&len);
	for(i = 1; i <= len; i++) {
		printf("第 %d 个节点的数值:",i);
		scanf("%d",&val);
		pNode pNew = (pNode)malloc(sizeof(Node));
		pNew->Nnember = val;
		pTail->pNext = pNew;
		pNew->pNext = NULL;
		pTail = pNew;
	}
	return pHead;
}

//查找number在链表中的位置 
int Find(pNode pHead, int number) {
	pNode p=pHead->pNext;
	int i=1;
	while(p!=NULL) {
		if(p->Nnember==number) {
			return i;
		}
		i++;
		p=p->pNext;
	}
	return 0;
}

//寻找两个集合的交集 
void Find_intersection(pNode pHead) {
	pNode pHead_t=NULL;
	pHead_t=CreateList();
	pNode p=pHead->pNext;
	int number;
	int i;
	int j=0;
	while(p!=NULL) {
		pNode tail=pHead_t;
		number=p->Nnember;
		i=Find(tail,number);
		if(i!=0) {
			if(j==0) {
				printf("交集为:");
			}
			printf("%d\t",number);
			j++;
		}
		p=p->pNext;
	}
	printf("\n");
	if(j==0)printf("没有交集\n");
}

//遍历链表 
void TraversaList(pNode pHead) {
	pNode p = pHead->pNext;
	while(NULL != p) {
		printf("%d ",p->Nnember);
		p = p->pNext;
	}
	printf("\n");
	return ;
}

//插入元素 
bool Insert(pNode pHead, int front,int data) {
	int i = 0;
	pNode _node = pHead;
	pNode pSwap;
	if ((front < 1) && (NULL != _node)) {
		return false;
	}
	while (i < front - 1) {
		_node = _node->pNext;
		++i;
	}
	pNode pNew = (pNode)malloc(sizeof(Node));

	pNew->Nnember = data;
	pSwap = _node->pNext;
	_node->pNext = pNew;
	pNew->pNext = pSwap;
	return true;
}

//删除元素 
int Delete(pNode pHead,int back) {
	int i = 0;
	int data;
	pNode _node = pHead;
	pNode pSwap;
	if ((back < 1) && (NULL == _node->pNext)) {
		printf("删除失败!\n");
		return 0;
	}
	while(i < back-1) {
		_node = _node->pNext;
		++i;
	}
	pSwap = _node->pNext;
	data = pSwap->Nnember;
	_node->pNext = _node->pNext->pNext;
	free(pSwap);
	return data;
}

//选择操作 
void Select() {
	printf("选择你的操作:\n");
	printf("1.插入数据      2.删除数据     3.按值查找    4.寻找交集   5.退出\n");
	printf("请输入:");
	scanf("%d",&choose);
	switch (choose) {
		case 1: {
			printf("请输入要在第几个节点前插入数据:");
			scanf("%d",&num);
			printf("请输入要插入的数据:");
			scanf("%d",&data);
			if(Insert(pHead,num,data) == true) {
				printf("插入成功\n插入后的数据是:\n");
				TraversaList(pHead);
			} else {
				printf("插入失败\n");
			}
			printf("操作完成后的数据是:");
			TraversaList(pHead);
			Select();
			break;
		}
		case 2: {
			printf("请输入要删除第几个节点的数据:");
			scanf("%d",&num);
			return_val = Delete(pHead,num);
			if (return_val == 0) {
				printf("删除失败。\n");
			} else {
				printf("删除成功。删除的元素是:%d\n",return_val);
			}
			printf("操作完成后的数据是:");
			TraversaList(pHead);
			Select();
			break;
		}
		case 3: {
			printf("请输入你要查找的值:");
			scanf("%d",&num);
			return_find=Find(pHead,num);
			if(return_find!=0) {
				printf("查找成功。元素在%d个节点\n",return_find);
			} else {
				printf("查找失败。没有这个元素\n");
			}
			Select();
			break;
		}
		case 4: {
			Find_intersection(pHead);
			Select();
			break;
		}
		case 5: {
			exit(0);
			break;
		}
	}

}

int main() {
	pHead = CreateList();
	printf("你输入的数据为:");
	TraversaList(pHead);
	Select();
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值