C语言 两个有序链表序列的交集

这个算法的思路是两个指针分别指向两个链表头,然后才头到尾遍历,复杂度是O(min(Length(a),Length(b)),下面给出实现代码,是用纯C写的链表实现,注意代码中直接采用尾插法最后一个数据会超时,因为链表尾插的时间复杂度是O(N),解决的办法是创建一个尾节点

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

#define ERROR NULL
typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode {
	ElementType Data;
	PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;

List MakeEmpty();
Position Find(List L, ElementType X);
bool Insert(List L, ElementType X, Position P);
bool Delete(List L, Position P);
void InsertTail(List L, ElementType X);

int main()
{
	List L1 = MakeEmpty();
	List L2 = MakeEmpty();
	List L3 = MakeEmpty();
	int temp;
	while (1)
	{
		scanf_s("%d", &temp);
		if (temp == -1)
			break;
		InsertTail(L1, temp);
	}
	while (1)
	{
		scanf_s("%d", &temp);
		if (temp == -1)
			break;
		InsertTail(L2, temp);
	}
	PtrToLNode p, q;
	p = L1->Next;
	q = L2->Next;
	while (p&&q)
	{
		if (p->Data == q->Data)
		{
			InsertTail(L3, p->Data);
			p = p->Next;
			q = q->Next;
		}
		else if (p->Data > q->Data)
		{
			q = q->Next;
		}
		else if (p->Data < q->Data)
		{
			p = p->Next;
		}
	}
	p = L3->Next;
	if (p == NULL)
		printf("NULL\n");
	else
	{
		printf("%d", p->Data);
		while (p)
		{
			p = p->Next;
			if(p)
				printf(" %d", p->Data);
		}
	}
		
}

List MakeEmpty()
{
	List L = (List)malloc(sizeof(struct LNode));
	L->Next = NULL;
	return L;
}

Position Find(List L, ElementType X)
{
	PtrToLNode p;
	p = L->Next;
	while (p)
	{
		if (p->Data == X)
			return p;

		p = p->Next;
	}
	return p;
}

bool Insert(List L, ElementType X, Position P)
{
	PtrToLNode p, temp;
	p = L;
	while (p&&p->Next != P)
	{
		p = p->Next;
	}
	if (p == NULL)
	{
		printf("Wrong Position for Insertion\n");
		return false;
	}

	temp = (PtrToLNode)malloc(sizeof(struct LNode));
	temp->Data = X;
	temp->Next = p->Next;
	p->Next = temp;
	return true;
}

bool Delete(List L, Position P)
{
	PtrToLNode p, temp;
	p = L;
	while (p&&p->Next != P)
	{
		p = p->Next;
	}
	if (p == NULL)
	{
		printf("Wrong Position for Deletion\n");
		return false;
	}

	temp = p->Next;
	p->Next = temp->Next;
	free(temp);
	return true;
}

void InsertTail(List L, ElementType X)
{
	PtrToLNode p, temp;
	p = L;
	while (p->Next)
	{
		p = p->Next;
	}
	temp = (PtrToLNode)malloc(sizeof(struct LNode));
	temp->Data = X;
	temp->Next = NULL;
	p->Next = temp;
}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用 C 语言实现两个有序链表序列交集的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表结构体 struct ListNode { int val; struct ListNode *next; }; // 创建链表节点 struct ListNode* createNode(int val) { struct ListNode* node = (struct ListNode*) malloc(sizeof(struct ListNode)); node->val = val; node->next = NULL; return node; } // 向链表尾部添加元素 void addNode(struct ListNode** head, int val) { struct ListNode* node = createNode(val); if (*head == NULL) { *head = node; } else { struct ListNode* curr = *head; while (curr->next != NULL) { curr = curr->next; } curr->next = node; } } // 打印链表 void printList(struct ListNode* head) { struct ListNode* curr = head; while (curr != NULL) { printf("%d ", curr->val); curr = curr->next; } printf("\n"); } // 计算两个有序链表交集 struct ListNode* getIntersection(struct ListNode* l1, struct ListNode* l2) { struct ListNode* dummy = createNode(-1); struct ListNode* curr = dummy; while (l1 != NULL && l2 != NULL) { if (l1->val == l2->val) { addNode(&curr, l1->val); l1 = l1->next; l2 = l2->next; } else if (l1->val < l2->val) { l1 = l1->next; } else { l2 = l2->next; } } return dummy->next; } int main() { // 创建第一个有序链表 struct ListNode* l1 = NULL; addNode(&l1, 1); addNode(&l1, 2); addNode(&l1, 3); addNode(&l1, 4); addNode(&l1, 5); printf("List 1: "); printList(l1); // 创建第二个有序链表 struct ListNode* l2 = NULL; addNode(&l2, 3); addNode(&l2, 4); addNode(&l2, 5); addNode(&l2, 6); addNode(&l2, 7); printf("List 2: "); printList(l2); // 计算两个有序链表交集 struct ListNode* intersection = getIntersection(l1, l2); printf("Intersection: "); printList(intersection); return 0; } ``` 输出结果: ``` List 1: 1 2 3 4 5 List 2: 3 4 5 6 7 Intersection: 3 4 5 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值