408-数据结构-代码题

news/2024/7/20 22:33:00 标签: 数据结构, 深度优先, 计算机考研

2014

2014 二叉树(链式存储)

#include<iostream>
#include<bits/stdc++.h>
using namespace std;

typedef struct Node{
	struct Node *left;
	struct Node *right;
	int high=0;
	double weight; 
}node;

double sum=0;

void visit(node *t){
	int lop=0;
	if(t->left != NULL){
		t->left->high = t->high+1;
		lop = 1;
	}
	if(t->right != NULL){
		t->right->high = t->high+1;
		lop = 1;
	}
	if(lop == 0){
		sum += t->weight * t->high;
	}
}

void dfs(node *t){
	if(t != NULL){
		cout << t->weight << endl;
		visit(t);
		dfs(t->left);
		dfs(t->right);
	}
}

node* buildnode(int w){
	node* tmp = (node *)malloc(sizeof(node));
//	tmp->high = 0;
	tmp->weight = w;
	tmp->left = tmp->right = NULL;
	return tmp;
}

void first(node *t){
	if(t != NULL){
	//	visit(t);
		cout << t->weight << "  " << t->high << endl;
		first(t->left);
		first(t->right);
	}
	
// also 
//	if(t == NULL)  return; 
//	//	visit(t);
//	cout << t->weight << endl;
//		
//	first(t->left);
//	
//	first(t->right);
//	
}

int main(){
	
	node* n1 = buildnode(1);
	node* n2 = buildnode(2);
	node* n3 = buildnode(3);
	node* n4 = buildnode(4);
	node* n5 = buildnode(5);
	node* n6 = buildnode(6);
	node* n7 = buildnode(7);
	
	n1->left = n2;
	n1->right = n3; 
	n2->left = n4;
	n2->right = n5;
	n3->right = n6;
	n4->right = n7;
	
	dfs(n1);
	cout << "sum = " <<  sum << endl; 
	first(n1);
	
	return 0;
} 

2017

2015 单链表(带头结点)

#include<iostream>
#include<cstring>
using namespace std;

typedef struct Node{
	int data;
	struct Node *next; 
}node,*list;
 
const int N = 100010;
int a[N];
void func(Node *head){
	node *p;
	node *r;
	p = head;
	while(1){
		int x = p->next->data;
		if(x < 0) x = -1*x;
		if(a[x] == 0){
			a[x] = 1;
			p = p->next;
		}
		else{
			r = p->next;
			p->next = r->next;
			free(r);
		}
		if(p->next == NULL) break;
	}
}
 
node* create(int n){
	node* p;
	p = (node*)malloc(sizeof(node));
	p->data = n;
	p->next = NULL;
}
 
void func2(Node *head){
	node *p;
	p = head;
	while(p->next != NULL){
		cout << p->next->data << " ";
		p = p->next;
	}
	return;
}
 

int main(){
	node *head;
	node *s1;
	node *s2;
	node *s3;
	node *s4;
	
	head = create(-1);
	s1 = create(2);
	s2 = create(2);
	s3 = create(1);
	s4 = create(1);
	
	head->next = s1;
	s1->next = s2;
	s2->next = s3;
	s3->next = s4;
	
	// bianli
	func2(head); 
	
	func(head);
	
	cout << endl;
	
	func2(head);

	return 0;
}
 

2017

2017 二叉树中序遍历

#include<iostream>

using namespace std;

typedef struct Node{
	char data;
	struct Node *left, *right;
}node;

void visit(node *t){
	cout << t->data << " ";
}

void bianli(node *t,int h){
	if(t != NULL){
	// 根 和 叶子 
		if(h > 0 && t->left!=NULL && t->right!=NULL) cout << "(";
		bianli(t->left,h+1);
		visit(t);
		bianli(t->right,h+1);
		if(h > 0 && t->left!=NULL && t->right!=NULL) cout << ")";
	}	
}

node* create(char data){
	node* s;
	s = (node *)malloc(sizeof(node));
	s->data = data;
	s->right = NULL;
	s->left = NULL;
	return s;
}

int main(){
	node *s1 = create('*');
	node *s2 = create('+');
	node *s3 = create('4');
	node *s4 = create('-');
	node *s5 = create('3');
	node *s6 = create('2');
	node *s7 = create('1');
	
	s1->left = s2;
	s1->right = s3;
	s2->left = s4;
	s2->right = s5;
	s4->right = s6;
	s4->left = s7;
	
	bianli(s1,0);
	
	return 0;	
} 

2019

2019 链表(快慢指针、反转链表、链表重排)

#include<iostream>
#include<cstring>

using namespace std;


typedef struct Node{
	int data;
	Node *next;
}node;

node* create(int data){
	node *n = (node *)malloc(sizeof(node));
	n->data = data;
	n->next = NULL;
	return n;
}

void print(node *head){
	node* p;
	p = head;
	while(p->next != NULL){
		cout << p->next->data << " ";
		p = p->next;
	}
	cout << endl;
}


void reverse(node *head){
	node *p;
	p = head->next;
	head->next = NULL;
	while(p != NULL){
		node *t = p->next;
		p->next = head->next;
		head->next = p;
		p = t;
	}
}

