蓝桥杯 算法训练 自行车停放问题&&双向链表

蓝桥杯 算法训练 自行车停放问题&&双向链表

完整代码在最下面!

题目

问题描述
  有n辆自行车依次来到停车棚,除了第一辆自行车外,每辆自行车都会恰好停放在已经在停车棚里的某辆自行车的左边或右边。(e.g.停车棚里已经有3辆自行车,从左到右编号为:3,5,1。现在编号为2的第4辆自行车要停在5号自行车的左边,所以现在停车棚里的自行车编号是:3,2,5,1)。给定n辆自行车的停放情况,按顺序输出最后停车棚里的自行车编号。
输入格式
  第一行一个整数n。
  第二行一个整数x。表示第一辆自行车的编号。
  以下n-1行,每行3个整数x,y,z。
  z=0时,表示编号为x的自行车恰停放在编号为y的自行车的左边
  z=1时,表示编号为x的自行车恰停放在编号为y的自行车的右边
输出格式
  从左到右输出停车棚里的自行车编号
样例输入
4
3
1 3 1
2 1 0
5 2 1
样例输出
3 2 5 1

数据规模和约定
  n<=100000
  自行车编号为不超过100000的正整数。

代码

1.结构体代码

 //链表结构体 
struct b{
	int id;//编号
	struct b* left;//前驱
	struct b* right;//后继
};
//链表的表头和表尾 
struct b* head = NULL;
struct b* end = NULL;
//插第一个元素 

2.建立第一个头
也就是说第一辆车的编号
或者是插入第一节点

void addhead(int x)
{
  		struct b* nb = (struct b*)malloc(sizeof(struct b));
  		nb->id=x;
  		head=nb;
  		end=nb;
  		nb->left=head->left=end->left=NULL;
  		nb->right=head->right=end->right=NULL;		
}

3.添加新节点

//添加元素 
void add(int x,int y,int z)
{

	struct b* temp=head;

	int flag=0;
	while(temp!=NULL)
	{
		if(temp->id==y)
		{
			flag=1;
			break;
		}
		temp=temp->right;
	}
	if(flag==0)
	{
		cout<<"未找到!"<<endl; 
	}
	
	struct b* nb = (struct b*)malloc(sizeof(struct b));
	nb->id = x;
		//头插头 
		//相当于节点没有前驱
		if(temp->left==NULL&&z==0)
		{
			nb->right=temp;
			temp->left=nb;
			head=nb;
			head->left=NULL;
			nb->left=NULL;
		return;
		}
		//尾插尾 
		//相当于节点没有后继
		if(temp->right==NULL&&z==1)
		{
			nb->left=temp;
			temp->right=nb;
			end=nb;
			end->right=nb->right=NULL;
				return;
		}
	//	插中间元素的左边 
	//相当于存在前驱
	if(temp->left!=NULL&&z==0)
	{
		nb->left=temp->left;
		temp->left->right=nb;
		nb->right=temp;
		temp->left=nb;

				return;
	}
//	插中间元素的右边 
//相当于存在后继
	 if(temp->right!=NULL&&z==1)
	{
	nb->right=temp->right;	
	temp->right->left=nb;
	nb->left=temp;
	temp->right=nb;
	return;
	}	
}

4.打印链表

//打印链表 
void print()
{
	struct b* temp=head;
	while(temp!=NULL)
	{
		cout<<temp->id;
		if(temp->right!=NULL)
		cout<<" ";
		temp=temp->right;
	}
}

notes:

由于涉及到指针操作
所以在添加节点中进入if判断后一定要让函数结束!!!

否则,temp有可能满足后面的if条件,还会执行后面的if指令
有可能陷入死循环!!

完整代码

#include<iostream>
#include<stdlib.h>
using namespace std;
 //链表结构体 
struct b{
	int id;
	struct b* left;
	struct b* right;
};
//链表的表头和表尾 
struct b* head = NULL;
struct b* end = NULL;
//插第一个元素 
void addhead(int x)
{
  		struct b* nb = (struct b*)malloc(sizeof(struct b));
  		nb->id=x;
  		head=nb;
  		end=nb;
  		nb->left=head->left=end->left=NULL;
  		nb->right=head->right=end->right=NULL;		
}

//添加元素 
void add(int x,int y,int z)
{

	struct b* temp=head;

	int flag=0;
	while(temp!=NULL)
	{
		if(temp->id==y)
		{
			
			flag=1;
			break;
		}
		
		temp=temp->right;
	}
	if(flag==0)
	{
		cout<<"未找到!"<<endl; 
	}
	
	struct b* nb = (struct b*)malloc(sizeof(struct b));
	nb->id = x;
		//头插头 
		if(temp->left==NULL&&z==0)
		{
			nb->right=temp;
			temp->left=nb;
			head=nb;
			head->left=NULL;
			nb->left=NULL;
		return;
		}
	
		 
		//尾插尾 
		if(temp->right==NULL&&z==1)
		{
			nb->left=temp;
			temp->right=nb;
			end=nb;
			end->right=nb->right=NULL;
				return;
		}
	//		插中间元素的左边 
	if(temp->left!=NULL&&z==0)
	{
		nb->left=temp->left;
		temp->left->right=nb;
		nb->right=temp;
		temp->left=nb;

				return;
	}
//	插中间元素的右边 
	 if(temp->right!=NULL&&z==1)
	{
	nb->right=temp->right;	
	temp->right->left=nb;
	nb->left=temp;
	temp->right=nb;
	return;
	}
	
	
}
//打印链表 
void print()
{
	struct b* temp=head;

	
	while(temp!=NULL)
	{
		cout<<temp->id;
		if(temp->right!=NULL)
		cout<<" ";
		temp=temp->right;
	}
}
int main(){
	int n,x,i;
	cin>>n;
	cin>>x;
	addhead(x);
	print();
	for(i=0;i<n-1;i++)
	{
		int x,y,z;
		cin>>x;
		cin>>y;
		cin>>z;
		add(x,y,z);
	}
	 print();
	
	return 0; 
	
} 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值