蓝桥杯 算法训练 自行车停放问题&&双向链表
完整代码在最下面!
题目
问题描述
有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;
}