C/C++ | 27-19 写出程序把一个链表中的接点顺序倒排

#include <cstdio>
#include <deque>
#include <algorithm>
#include <iterator>
#include <stdio.h>  
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <assert.h>

using namespace std;

typedef struct LNode
{
	int data;
	struct LNode *next;
}LNode;

LNode *CreateLink(int num)  // 构造
{
	int i = 1;
	LNode *head, *tail, *p;
	head = (LNode *)malloc(sizeof(LNode));
	tail = head;
	while (num--)
	{
		p = (LNode *)malloc(sizeof(LNode));
		p->data = i;
		tail->next = p;
		tail = p;
		i++;
	}
	tail->next = NULL;
	return head;
}

LNode *FindLink(LNode *head, int X)  //查找
{
	assert(head != NULL);
	LNode *chick = head->next;
	while (chick != NULL)
	{
		if (chick->data == X)
			return chick;
		else
			chick = chick->next;
	}
	return NULL;
}

void DeleteLink(LNode *head, int X)  //删除
{
	assert(head != NULL);
	LNode *p;
	LNode *q;
	p = FindLink(head, X);
	q = p->next;
	p->data = q->data;
	p->next = q->next;
	free(q);
}

LNode *inverseLink(LNode *head)
{
	assert(head != NULL);
	LNode *p, *q, *r;
	p = head;
	q = p->next;
	while (q != NULL)
	{
		r = q->next;
		q->next = p;
		p = q; 
		q = r;
	}
	head->next = NULL;
	head = p;
	return head;
}

int main()
{
	LNode *head = CreateLink(5);
	head=inverseLink(head);  // 这里要对head重新进行写,函数里面对head并不保存。

	system("pause");
	return 0;
}

请编写程序,用广度优先搜索输出给定无向图中的各个连通分量,并判断给定的无向图是否连通。 注意输出顺序规定如下: 每个连通分量的输出从其中编号最小的顶点开始; 不同连通分量按其第一个顶点的编号增序输出,每个连通分量占一行; 当一个顶点有多个邻接点时,按其输入的逆序进行访问,即最后输入的邻接点,在广度优先搜索中应被最先访问输出。 输入格式: 输入首先在第一行给出图中最大顶点数量,即正整数 kMaxVertex(≤10)。 第二行给出两个正整数,依次为当前要创建的图的顶点数 n 和边数 m(保证顶点数至少为 2 且不超过最大顶点数量)。 第三行给出 n 个英文字母,顺序对应每个顶点的信息。 随后 m 行,每行给出一条无向边的两个端点的编号、以及边的权重。顶点编号从 0 开始,权重(≤2 30 )为整数。 同行数字、字符均以一个空格分隔。 输出格式: 按照题面中规定的顺序,输出图中的连通分量每个顶点的信息。每个连通分量占一行,字符间不要有空格。 随后,如果图不连通,则在一行中输出 有 x 个连通分量,其中 x 是连通分量的个数。 最后一行输出 该图连通性为 y,其中图连通时 y 值为 1,否则为 0。 输入样例: 10 6 7 a b c d e f 2 0 2 2 3 1 3 0 3 3 4 4 2 4 2 4 0 5 4 5 1 输出样例: aedcf b 有 2 个连通分量 该图连通性为 0 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB 栈限制 8192 KB C++ (clang++) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 #include<iostream> #include<vector> using namespace std; struct A{ A*next; int to; }; int main() { int max; cin>>max; int n,m; cin>>n>>m; A**num=(A**)malloc(sizeof(A*)*n); for(int i=0;i<n;i++) num[i]=NULL; //vector<vector<int>>num(n,vector<int>(n,-1)); vector<int>id(n); vector<char>info(n); getchar(); for(int i=0;;) { char ch=getchar(); if(ch=='\n') break; if(ch!=' ') info[i++]=ch; } for(int i=0;i<m;i++) { int a,b,c; cin>>a>>b>>c; id[a]++;id[b]++; A*t=(A*)malloc(sizeof(A)); t->to=b; t->next=num[a]; num[a]=t; A*tmp=(A*)malloc(sizeof(A)); tmp->to=a; tmp->next=num[b]; num[b]=tmp; // num[a][b]=b; // num[b][a]=a; } int flag=0,count=1; vector<int>visited(n); for(int i=0;i<n;i++) { if(!id[i])//未连通 { count++;flag=1; cout<<info[i]<<endl; } if(id[i]&&!visited[i]) { cout<<info[i]; visited[i]=1; vector<int>stack(n); int front=0,rear=0; stack[rear++]=i; for(;front<rear;) { int s=stack[front++]; for(A*p=num[s];p;p=p->next) { if(p) { if(!visited[p->to]) { stack[rear++]=p->to; cout<<info[p->to];visited[p->to]=1; } } } } cout<<endl; } } cout<<"有 "<<count<<" 个连通分量"<<endl; cout<<"该图连通性为 "; if(flag) cout<<0; else cout<<1; } 测试用例 运行结果 编译器输出 查看上次提交 提交本题作答
最新发布
12-06
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值