数据结构实验期末押题卷1

1.链表部分:反转链表

#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
//押题第一道,实现链表的反转,样例输入:1 2 3,0,样例输出3 2 1,样例输入:1 2 3 4,0样例输出4 3 2 1
typedef struct node
{
	int data;
	node* next;
}node;
void creat(node** head, vector<int>s, int& i)
{
	if (i >= s.size())
	{
		return;
	}
	node* newnode = new node;
	newnode->data = s[i];
	newnode->next = nullptr;
	i++;
	if (*head == nullptr)
	{
		*head = newnode;
	}
	else
	{
		node* point = *head;
		while (point->next)
		{
			point = point->next;
		}
		point->next = newnode;
	}
	creat(head, s, i);
	
}
void print(node* head)
{
	node* point = head;
	while (point)
	{
		cout << point->data << ' ';
		point = point->next;
	}
}
node* re(node* head)
{
	if (head == nullptr)
	{
		return nullptr;
	}
	node* point = nullptr;
	node* good = head->next;
	node* bad = head;
	while (bad)
	{
		bad->next = point;
		point = bad;
		bad = good;
		if (good)
		{
			good = good->next;
		}
	}
	return point;

}
int main()
{
	node* head = nullptr;
	vector<int>s;
	while (1)
	{
		int t;
		cin >> t;
		if (t == 0)
		{
			break;
		}
		s.push_back(t);
	}
	int i = 0;
	creat(&head, s, i); 
	node*point=re(head);
	print(point);
	return 0;
}

2.栈和队列:使用两个栈实现队列

#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
//押题第二道,使用栈来模拟队列,使用容器按0分计算,样例输入:1 2 3 0,样例输出:1 2 3 
typedef struct mystack
{
	int arr[100];
	int top;
}mystack;
void creat(mystack& st)
{
	st.top = -1;
}
void push(int i, mystack& st)
{
	
	st.arr[++st.top] = i;
}
void pop(mystack& st)
{
	st.top--;
}
int top(mystack&st)
{
	return st.arr[st.top];
}
int size(mystack& st)
{
	return st.top+1;
}
int main()
{
	mystack a;
	creat(a);
	while (1)
	{
		int t;
		cin >> t;
		if (t == 0)
		{
			break;
		}
		push(t, a);
	}
	mystack b;
	creat(b);
	while (size(a) != 0)
	{
		push(top(a), b);
		pop(a);
	}
	while (size(b) != 0)
	{
		cout << top(b)<<' ';
		pop(b);
	}
	return 0;
}

3.二叉树:两种遍历建树以及寻找共同祖先

#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
//押题第三道:根据前序序列与中序序列创建二叉树,输入两个节点并且寻找这两个节点的共同祖先,样例输入:GABDCEF,BDAEFCG,DF,样例输出:A
typedef struct tree
{
	char data;
	tree* left;
	tree* right;
}tree;
tree* creat(string f, string i, int left, int right, int left1, int right1)
{
	if (left > right||left1>right1)
	{
		return nullptr;
	}
	tree* root = new tree;
	root->data = f[left];
	int mid = left1;
	while (i[mid] != f[left])
	{
		mid++;
	}
	int leftsize = mid - left1;

	root->left = creat(f, i, left + 1, left + leftsize , left1, mid - 1);
	root->right = creat(f, i, left + leftsize+1,right, mid + 1, right1);
	return root;
}
tree* find(tree* root, char data1, char data2)
{
	if (root == nullptr)
	{
		return nullptr;
	}
	if ( root->data == data1 || root->data == data2)
	{
		return root;
	}
	tree* left1 = find(root->left, data1, data2);
	tree* right1 = find(root->right, data1, data2);
	if (left1==nullptr)
	{
		return right1;
	}
	if (right1 == nullptr)
	{
		return left1;
	}
	return root;
}
void print(tree* root)
{
	if (root == nullptr)
	{
		return;
	}
	cout << root->data;
	print(root->left);
	print(root->right);
}
int main()
{
	string a;
	string b;
	cin >> a;
	cin >> b;
	char c;
	char d;
	cin >> c >> d;
	tree*root=creat(a, b, 0, a.size() - 1, 0, b.size() - 1);

	cout << find(root, c, d)->data;
	return 0;
}

4.图论:图的连通性与深度优先搜素

#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
//押题第四题,图的深度优先遍历,邻接表存储以及图的连通性:在你的大学课程中,有很多特定的课程关系,这些课程无论先后顺序必须在一个学年学完,现在将课程
//进行编号并且输入课程之间的关系,假如你是学生,请输出你可以选择多少类课程 样例输入 11(大学四年的课程数量),8(课程的关系数),1 2,4 3,5 4
//1 3,5 6,7 10,5 10,8 9样例输出3
void dfs(vector<bool>& visit, vector<vector<int>>& grap, int pos)
{
	visit[pos] = true;
	for (int point : grap[pos])
	{
		if (visit[point] == false)
		{
			dfs(visit, grap, point);
		}
	}
}
void judge(vector<vector<int>>& grap,int n)
{
	vector<bool>visit(n, false);
	int count = 0;
	for (int i = 0; i < n; i++)
	{
		if (visit[i] == false)
		{
			dfs(visit, grap, i);
			count++;
		}
	}
	cout << count;
}
int main()
{
	int n;
	int m;
	cin >> n;
	cin >> m;
	vector<vector<int>>grap(n);
	for (int i = 0; i < m; i++)
	{
		int a;
		int b;
		cin >> a >> b;
		grap[a - 1].push_back(b-1);
		grap[b - 1].push_back(a - 1);
	}
	judge(grap, n);
	return 0;

}

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值