链表的详细总结

链表的详细总结

#include"pch.h"
#include <bits/stdc++.h>
typedef int elementType;
typedef struct Node {
	elementType value;
	struct Node *next;
}nodelist;
//创建链表节点
nodelist *newnode(int value) {
	nodelist *node;
	if (!(node = (nodelist*)malloc(sizeof(nodelist))))
		return NULL;
	node->value = value;
	node->next = NULL;
	return node;
}
//头插法
nodelist *headInsert(nodelist*head, int value) {
	nodelist*node=NULL;
	if(!(newnode(value)))
	return NULL;
	if(head)
	node->next = head;
	head = node;
	return head;
}

//尾插节点法
nodelist*tailListInsert(nodelist*head, nodelist*node) {
	nodelist*current = head;
	if (!head)
		head=node;
	else {
		while (current->next)
			current = current->next;
		node->next = NULL;
		current->next = node;
	}
	return head;
}
//尾插值法
nodelist*tailValueInsert(nodelist*head, int value) {
	nodelist*node;
	if (!(node = newnode(value)))
		return NULL;
	return tailListInsert(head, node);
}
//删除特定值的节点
nodelist*deleteNode(nodelist*head, int value) {
	nodelist*current = head;
	while (current->next) {
		current = current->next;
		if (current->next->value == value) {
			nodelist*temp = current->next;
			current->next = current->next->next;
			free(temp);
			break;
		}
	}
	return head;
}
//遍历所有节点
nodelist*printNode(nodelist*head) {
	nodelist*current = head;
	if (!head)
		return NULL;
	else {
		while(current){
			printf_s("%-2d->", current->value);
			current = current->next;
			if (current->next == head)//处理循环的链表
				break;
		}
	}
	return head;
}
//使用数组初始化一个元素,长度为len
nodelist*listCreatArray(int a[]) {
	int len = sizeof(a) / sizeof(a[0]);
	nodelist*head=NULL;
	for (int i = 0; i < len; i++) {
		if (!(head = tailValueInsert(head, a[i])))
			return NULL;
	}
	return head;
}
//求链表长度
int lengthNode(nodelist*head) {
	int sum = 0;
	if (!head)
		return 0;
	else {
		nodelist*current = head;
		while (current) {
			++sum;
			current = current->next;
		}
		return sum;
	}
}
//链表逆序
nodelist*reverseNode(nodelist*head) {
	nodelist*current=head, *newHead=NULL;
	while (current) {
		nodelist*next = current->next;
		current->next = newHead;
		newHead = current;
		current = next;
	}
	return newHead;
}
//链表合并
nodelist*mergeNode(nodelist*a, nodelist*b) {
	nodelist *c=NULL;
	nodelist*tail = c;
	if (!a)
		return b;
	if (!b)
		return a;
	while (a&&b) {
		if (a->value > b->value) {
			tail->next = b;
			tail = b;
			b = b->next;
		}
		if (a->value < b->value) {
			tail->next = a;
			tail = a;
			a = a->next;
		}
	}
	if (a)
		tail->next = a;
	if (b)
		tail->next = b;
	return c;
}
//链表复制
nodelist*copyNode(nodelist*head) {
	nodelist*current = head, *newhead = NULL;
	if (head == NULL || head->next == NULL)
		return head;
	else {
		while (current) {
			nodelist*next = current->next;
			current->next = newhead;
			newhead = current;
			current = next;
		}
		return newhead;
	}
}
//链表相交判断
bool IntersertNode(nodelist*a, nodelist*b) {
	int len = abs(lengthNode(a)-lengthNode(b));
	nodelist*longnode = a, *shortnode = b;
	if (lengthNode(b) > lengthNode(a))
		longnode = b, shortnode = a;
	for (int i = 0; i < len; i++) {
		longnode = longnode->next;
	}
	while (longnode&&shortnode) {
		if (longnode == shortnode) {
			return true;
			break;
		}
		longnode = longnode->next;
		shortnode = shortnode->next;
	}
	return false;
}
//判断链表是否存在环利用Floyd算法
nodelist* ringNode(nodelist*head) {
	nodelist*p, *q;
	p = q = head;
	while (p&&q&&q->next) {
		p = p->next;
		q = q->next->next;
		if (p == q)
			return q;
	}
	return NULL;
}
//求环的起点
nodelist*startNode(nodelist*head) {
	nodelist*meeting ,*current=head;
	if (!(meeting = ringNode(head)))
		return NULL;
	while (meeting&&current) {
		if (meeting == current)
			break;
		current = current->next;
		meeting = meeting->next;
	}
	return current;
}
// 链表模拟加法
nodelist *listEnumarateAdd(nodelist *list1, nodelist *list2)
{
	int carry = 0;
	nodelist *result = NULL;
	while (list1 || list2 || carry) {
		int value = carry;
		if (list1) {
			value += list1->value;
			list1 = list1->next;
		}
		if (list2) {
			value += list2->value;
			list2 = list2->next;
		}

		result = tailValueInsert(result, value % 10);
		carry = (value >= 10 ? 1 : 0);
	}

	return result;
}
//有序单向链表插入结点
nodelist*insertNodeNoRing(nodelist*head, int value) {
	nodelist*current = head;
	nodelist*node = newnode(value);
	if (!head || head->value>value) {
		node->next = head;
		head = node;
	}
	else {
		while (current->next!=NULL&&current->next->value<value)
			current = current->next;
		node->next = current->next->next;
		current->next = node;
	}
	return head;
}
//另外一种方法使用二级指针,从而合并了两种情况
/*
void sortedListAddNodeUnify(nodelist **head, int value)
{
	nodelist *node = newnode(value);
	nodelist **current = head;
	while ((*current) && (*current)->value < value) {
		current = &((*current)->next);
	}
	node->next = *current;
	*current = node;
}
*/
//有序单向循环链表插入结点
nodelist*insertNodeRing(nodelist*head, int value) {
	nodelist*node = newnode(value),*pre=NULL,*current=head;
	do {
		pre = current;
		current = current->next;
		if (value<current->value&&value>pre->value)
			break;
	} while (current != head);
	if (value < head->value || current==head) 
		head = node;
	return head;
}
//输出链表倒数第K个节点
nodelist*findReNode(nodelist*head, int K) {
	nodelist*p, *q;
	p = q = head;
	for (int i = 0; i < K; i++)
		p=p->next;
	while (p&&q) {
		p = p->next;
		q = q->next;
	}
	return q;
}

//链表实现约瑟夫循环
//初始化
nodelist*initList(int n) {
	nodelist*head = (nodelist*)malloc(sizeof(nodelist));
	head->value = 1;
	head->next = NULL;
	nodelist*current = head;
	for (int i = 2; i < n  ; i++) {
		nodelist*node= (nodelist*)malloc(sizeof(nodelist));
		node->value = i;
		node->next = NULL;
		current->next = node;
		current = current->next;
	}
	current->next = head;
	return head;
}
//取出应该杀死的人k为间隔m为初始点
int removeNode(nodelist*head, int m, int k) {
	nodelist*current = head,*temp=NULL;
	for (int i = 0; i < m; i++)
		current = current->next;
	nodelist*node = current;
	while (node->next != node) {
		for (int i = 0; i < k; i++) {
			temp = node;
			node = node->next;
		}
		temp->next = node->next;
		printf_s("%d -> ", node->value);
		free(node);
		node = temp->next;
	}
	return node->value;
}
int main()
{
	nodelist*head=initList(35);
	int n=removeNode(head, 7, 5);
	std::cout << n << std::endl;
	system("pause");
	return 0;
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值