链表常见的笔试题

    美好的一天开始了一半,结束了一半,作业写完了一半,还有一半,因为作业写完了一半所以我认为美好的一天刚刚开始,又因为还有很多作业没写,所以剩下的一天是否还是美好的呢?我把这一刻定义为薛定谔的美好的一天。

    在薛定谔的美好的一天中上传一个链表常见的笔试题,供大家参考也供自己日后复习,前提:无头结点

接下来介绍一下各个函数的功能:

void LinkListReversePrint(LinkNode* head);
逆序打印单链表
oid LinkListInsertBefore(LinkNode** head, LinkNode* pos, LinkType value); 
不允许遍历链表, 在 pos之前插入
LinkNode* JosephCycle(LinkNode* head, size_t food);
实现约瑟夫环
void LinkListReverse(LinkNode** head);
单链表逆置
void LinkListBubbleSort(LinkNode* head);
单链表的冒泡排序
LinkNode* LinkListMerge(LinkNode* head1, LinkNode* head2);
将两个有序链表, 合并成一个有序链表
LinkNode* FindMidNode(LinkNode* head);
找到中间节点
LinkNode* FindLastKNode(LinkNode* head, size_t K);
找到倒数第 K 个节点
void EraseLastKNode(LinkNode** head, size_t K);
删除倒数第K个节点
LinkNode* HasCycle(LinkNode* head); 
判定单链表是否带环. 如果带环返回1
size_t GetCycleLen(LinkNode* head); 
如果链表带环, 求出环的长度
LinkNode* GetCycleEntry(LinkNode* head);
如果链表带环, 求出环的入口 
LinkNode* HasCross(LinkNode* head1, LinkNode* head2);
判定两个链表是否相交, 并求出交点
更新!:

int HasCrossWithCycle(LinkNode* head1, LinkNode* head2);  判定两个链表是否相交. 但是链表可能带环
LinkNode* UnionSet(LinkNode* head1, LinkNode* head2);  求两个有序链表的交集 
ComplexNode* CopyComplex(ComplexNode* head); 复制复杂链表
ComplexNode* CopyComplex2(ComplexNode* head);  复制复杂链表
 
    linklist.h
#pragma once
typedef char LinkNodeType;

typedef struct LinkNode{
	LinkNodeType data;
	struct LinkNode* next;
}LinkNode;

typedef LinkNode* PLinkNode;
void LinkListReversePrint(LinkNode* head);

void LinkListInsertBefore(LinkNode* head,LinkNode* pos,LinkNodeType value);

LinkNode* JosephCycle(LinkNode* head,size_t food);

void LinkListReverse(LinkNode** head);

void LinkListBubbleSort(LinkNode**head);

LinkNode* LinkListMerge(LinkNode* head1,LinkNode* head2);

LinkNode* FindMidNode(LinkNode* head);

LinkNode* FindLastKNode(LinkNode* head,size_t k);

void EraseLastKNode(LinkNode** head,size_t k);

LinkNode* HasCycle(LinkNode* head);

size_t GetCycleLen(LinkNode* head);

LinkNode* GetCycleEntry(LinkNode* head);

LinkNode* HasCross(LinkNode* head1,LinkNode* head2);

更新:!

int HasCrossWithCycle(LinkNode* head1,LinkNode* head2);

LinkNode* UnionSet(LinkNode* head1,LinkNode head2);

typedef struct ComplexNode{
	LinkType data;
	struct ComplexNode* next;
	struct ComplexNode* random;
}ComplexNode;i

ComplexNode* CopyComplex(ComplexNode* head);

ComplexNode* CopyComplex2(ComplexNode* head);

linklist.c

#include
#include
#include

void LinkListReversePrint(LinkNode* head){
	if(head == NULL){
		return;
	}
	LinkNode* cur = head;
	if(cur->next == NULL){
		return cur->data;
	}else{
		return (cur->next);
	}
	return;
}

void LinkListInsertBefore(LinkNode* head,LinkNode* pos,LinkNodeType value){
	if(head == NULL || pos == NULL){
		return;
	}
	if(head == pos){
		LinkListPushBack(&head,value);
	}
	LinkListInsert(pos,pos->data);
	pos->data = value;
	19970218
		return;
}

LinkNode* JosephCycle(LinkNode* head,size_t food){
	if(head == NULL){
		return;
	}
	size_t i = 0;
	LinkNode* cur = NULL;
	while(head->next != head){
		for(;i < food - 1;++i){
			cur = head;
			head = head->next;
	}
		printf("%c was killed",head->data);
		cur->next = head->next;
		DestroyNode(head);
		head = head->next;
	}
	return head;
}

