2022/1/10总结

今天又学了9.5h,真是充实(

主要今天在链表上面下了比较多的精力

不多说,上题


这是我自己练手写的链表方法:

#include <bits/stdc++.h>

using namespace std;

typedef struct _mylist {
	int val;
	struct _mylist *next;
} *mylist;

mylist CreateList()
{
	mylist headnode = (mylist)malloc(sizeof(_mylist));
	headnode->val = -1;
	headnode->next = NULL;
	mylist guardnode = (mylist)malloc(sizeof(_mylist));
	headnode->next = guardnode;
	guardnode->val = 0;
	guardnode->next = NULL;	
//	mylist endnode = (mylist)malloc(sizeof(_mylist));
//	guardnode->next = endnode;
//	endnode->val = NULL;
//	endnode->next = NULL;
	return headnode;
}

/*----------------------------------------Ç°ÖÃ-----------------------------------------*/
void SwapNode(mylist node1_prev, mylist node2_prev)
{
	mylist node1_next = node1_prev->next->next;
	mylist node2_next = node2_prev->next->next;
	mylist tempnode = node1_prev->next;
	node1_prev->next = node2_prev->next;
	node1_prev->next->next = node1_next;
	node2_prev->next = tempnode;
	node2_prev->next->next = node2_next;
}
/*----------------------------------------Ç°ÖÃ-----------------------------------------*/

void ClearCosole()
{
	system("cls");
}

void DelList(mylist Head)
{
	mylist NowNode = Head;
	mylist NextNode = NowNode->next;
	while(NextNode->next != NULL)
	{
		free(NowNode);
		NowNode = NextNode;
		NextNode = NextNode->next;
	}
	free(NextNode);
}

void DelNode_ALL(mylist Head)
{
	int sum = Head->next->val;
	if(sum != 0)
	{
		mylist node = Head->next->next;
		Head->next->val = 0;
		Head->next->next = NULL;
		while(node->next != NULL)
		{
			mylist NowNode = node;
			node = node->next;
			free(NowNode);
		}
		free(node);
	}
	else
	{
		cout << "This list is empty!" << endl;
	}
}

void DelNode_SUPER(mylist Head, int pos)
{
	int sum = Head->next->val;
	if(pos > sum) pos = sum;
	if(sum != 0)
	{
		mylist node = Head->next;
		Head->next->val --;
		for(int i = 0;i < pos - 1;i ++)
		{
			node = node->next;
		}		
		mylist NowNode = node;
		node = node->next;
		if(node->next != NULL)
		{
			NowNode->next = node->next;
		}
		else
		{
			NowNode->next = NULL;
		}
		free(node);
	}
	else
	{
		cout << "This list is empty!" << endl;
	}
}

void AddNode_INSERT(mylist Head, int pos, int val)
{
	int sum = Head->next->val;
	Head->next->val ++;
	if(pos > sum) pos = sum;
	mylist node = Head->next;
	for(int i = 0;i < pos - 1;i ++)
	{
		node = node->next;
	}
	mylist NewNode = (mylist)malloc(sizeof(_mylist));
	if(node->next != NULL)
	{
		mylist tempnode = node->next;
		node->next = NewNode;
		NewNode->next = tempnode;
		NewNode->val = val;
	}
	else
	{
		node->next = NewNode;
		NewNode->next = NULL;
		NewNode->val = val;
	}
}

void AddNode_START(mylist Head, int val)
{
	mylist node = Head->next;
	Head->next->val ++;
	mylist NewNode = (mylist)malloc(sizeof(_mylist));
	if(node->next != NULL)
	{
		mylist tempnode = node->next;
		Head->next->next = NewNode;
		NewNode->next = tempnode;
		NewNode->val = val;
	}
	else
	{
		node->next = NewNode;
		NewNode->next = NULL;
		NewNode->val = val;
	}
}

void AddNode_END(mylist Head, int val)
{
	mylist node = Head;
	Head->next->val ++;
	while(node->next != NULL)
	{
		node = node->next;
	}
	mylist NewNode = (mylist)malloc(sizeof(_mylist));
	node->next = NewNode;
	NewNode->next = NULL;
	NewNode->val = val;
}

void Print(mylist Head)
{
	int sum = Head->next->val;
	if(sum != 0)
	{
		mylist node = Head->next->next;
		for(int i = 0;i < sum;i ++)
		{
			cout << node->val << " ";
			if(node->next != NULL)
			{
				node = node->next;
			}
		}	
		cout << endl;
	}
	else
	{
		cout << "This list is empty!" << endl;
	}
}

void ChangeNode(mylist Head, int pos, int val)
{
	int sum = Head->next->val;
	if(sum == 0) return;
	if(pos > sum) pos = sum;
	mylist node = Head->next;
	for(int i = 0;i < pos - 1;i ++)
	{
		node = node->next;
	}
	node->next->val = val;
}


/*-------------------------------------·â×°-------------------------------------*/
void SwapNodeF(mylist Head, int pos1, int pos2)
{
	int sum = Head->next->val;
	if(pos1 > sum) pos1 = sum;
	if(pos2 > sum) pos2 = sum;
	if(pos1 == pos2) return;
	mylist node1 = Head->next, node2 = Head->next;
	for(int i = 0;i < pos1 - 1;i ++)
	{
		node1 = node1->next;
	}
	for(int i = 0;i < pos2 - 1;i ++)
	{
		node2 = node2->next;
	}
	SwapNode(node1, node2);
}

void FillList(mylist Head, int sum, int val)
{
	for(int i = 0;i < sum;i ++)
	{
		AddNode_END(Head, val);
	}
}

void SortList(mylist Head, int mod)
{
	int sum = Head->next->val;
	if(sum <= 1) return;
	mylist cmpnode = Head;
	for(int i = 0;i < sum;i ++)
	{
		cmpnode = Head->next;
		for(int j = 0;j < sum - i - 1;j ++)
		{
			if(mod == 1)
			{
//				cout << "mod1" << endl;
				if(cmpnode->next->val > cmpnode->next->next->val)
				{
					int tmp = cmpnode->next->next->val;
					cmpnode->next->next->val = cmpnode->next->val;
					cmpnode->next->val = tmp;
				}	
			}
			else if(mod == 0)
			{
				if(cmpnode->next->val < cmpnode->next->next->val)
				{
					int tmp = cmpnode->next->next->val;
					cmpnode->next->next->val = cmpnode->next->val;
					cmpnode->next->val = tmp;
				}	
			}
			cmpnode = cmpnode->next;	
		}
	}
}
/*-------------------------------------·â×°-------------------------------------*/

int main()
{
	string cmd;
	string listname;
	mylist lists[128];
	int lpos = -1;
	map<string, mylist> mp;
	int val, pos;
	while(1)
	{
		cin >> cmd;
		if(cmd == "create")
		{
			cin >> listname;
			if(mp[listname] != NULL)
			{
				cout << "This list is already created! Please change a name!" << endl;
				continue;
			}			
			lists[++ lpos] = CreateList();
			mp[listname] = lists[lpos];
		}
		else if(cmd == "add_start")
		{
			cin >> listname;
			cin >> val;
			getchar();
			if(mp[listname] == NULL)
			{
				cout << "Don't have this list!" << endl;
				continue;
			}			
			AddNode_START(mp[listname], val);
		}
		else if(cmd == "add_end")
		{
			cin >> listname;
			cin >> val;
			getchar();
			if(mp[listname] == NULL)
			{
				cout << "Don't have this list!" << endl;
				continue;
			}			
			AddNode_END(mp[listname], val);
		}
		else if(cmd == "add_insert")
		{
			cin >> listname;
			cin >> pos;
			cin >> val;
			getchar();
			if(mp[listname] == NULL)
			{
				cout << "Don't have this list!" << endl;
				continue;
			}			
			AddNode_INSERT(mp[listname], pos, val);
		}
		else if(cmd == "del_super")
		{
			cin >> listname;
			cin >> pos;
			getchar();
			if(mp[listname] == NULL)
			{
				cout << "Don't have this list!" << endl;
				continue;
			}			
			DelNode_SUPER(mp[listname], pos);
		}
		else if(cmd == "del_all")
		{
			cin >> listname;
			if(mp[listname] == NULL)
			{
				cout << "Don't have this list!" << endl;
				continue;
			}			
			DelNode_ALL(mp[listname]);
		}
		else if(cmd == "del_list")
		{
			cin >> listname;
			if(mp[listname] == NULL)
			{
				cout << "Don't have this list!" << endl;
				continue;
			}			
			DelList(mp[listname]);
			mp[listname] = NULL;
		}
		else if(cmd == "change")
		{
			cin >> listname;
			cin >> pos;
			cin >> val;
			getchar();
			if(mp[listname] == NULL)
			{
				cout << "Don't have this list!" << endl;
				continue;
			}
			ChangeNode(mp[listname], pos, val);
		}
		else if(cmd == "swap")
		{
			cin >> listname;
			cin >> pos;
			cin >> val; //pos2
			getchar();
			if(mp[listname] == NULL)
			{
				cout << "Don't have this list!" << endl;
				continue;
			}
			SwapNodeF(mp[listname], pos, val);			
		}
		else if(cmd == "fill")
		{
			cin >> listname;
			cin >> pos; //sum
			cin >> val; 
			getchar();
			if(mp[listname] == NULL)
			{
				cout << "Don't have this list!" << endl;
				continue;
			}
			FillList(mp[listname], pos, val);			
		}
		else if(cmd == "sort")
		{
			cin >> listname;
			cin >> val;
			getchar();
			if(mp[listname] == NULL)
			{
				cout << "Don't have this list!" << endl;
				continue;
			}
			SortList(mp[listname], val);
		}
		else if(cmd == "print")
		{
			cin >> listname;
			if(mp[listname] == NULL)
			{
				cout << "Don't have this list!" << endl;
				continue;
			}
			Print(mp[listname]);
		}
		else if(cmd == "clear")
		{
			ClearCosole();
		}
	}
	return 0;
}


 

题目描述

知L1、L2分别为两循环单链表的头结点指针,m,n分别为L1、L2表中数据结点个数。要求设计一算法,用最快速度将两表合并成一个带头结点的循环单链表。

输入格式

m=5

3 6 1 3 5

n=4

7 10 8 4

输出格式

3 6 1 3 5 7 10 8 4

样例输入content_copy

m=7
3 5 1 3 4 6 0

n=5
5 4 8 9 5

样例输出content_copy

3 5 1 3 4 6 0 5 4 8 9 5 

很简单,只要创建两个链表赋值,并链接它们的头尾即可,上AC代码

#include <bits/stdc++.h>

using namespace std;

typedef struct _mylist {
	int val;
	struct _mylist *next;
} *mylist;

mylist CreateList()
{
	mylist headnode = (mylist)malloc(sizeof(_mylist));
	headnode->val = -1;
	headnode->next = NULL;
	mylist guardnode = (mylist)malloc(sizeof(_mylist));
	headnode->next = guardnode;
	guardnode->val = 0;
	guardnode->next = NULL;	
//	mylist endnode = (mylist)malloc(sizeof(_mylist));
//	guardnode->next = endnode;
//	endnode->val = NULL;
//	endnode->next = NULL;
	return headnode;
}

void AddNode_END(mylist Head, int val)
{
	mylist node = Head;
	Head->next->val ++;
	while(node->next != NULL)
	{
		node = node->next;
	}
	mylist NewNode = (mylist)malloc(sizeof(_mylist));
	node->next = NewNode;
	NewNode->next = NULL;
	NewNode->val = val;
}

void Print(mylist Head)
{
	int sum = Head->next->val;
	if(sum != 0)
	{
		mylist node = Head->next->next;
		for(int i = 0;i < sum;i ++)
		{
			cout << node->val << " ";
			if(node->next != NULL)
			{
				node = node->next;
			}
		}	
		cout << endl;
	}
	else
	{
		cout << "This list is empty!" << endl;
	}
}

int main()
{
	int m;
	scanf("m=%d", &m);
	mylist a = CreateList();
	for(int i = 0;i < m;i ++)
	{
		int tmp;
		cin >> tmp;
		AddNode_END(a, tmp);
	}
	getchar();
	getchar();
	int n;
	scanf("n=%d", &n);
	mylist b = CreateList();
	for(int i = 0;i < n;i ++)
	{
		int tmp;
		cin >> tmp;
		AddNode_END(b, tmp);
	}
	mylist c = CreateList();
	c->next->next = a->next->next;
	mylist k = c->next;
	for(int i = 0;i < m;i ++)
	{
		k = k->next;
	}
	k->next = b->next->next;
	c->next->val += a->next->val + b->next->val;
	for(int i = 0;i < n;i ++)
	{
		k = k->next;
	}
	k->next = c->next->next;
	Print(c);
	return 0;
}

其实,下面这道题跟上面的答案几乎一样,就多了拼接时的插入排序操作,并且不是直接链接头尾,因为篇幅太长就不给码了。

(线性表)假设有两个按元素值递增次序排列的线性表,均以单链表形式存储。请编写算法将这两个单链表归并为一个按元素值递减次序排列的单链表,并要求利用原来两个单链表的结点存放归并后的单链表。

输入格式

输入长度n:5

输入数据:1 2 5 6 8

输入长度m:5

输入数据:3 4 7 9 10

输出格式

10 9 8 7 6 5 4 3 2 1

样例输入content_copy

4
7 9 10 11
4
8 12 13 14

样例输出content_copy

14 13 12 11 10 9 8 7 

题目描述

(线性表)已知不带头结点的线性链表list,链表中结点构造为(data、link),其中data为数据域,link为指针域。请写一算法,将该链表按结点数据域的值的大小从小到大重新链接。要求链接过程中不得使用除该链表以外的任何链结点空间。

输入格式

自定义链表节点数

m=5

3 1 5 4 6

输出格式

1 3 4 5 6

样例输入content_copy

8

10 1 5 14 32 55 67 6

样例输出content_copy

1 5 6 10 14 32 55 67 

其实,这道题也不难,最简单的方法就是交换它们的数据域,按照一般处理数组的方法处理它们就好了。

#include <bits/stdc++.h>

using namespace std;

typedef struct _mylist {
	int val;
	struct _mylist *next;
} *mylist;

mylist CreateList()
{
	mylist headnode = (mylist)malloc(sizeof(_mylist));
	headnode->val = -1;
	headnode->next = NULL;
	mylist guardnode = (mylist)malloc(sizeof(_mylist));
	headnode->next = guardnode;
	guardnode->val = 0;
	guardnode->next = NULL;	
//	mylist endnode = (mylist)malloc(sizeof(_mylist));
//	guardnode->next = endnode;
//	endnode->val = NULL;
//	endnode->next = NULL;
	return headnode;
}

void AddNode_END(mylist Head, int val)
{
	mylist node = Head;
	Head->next->val ++;
	while(node->next != NULL)
	{
		node = node->next;
	}
	mylist NewNode = (mylist)malloc(sizeof(_mylist));
	node->next = NewNode;
	NewNode->next = NULL;
	NewNode->val = val;
}

void Print(mylist Head)
{
	int sum = Head->next->val;
	if(sum != 0)
	{
		mylist node = Head->next->next;
		for(int i = 0;i < sum;i ++)
		{
			cout << node->val << " ";
			if(node->next != NULL)
			{
				node = node->next;
			}
		}	
		cout << endl;
	}
	else
	{
		cout << "This list is empty!" << endl;
	}
}

void SortList(mylist Head, int mod)
{
	int sum = Head->next->val;
	if(sum <= 1) return;
	mylist cmpnode = Head;
	for(int i = 0;i < sum;i ++)
	{
		cmpnode = Head->next;
		for(int j = 0;j < sum - i - 1;j ++)
		{
			if(mod == 1)
			{
//				cout << "mod1" << endl;
				if(cmpnode->next->val > cmpnode->next->next->val)
				{
					int tmp = cmpnode->next->next->val;
					cmpnode->next->next->val = cmpnode->next->val;
					cmpnode->next->val = tmp;
				}	
			}
			else if(mod == 0)
			{
				if(cmpnode->next->val < cmpnode->next->next->val)
				{
					int tmp = cmpnode->next->next->val;
					cmpnode->next->next->val = cmpnode->next->val;
					cmpnode->next->val = tmp;
				}	
			}
			cmpnode = cmpnode->next;	
		}
	}
}

int main()
{
	int m;
	cin >> m;
	mylist a = CreateList();
	for(int i = 0;i < m;i ++)
	{
		int tmp;
		cin >> tmp;
		AddNode_END(a, tmp);
	}
	SortList(a, 1);
	Print(a);
	return 0;
}

 因为篇幅过长,以下这题采用直接插入就可以完成排序工作了,这里就不再赘述。

题目描述

(线性表)已知一单链表,从第二个结点至表尾递增有序,(设a1<x<an)如下图(“第二个结点至表尾”指a1..an )。试编写程序,将第一个结点删除并插入表中适当位置,使整个链表递增有序。

输入格式

输入长度n:7

输入数据:4 1 2 3 6 8 9

输出格式

1 2 3 4 6 8 9

样例输入content_copy

5
11 7 8 9 10

样例输出content_copy

7 8 9 10 11 

 

题目描述

已知非空线性链表由list指出,链结点的构造为(data,link).请写一算法,将链表中数据域值最小的那个链结点移到链表的最前面。要求:不得额外申请新的链结点

输入格式

输入长度n:6

 输入数据:4 2 6 88 34 6

输出格式

2 4 6 88 34 6

样例输入content_copy

5
11 6 8 7 9

样例输出content_copy

6 11 8 7 9 

我这道题“取巧”了一下,我直接交换了数据域,因为指针域的交换极其容易出错。

#include <bits/stdc++.h>

using namespace std;

typedef struct _mylist {
	int val;
	struct _mylist *next;
} *mylist;

mylist CreateList()
{
	mylist headnode = (mylist)malloc(sizeof(_mylist));
	headnode->val = -1;
	headnode->next = NULL;
	mylist guardnode = (mylist)malloc(sizeof(_mylist));
	headnode->next = guardnode;
	guardnode->val = 0;
	guardnode->next = NULL;	
//	mylist endnode = (mylist)malloc(sizeof(_mylist));
//	guardnode->next = endnode;
//	endnode->val = NULL;
//	endnode->next = NULL;
	return headnode;
}

void AddNode_END(mylist Head, int val)
{
	mylist node = Head;
	Head->next->val ++;
	while(node->next != NULL)
	{
		node = node->next;
	}
	mylist NewNode = (mylist)malloc(sizeof(_mylist));
	node->next = NewNode;
	NewNode->next = NULL;
	NewNode->val = val;
}

void Print(mylist Head)
{
	int sum = Head->next->val;
	if(sum != 0)
	{
		mylist node = Head->next->next;
		for(int i = 0;i < sum;i ++)
		{
			cout << node->val << " ";
			if(node->next != NULL)
			{
				node = node->next;
			}
		}	
		cout << endl;
	}
	else
	{
		cout << "This list is empty!" << endl;
	}
}

void SwapNode(mylist node1_prev, mylist node2_prev)
{
	mylist node1_next = node1_prev->next->next;
	mylist node2_next = node2_prev->next->next;
	mylist tempnode = node1_prev->next;
	node1_prev->next = node2_prev->next;
	node1_prev->next->next = node1_next;
	node2_prev->next = tempnode;
	node2_prev->next->next = node2_next;
}

void SwapNodeF(mylist Head, int pos1, int pos2)
{
	int sum = Head->next->val;
	if(pos1 > sum) pos1 = sum;
	if(pos2 > sum) pos2 = sum;
	if(pos1 == pos2) return;
	mylist node1 = Head->next, node2 = Head->next;
	for(int i = 0;i < pos1 - 1;i ++)
	{
		node1 = node1->next;
	}
	for(int i = 0;i < pos2 - 1;i ++)
	{
		node2 = node2->next;
	}
	SwapNode(node1, node2);
}

int main()
{
	int n;
	cin >> n;
	mylist a = CreateList();
	for(int i = 0;i < n;i ++)
	{
		int tmp;
		cin >> tmp;
		AddNode_END(a, tmp);
	}
	mylist minnode = a->next;
	mylist node = a->next;
	for(int i = 0;i < n - 1;i ++)
	{
		if(node->next->val < minnode->next->val)
		{
			minnode = node;
//			cout << minnode->next->val << endl;
		}
		node = node->next;
	}
	int tmp = minnode->next->val;
	minnode->next->val = a->next->next->val;
	a->next->next->val = tmp;
	Print(a);
	return 0;
}

 


题目描述

构建一个双向链表并进行删除和插入操作,按要求输出。

输入格式

 

输入:

第一行输入元素个数M

第二行输入M个元素

第三行输入删除位置,位置为0时不删除

第四行输入插入位置和插入元素

第五行输入输出时的起始位置

输出格式

按要求的起始位置输出链表

样例输入content_copy

8
1 2 3 4 5 6 7 8
6
6 6
5

样例输出content_copy

5 6 7 8 1 2 3 4 

双向链表,其实处理方法跟线性链表很像,根据我自己写的方法组,就很简单了,直接上AC代码:

#include <bits/stdc++.h>

using namespace std;

typedef struct _mylist {
	int val;
	struct _mylist *next;
	struct _mylist *prev;
} *mylist;

mylist CreateList()
{
	mylist headnode = (mylist)malloc(sizeof(_mylist));
	headnode->val = -1;
	headnode->next = NULL;
	headnode->prev = NULL;
	mylist guardnode = (mylist)malloc(sizeof(_mylist));
	headnode->next = guardnode;
	guardnode->val = 0;
	guardnode->next = NULL;	
	guardnode->prev = headnode;
//	mylist endnode = (mylist)malloc(sizeof(_mylist));
//	guardnode->next = endnode;
//	endnode->val = NULL;
//	endnode->next = NULL;
	return headnode;
}

void AddNode_END(mylist Head, int val)
{
	mylist node = Head;
	Head->next->val ++;
	while(node->next != NULL)
	{
		node = node->next;
	}
	mylist NewNode = (mylist)malloc(sizeof(_mylist));
	node->next = NewNode;
	NewNode->next = NULL;
	NewNode->val = val;
	NewNode->prev = node;
}

void Print(mylist Head)
{
	int sum = Head->next->val;
	if(sum != 0)
	{
		mylist node = Head->next->next;
		for(int i = 0;i < sum;i ++)
		{
			cout << node->val << " ";
			if(node->next != NULL)
			{
				node = node->next;
			}
		}	
		cout << endl;
	}
	else
	{
		cout << "This list is empty!" << endl;
	}
}

void DelNode_SUPER(mylist Head, int pos)
{
	int sum = Head->next->val;
	if(pos > sum) pos = sum;
	if(sum != 0)
	{
		mylist node = Head->next;
		Head->next->val --;
		for(int i = 0;i < pos - 1;i ++)
		{
			node = node->next;
		}		
		mylist NowNode = node;
		node = node->next;
		if(node->next != NULL)
		{
			NowNode->next = node->next;
			node->next->prev = NowNode;
		}
		else
		{
			NowNode->next = NULL;
			node->prev = NULL;
		}
		free(node);
	}
	else
	{
		cout << "This list is empty!" << endl;
	}
}

void AddNode_INSERT(mylist Head, int pos, int val)
{
	int sum = Head->next->val;
	Head->next->val ++;
	if(pos > sum)
	{
		AddNode_END(Head, val);
		return;
	}
	mylist node = Head->next;
	for(int i = 0;i < pos - 1;i ++)
	{
		node = node->next;
	}
	mylist NewNode = (mylist)malloc(sizeof(_mylist));
	if(node->next != NULL)
	{
		mylist tempnode = node->next;
		node->next = NewNode;
		NewNode->prev = node;
		NewNode->next = tempnode;
		tempnode->prev = NewNode;
		NewNode->val = val;
	}
	else
	{
		node->next = NewNode;
		NewNode->prev = node;
		NewNode->next = NULL;
		NewNode->val = val;
	}
}

void AddNode_START(mylist Head, int val)
{
	mylist node = Head->next;
	Head->next->val ++;
	mylist NewNode = (mylist)malloc(sizeof(_mylist));
	if(node->next != NULL)
	{
		mylist tempnode = node->next;
		Head->next->next = NewNode;
		NewNode->next = tempnode;
		NewNode->val = val;
	}
	else
	{
		node->next = NewNode;
		NewNode->next = NULL;
		NewNode->val = val;
	}
}

int main()
{
	int M;
	cin >> M;
	mylist a = CreateList();
	for(int i = 0;i < M;i ++)
	{
		int tmp;
		cin >> tmp;
		AddNode_END(a, tmp);
	}
	int pos;
	cin >> pos;
	if(pos != 0)
	{
		DelNode_SUPER(a, pos);
	}
	cin >> pos;
	int val;
	cin >> val;
	int sum = a->next->val;
	if(pos <= sum && pos >= 1) AddNode_INSERT(a, pos, val);
	else if(pos > sum)
	{
		AddNode_END(a, val);
	}
	else
	{
		AddNode_START(a, val);
	}
	mylist k = a->next;
	for(int i = 0;i < a->next->val;i ++)
	{
		k = k->next;
	}
	k->next = a->next->next;
	int P;
	cin >> P;
	k = a->next;
	for(int i = 0;i < P;i ++)
	{
		k = k->next;
	}
	for(int i = 0;i < a->next->val;i ++)
	{
		cout << k->val << " ";
		k = k->next;
	}
	return 0;
}

题目描述

小T昨天很不爽
昨天路过体彩销售点买了2注生日号码玩,当晚开奖后……
小T号码: 8902088
中奖号码:3902888
一个号码不对就差了十几万……
小T想:为什么规则是那样玩的啊……为什么5个号码要连续才能中二等奖啊……
我自己创建个规则,开N个数字,只要数字位置对了不一定要连续就可以有奖。
现在有一个中奖号码,有一个兑奖号码,小T想知道一共有多少个数字位置正确的,N太大了……

要求用链表做

输入格式

输入数据第一行包含一个整数T表示有T组测试数据

对于每组测试数据

第一行包含一个整数N,表示有N个数字

第二行包含N个中奖号码

第三行包含N个兑奖号码

输出格式

对于每组测试数据输出有多少个号码位置正确了

样例输入content_copy

2
7
3902888
8902088
10
1234567890
0987654321

样例输出content_copy

5
0

其实这道题可以用string直接做,但是要求用链表,就写了个链表的。逻辑很简单,直接比较相同位置相同数字的个数就行了。上AC代码:

#include <bits/stdc++.h>

using namespace std;

typedef struct _mylist {
	int val;
	struct _mylist *next;
} *mylist;

mylist CreateList()
{
	mylist headnode = (mylist)malloc(sizeof(_mylist));
	headnode->val = -1;
	headnode->next = NULL;
	mylist guardnode = (mylist)malloc(sizeof(_mylist));
	headnode->next = guardnode;
	guardnode->val = 0;
	guardnode->next = NULL;	
//	mylist endnode = (mylist)malloc(sizeof(_mylist));
//	guardnode->next = endnode;
//	endnode->val = NULL;
//	endnode->next = NULL;
	return headnode;
}

void AddNode_END(mylist Head, int val)
{
	mylist node = Head;
	Head->next->val ++;
	while(node->next != NULL)
	{
		node = node->next;
	}
	mylist NewNode = (mylist)malloc(sizeof(_mylist));
	node->next = NewNode;
	NewNode->next = NULL;
	NewNode->val = val;
}

void Print(mylist Head)
{
	int sum = Head->next->val;
	if(sum != 0)
	{
		mylist node = Head->next->next;
		for(int i = 0;i < sum;i ++)
		{
			cout << node->val << " ";
			if(node->next != NULL)
			{
				node = node->next;
			}
		}	
		cout << endl;
	}
	else
	{
		cout << "This list is empty!" << endl;
	}
}

int main()
{
	int T;
	cin >> T;
	for(int i = 0;i < T;i ++)
	{
		int len;
		cin >> len;
		getchar();
		string str1, str2;
		getline(cin, str1, '\n');
		getline(cin, str2, '\n');
		mylist a = CreateList();
		mylist b = CreateList();
		
		for(int j = 0;j < len;j ++)
		{
			AddNode_END(a, str1[j] - '0');
			AddNode_END(b, str2[j] - '0');
		}
		
		mylist tmp1 = a->next;
		mylist tmp2 = b->next;
		
		int sum = 0;
		
		for(int j = 0;j < len;j ++)
		{
			tmp1 = tmp1->next;
			tmp2 = tmp2->next;
			if(tmp1->val == tmp2->val)
			{
				sum ++;
			}
		}
		
		cout << sum << endl;
	}
	return 0;
}

我是真的一整天都在写链表,人都要写傻了。

明天试试能不能AK了链表题组,然后把搜索的任务完成掉。

刚刚打了会儿codeforces,差点把总结忘了,今天精神也不太好,题目也做不太下去了,还是需要把技术提升一下呀。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ISansXI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值