void change(node *head){
	
	node *q, *p;
	
	// 1. 快慢指針 來确定中间位置
	q = head;
	p = head;
	while(q->next != NULL){
		p = p->next;
		q = q->next;
		if(q->next != NULL) q = q->next;
	} 
	
//	cout << "## " << q->data << " " << p->data << endl;
	
	// 2. 反转链表
	// 头插法 头就是 p
	
	reverse(p);
	
//	cout << "p:  " << p->data << endl;
	
//	node *h;
//	h = p;
//	h->next = NULL;
//	p = p->next;
//	while(p!=NULL){
//		node *t = p->next;
//		p->next = head->next;
//		h->next = p;
//		p = t;
//	}

//	cout << "T: ";
//	print(head);
	
	// 3. 按题目要求进行插入
	node *f = head->next;
	node *r = p->next;
	p->next = NULL;
	
//	r = r->next;
//	if(r->next == NULL)  cout << "!!!!!!!!!!!!!!!!!!";
//	cout << "r:  " << r->data << endl;
	
	while(r!=NULL){
		node* t2 = r->next;
		r->next = f->next;
		f->next = r;
		f = r->next; 
		r = t2;
	} 
	 
}


int main(){
	
	node* head = create(-1);
	node* n1 = create(1);
	node* n2 = create(2);
	node* n3 = create(3);
	node* n4 = create(4);
	node* n5 = create(5);
	
	head->next = n1;
	n1->next = n2;
	n2->next = n3;
	n3->next = n4;
	n4->next = n5;
	n5->next = NULL;
	// n1->n2->n3->n4->n5
	
	print(head);
	
//	reverse(head);
	
//	print(head);
	
	change(head);
	
	print(head);
	
	return 0;
}
 

模拟题1

输出二叉树任意节点到根节点的路径(链式存储)

#include<iostream>
#include<cstring>

using namespace std;

typedef struct Node{
	int num;
	struct Node *left, *right;
	struct Node *front;
}node;

void visit(node *t){
	if(t->left != NULL){
		t->left->front = t;
	}
	if(t->right != NULL){
		t->right->front = t;
	}
}

void first(node* t){
	if(t!=NULL){
		visit(t);
		cout << t->num << endl;
		first(t->left);
		first(t->right);
	}
}




void print(node *t){
	cout << "the route is ";
	while(1){
		cout << t->num << " ";
		t = t->front;
		if(t == NULL) break;
	}
	cout << endl;
}

node* create(int num){
	node* s;
	s = (node *)malloc(sizeof(node));
	s->num = num;
	s->front = NULL;
	s->right = NULL;
	s->left = NULL;
	return s;
}

int main(){
	node *s1 = create(1);
	node *s2 = create(2);
	node *s3 = create(3);
	node *s4 = create(4);
	node *s5 = create(5);
	node *s6 = create(6);
	node *s7 = create(7);
	
	s1->left = s2;
	s1->right = s3;
	s2->left = s4;
	s2->right = s5;
	s3->left = s6;
	s4->left = s7;
	
	first(s1);
	
	print(s7);
	
	return 0;	
} 

http://www.niftyadmin.cn/n/5207711.html

相关文章

.net core 校验字段并自定义返回格式

/// <summary>/// 开始行数/// </summary>[Range(1, int.MaxValue, ErrorMessage "Page必须大于0")]public int Page { get; set; }配置 [Required]、[StringLength]、[RegularExpression] 和 [Range] 验证特性 services.Configure<ApiBehaviorOpti…

QSplitter分裂器

QSplitter QSplitter 是 Qt 框架提供的一个小部件&#xff08;widget&#xff09;&#xff0c;用于在用户界面中创建可拖动的分割窗口&#xff0c;允许用户调整子部件的大小和布局。它可以将父部件分割为多个可调整大小的子部件&#xff0c;使用户能够自定义界面的布局和大小。…

2002-2020年341个地级市农业保险收入数据

2002-2020年341个地级市农业保险收入数据 1、时间&#xff1a;2002-2020年 2、范围&#xff1a;341个地级市 3、指标&#xff1a;农业保险收入 4、来源&#xff1a;整理自wind、保险年鉴 5、指标解释&#xff1a; 农业保险保费收入是指保险公司从农户或农业生产经营者那里…

ultralytics yolov8 实例分割 训练自有数据集

参考: https://docs.ultralytics.com/datasets/segment/coco/ http://www.bryh.cn/a/613333.html 1、数据下载与转换yolo格式 1)数据集下载: 参考:https://universe.roboflow.com/naumov-igor-segmentation/car-segmetarion 下载的是coco格式,需要转换 2)coco2yolo t…

Flink Table API 读写MySQL

Flink Table API 读写 MySQL import org.apache.flink.connector.jdbc.table.JdbcConnectorOptions; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.table.api.DataTypes; import org.apache.flink.table.api.Envi…

Hyper-V系列:Hyper-V启动、创建虚拟机、与主机传输文件

Hyper-V启动、创建虚拟机、与主机传输文件 一. 简介二. 启用Hyper-V的方式也很简单:一、从“任务管理器”的“性能”查看虚拟化是否启用,未启用的需要到BIOS开启:右下角可以看到“虚拟化:已启用”二、启用Hyper-v和虚拟机1.电脑左下角右键打开应用界面——可选功能2.在可选…

C++中缺省值的用法和注意事项

缺省值是指在函数声明时为参数提供的默认值&#xff0c;如果调用函数时没有传递参数&#xff0c;那么编译器就会自动使用缺省值。如果传递了参数&#xff0c;那么缺省值就会被忽略。使用缺省值可以简化函数的调用&#xff0c;减少重载函数的数量&#xff0c;提高代码的可读性和…

自动驾驶术语汇总

目录 智驾级别芯片相关自动驾驶相关辅助驾驶相关预警相关传感器相关泊车相关安全相关车灯相关 智驾级别 L0-L2属于辅助驾驶&#xff0c;L4-L5才算自动驾驶 L0&#xff08;Level 0&#xff09;&#xff1a;无自动化。这是大多数传统汽车的级别&#xff0c;所有的驾驶任务都需要…