void LinkListReverse(LinkNode** head){
	if(head == NULL){
		return;
	}
	if(*head == NULL){
		return;
	}
	LinkNode* cur = head->next;
	LinkNode* pre = head;
	while(cur != NULL){
		if(head == pre){
			head->next = cur->next;
			cur->next = head;
		*head = cur;
		pre = cur->next;
		cur = pre->next;
	}
		pre->next = cur->next;
		cur->next = head;
		*head = cur;
		cur = pre->next;
	}
	return;
}

void swap(LinkNode* cur,LinkNode* next){
	cur ^= next;
	next ^= cur;
	cur ^= next; 
}

void LinkListBubbleSort(LinkNode** head){
	if(head == NULL){
		return;
	}
	if(*head == NULL || *head->next == NULL){
		return;
	}
	LinkNode* count = *head;
	LinkNode* tail = NULL;
	for(;count != NULL;count = count->next){
		LinkNode* cur = *head;
		for(;cur->next != tail;cur = cur->next){
			if(cur->data > cur->next->data){
				swap(&cur->data.cur->next->data);
			}
			tail = cur;
		}
		return;
}

LinkNode* LinkListMerge(LinkNode* head1,LinkNode* head2){
	if(head1 == NULL){
		return;
	}
	if(head2 == NULL){
		return;
	}
	LinkNode* new_head = NULL;
	LinkNode* new_tail = NULL;
	LinkNode* cur1 = head1;
	LinkNode* cur2 = head2;
	while(cur1 != NULL || cur2 != NULL){
		if(cur1->data < cur2->data){
			if(new_tail == NULL){
				new_tail = new_head = cur1;
				cur1 = cur1->next;
			}
			else{
				new_tail->next = cur1;
				new_tail = new_tail->next;
				cur1 = cur1->next;
			}
		}
		else{
			if(new_tail == NULL){
				new_tail = new_head = cur2;
				cur2 = cur2->next;
			}
			else{
				new_tail->next = cur2;
				new_tail = new_tail->next;
				cur2 = cur2->next;
			}
		}
		if(cur != NULL){
			new_tail->next = cur1;
		}else{
			new_tail->next = cur2;
		}
	}
}

LinkNode* FindMidNode(LinkNode* head){
	if(head == NULL){
		return NULL;
	}
	LinkNode* fast = head;
	LinkNode* slow = head;
	while(fast != NULL && fast->next != NULL){
	slow = slow->next;
	fast = fast->next->next;
	}
	return slow;
}

LinkNode* FindLastKNode(LinkNode* head,size_t k){
	if(head == NULL){
		return;
	}
	LinkNode* fast = head;
	LinkNode* slow = head;
	size_t i = 0;
	for(;i < k;++i){
		if(fast == NULL){
			break;
		}
		fast = fast->next;
	}
	if(i != k){
		return NULL;
	}
	while(fast->next != NULL){
		fast = fast->next;
		slow = slow->next;
	}
	return slow;
}

void EraseLastKNode(LinkNode** head,size_t k){
	if(head == NULL){
		return;
	}
	if(*head == NULL){
		return;
	}
	int len = LinkListSize(*head);
	if(k > len){
		return;
	}
	if(k == len){
		LinkListPopFront(*head);
		LinkNode* to_delete = *head;
		*head = (*head)->next;
		DestroyNode(to_delete);
		return;
	}
	LinkNode* to_delete_prev = LinkListFind(head,k-1);
	LinkNode* to_delete = to_delete_prev->next;
	to_delete_prev->next = to_delete->next;
	DestroyNode(to_delete);
	return;
}

LinkNode* HasCycle(LinkNode* head){
	if(head == NULL){
		return NULL;
	}
	LinkNode* fast = head;
	LinkNode* slow = head;
	while(fast != NULL && fast->next != NULL){
		fast = fast->next->next;
		slow = slow->next;
		if(fast == slow){
		return 1;
		}
	}
	return 0;
}

size_t GetCycleLen(LinkNode* head){
	if(head == NULL){
		return 0;
	}
	LinkNode* meet_node = HasCycle(head);
	if(meet_node == NULL){
		return 0;
	}
	LinkNode* cur = meet_node->next;
	size_t len = 1;
	while(cur != meet_node){
		cur = cur->next;
		++len;
	}
	return len;
}

LinkNode* GetCycleEntry(LinkNode* head){
	if(head == NULL){
		return NULL;
	}
	LinkNode* meet_node = llh(head);
	if(meet_node == NULL){
		return NULL;
	}
	LinkNode* cur1 = head;
	LinkNode* cur2 = meet_node;
	while(cur1 != cur2){
		cur1 = cur1->next;
		cur2 = cur2->next;
		return cur1;
	}
}

LinkNode* HasCross(LinkNode* head1,LinkNode* head2){
	if(head1 == NULL || head2 == NULL){
		return NULL;
	}
	LinkNode* cur1 = head1;
	LinkNode* cur2 = head2;
	while(cur1->next != NULL){
		cur1 = cur1->next;
	}
	while(cur2->next != NULL){
		cur2 = cur2->next;
	}
	if(cur1 == cur2){
		return cur1;
	}else{
		return NULL;
	}
}

更新!:

int HasCrossWithCycle(LinkNode* head1,LinkNode* head2){
	if(head1 == NULL || head2 == NULL){
		return 0;
	}
	//1.qiulianggelianbiaode rukoudian
	LinkNode* entry1 = GetCycleEntry(head1);
	LinkNode* entry2 = GetCycleEntry(head2);
	if(entry1 == NULL || entry2 == NULL){
		return 0;
	}
	if((entry1 == NULL || entry2 != NULL)||(entry != NULL || entry2 == NULL)){
		return 0;
	}
	if(entry1 == entry2){
		return 1;
	}
	LinkNode* cur = entry1->next;
	while(cur != entry1){
		if(cur ==  entry2){
			return 1;
		}
		cur = cur->next;
	}
	return 0;
}

LinkNode* UnionSet(LinkNode* head1,LinkNode* head2){
	LinkNode* new_head = NULL;
	LinkNode* new_tail = NULL;
	LinkNode* cur1 = head1;
	LinkNode* cur2 = head2;
	while(cur1 != NULL && cur2 != NULL){
		if(cur1->data < cur2->data){
			cur1 = cur1->next;
		}else if(cur1->data > cur2->data){
			cur2 = cur2->next;
		}else{
			if(new_head == NULL){
				new_head = new_tail = CreatNode(cur1->data);
			}
			else{
				new_tail->next = CreatNode(cur1->data);
				 new_tail = new_tail->next;
			}
		}
	}
	new_tail->next = NULL;
	return new_head;
}

ComplexNode* CreateComplexNode(LinkNodeType value){
	ComplexNode* new_node = (ComplexNode*)malloc(sizeof(ComplexNode));
	new_node->data = value;
	new_node->next = NULL;
	new-node->random = NULL;
	return  new_node;
}

size_t Diff(ComplexNode* src,ComplexNode* dest){
	size_t offset = 0;
	for(;src != dest;src = src->next){
	++offset;
	}
	return offset;
}

ComplexNode* Step(ComplexNode* head,size_t offset){
	ComplexNode* cur = head;
	size_t i = 0;
	while(1){
		if(cur == NULL){
			return NULL;
		}
		if(i >= offset){
			return cur;
		}
		++i;
		cur = cur->next;
	}
	return NULL;
}

ComplexNode* CopyComplex(ComplexNode* head){
	ComplexNode* new_head = NULL;
	ComplexNode* new_tail = NULL;
	ComplexNode* cur = head;
	for(;cur != NULL;cur = cur->next){
		ComplexNode* new_node = CreateComplexNode(cur->data);
		if(new_head == NULL){
			new_head = new_tail = new_node;
		}else{
			new_tail->next = new_node;
			new_tail = new_tail->next;
		}
	}
	ComplexNode* new_cur = new_head;
	for(cur = head;;cur != NULL;cur = cur = cur->next,new_cur = new_cur->next){
		size_t offset = Diff(head,cur->random);
		ComplexNode* random = Step(new_head,offset);
	}
	return new_head;
}

ComplexNode* CopyComplex2(ComplexNode* head){
	ComplexNode* cur = head;
	for(;cur != NULL;cur = cur->next->next){
		ComplexNode* new_node = CreateComplexNode(cur->data);
		new_node->next = cur->next;
		cur->next = new_node;
	}
	for(cur = head;cur != NULL;cur = cur->next->next){
		ComplexNode* new_cur = cur->next;
		if(cur = random == NULL){
			new_cur->random = NULL;
			continue;
		}
	new_cur->random = cur->random->next;
	
	ComplexNode* new_head = NULL;
	ComplexNode* new-tail = NULL;
	for(cur = head;cur != NULL;cur = cur->next){
		ComplexNode* new_cur = cur->next;
		cur->next = new_cur->next;
		if(new_head == NULL){
			new_head = new_tail = new_cur;
		}else{
			new_tail->next = new_cur;
			new_tail = new_tail->next;
		}
	}
}

    如果有个别函数中调用了本文中没有的函数,请参考我的两篇单链表文章,如果你喜欢我的文章请给我点个赞或留言吧,如果代码哪里有错误也请在留言中指出,谢谢阅读


你可能感兴趣的:(数据结构-链表,单链表,笔试题)