• 考研模拟面试-答案【攻略】


    前言

    2023-10-19 12:00:57

    以下内容源自《考研模拟面试-答案》
    仅供学习交流使用

    版权

    禁止其他平台发布时删除以下此话
    本文首次发布于CSDN平台
    作者是CSDN@日星月云
    博客主页是https://jsss-1.blog.csdn.net
    禁止其他平台发布时删除以上此话

    推荐

    如果简历的话,看一下简历

    以下题目都是本人亲自想出来的

    如果你能信手拈来,那你绝对没有问题的。

    考研模拟面试-答案

    前面的问题

    请简单的自我介绍一下

    你的家乡是哪?(请用英文介绍一下你的家乡)

    本科专业是什么?(如果有,你为什么转专业?)
    你是如何学习你的专业的?(考查学习方法)

    遇到问题,你通常是怎么解决的?(考查解决困难的方法)
    自己解决不了怎么办?查资料,问别人?
    建议:3小时期限解决不了,就问其他人。

    通用问题

    你学过什么编程语言?
    你认为他们之间的区别是什么?
    如果提到了面向对象:
    可问:你是怎么理解面向对象的
    提示:特点或者其他理解都行

    你本科课程的专业课是什么?
    你认为你学的最好的一门课程是什么?

    专业题

    根据他的简历和问题的回答,
    问其中一个或几个

    数据结构

    你学过什么数据结构

    栈和队列的区别

    字符串的匹配算法
    提示:BP算法和KMP算法
    跳转:手写题

    树的表示法
    提示:双亲表示法,孩子表示法,兄弟孩子表示法

    树的遍历算法
    提示:前序,中序,后序

    问给定哪些就可以确定二叉树?
    跳转:手写题

    哈夫曼树的创建算法
    跳转:手写题

    二叉树和森林的转换

    图的存储算法:
    提示:邻接矩阵和邻接表
    区别:稀疏图和稠密图

    最短路径的算法
    提示:Dijkstra和Floyd

    最小生成树的生成算法
    提示:Prim和Kruskal
    区别:稀疏图和稠密图

    拓扑排序算法
    提示:找到入度为0的点入栈,
    如果栈不为空,从栈中弹出,并删除他所对应的边,如果入度变为0就入栈
    判断:栈是否为空和结点是否全部遍历

    有哪些查找算法
    提示:顺序,折半,hash
    跳转:手写题

    有哪些稳定的排序算法
    提示:冒泡、插入、归并、基数

    冒泡排序的优化
    提示:没有交换就返回

    快速排序的过程和特点,时间复杂度:O(nlog(n))
    特点:第n趟排序至少有n个数到其最终位置上
    跳转:手写题

    计算机网络

    计算机网络有哪几层?
    各层的功能和协议

    IP地址的分类
    为什么出现无分类的
    如果提到了IPv4的地址不够用,
    可问:除了无分类还有哪些技术解决不够用?
    提示:IPv6
    课文:IPv6的地址大小是IPv4的多少倍?
    提示:128/32=4倍
    跳转:手写题

    UDP和TCP的区别?

    TCP的三握四挥

    TCP如何保证可靠性

    DNS的过程

    Http的优化

    Http和Https的区别

    操作系统

    有哪些进程调度算法
    跳转:手写题

    你对虚拟内存的理解
    有哪些页面置换算法
    跳转:手写题

    有哪些磁盘调度算法

    数据库

    left join和right join的区别

    数据库的三大范式
    有没有需要违反范式的设计

    数据库的范式
    第一范式:列不可再分
    第二范式:非主属性完全依赖主键
    第三范式:非主属性之间不能有传递依赖
    反范式化:范式化是为了减少数据冗余和消除数据更新异常,
    而反范式化则是为了提高查询性能和简化复杂的查询操作。反范式化的过程可以包括合并表、增加冗余字段、创建索引等。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    数据库事务及其特性

    网络安全

    对称加密和非对称加密算法的区别
    有哪些算法

    DES、3DES、AES
    RSA
    
    
    • 1
    • 2
    • 3

    手写题

    数据结构

    前序和中序给出后序
    前序遍历A-B-D-F-G-H-I-E-C
    中序遍历F-D-H-G-I-B-E-A-C
    后序遍历F-H-I-G-D-E-B-C-A
    前序(根左右),中序(左根右),后序(左右根)

    已知一颗二叉树的先序遍历结果ABDGCEF,则其可能的后序遍历结果为()。
    A GDBEFCA
    B DGBAECF
    C BGECFDA
    D BAGDECF

    A肯定是根root节点
    F不可能是根root节点
    排除BD

    C也不可能

    A

    A BDG CEF
    - --- ---
    GDB EFC A
    --- --- -
    
    • 1
    • 2
    • 3
    • 4
                      A
                     / \
                    B   C
                   /   / \
                  D    E F
                 / 
                G    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    字符串“abaabcabc”的next值为() (蔚来 2023/08/24)

    0 1 1 2 2 3 1 2 3
    
    • 1

    Hello World的哈夫曼编码

    字母 h e l o w r d
    词频 1 1 3 2 1 1 1
    
                               [10]
    					 /                   \
                       [4]              	  [6]
                      / \	                  / \
                     [2]  o(2)             [3] 	l(3)
                     / \    	 	      / \
                  h(1)  e(1)            [2]    d(1)  
                        	            / \
                                     w(1) r(1)
    
    哈夫曼编码长度:
    10+4+6+3+2+2=27
    (2+3)*2+(1+1+1)*3+(1+1)*4=27
    
    哈夫曼编码:
    o:01
    l:11
    h:000
    e:001
    d:101
    w:1000
    r:1001
    
    “hello world”的哈夫曼编码:
    000 001 11 11 01
    1000 01 1001 11 101
    
    
    • 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

    哈希表的平均查找长度

    10 24 32 17 31 30 46 47 40 63 49
    哈希表:0~17
    哈希函数:n%16
    哈希冲突:index=i+1
    平均查找长度AVL
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    
    n		10 24 32 17 31 30 46 47 40 63 49
    n%16    10 8   0  1 15 14 14 15  8 15  1
    
    
    地址		 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17
    关键字   32 17 63 49             24 40 10          30 31 46 47
    比较次数  1  1  6  3              1  2  1           1  1  3  3
    
    
    AVL=(1*6+6+3*3+2)/11=23/11
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    假设一个数组采用快速排序,则下面的选项中,不可能是第4趟排序结果的是
    A 5,2,4,9,10,12,14
    B 14,11,9,10,6,4,2
    C 2,4,11,13,10,14,18
    D 6,8,10,12,14,2,4

    第n趟排序至少有n个数到其最终位置上
    B D
    
    • 1
    • 2

    操作系统

    总共有70,先分配20,再分配35,回收20,在分配13,再分配11,采用最佳适应算法,最大的空闲区容量是多少

    答案是9

    最佳适应算法是指空闲区从小到大排序,每次选出的适应的最小空闲分区

    解法是:

    20		20		(20)	(20)   11(9)
    --     ----     ----    ----   ----
    (50)	35       35      35     35 
           ----   	----    ----  
           (15)  	(15)     13(2)                    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    P1,P2,P3,P4四个进程到达时间和运行时间如下所示,则使用FCFS调度算法则平均周转时间是()

    进程到达时间运行时间
    P107
    P224
    P341
    P454
    平均周转时间=(7+9+8+11)/4=8.75
        到达时间 开始时间 结束时间 周转时间
    P1     0      0       7       7
    P2     2      7       11      9
    P3     4      11      12      8
    P4     5      12       16     11
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    计算机网络

    假设某网络块192.168.112.0中存在3台主机 A 、 B 、 C 。其中主机 A 的 IP 地址为192.168.112.40,主机 B 的 IP 地址为192.168.112.72,主机 C 的 IP 地址为192.168.112.100,如果主机 A 、 B 、 C 分别属于不同的网段,且 A 、 B 、 C 共同的子网码是255.255.255.224。则下列可能与主机 A 属于同一网段的 IP 地址有哪些
    A 192.168.112.32
    B 192.168.112.36
    C 192.168.112.40
    D 192.168.112.60

    同一网段的IP ,IP与子网掩码进行与操作,等于网络前缀即可

    224=11100000
    2^5=32
    0~31 32~63 ...
    其中网络地址:32 广播地址是63
    
    • 1
    • 2
    • 3
    • 4

    当前的cwnd=8,下一个RTT之后可能会是多少

    答案是:16,当前门限值,9,1,4或7
    
    如果是慢启动阶段 
    	当前门限制>=16 ,下一个cwnd是16;
    	8<当前门限制<16,下一个cwnd是当前门限值(9-15中的一个);
    如果是拥塞避免阶段,当前门限制<=8
    9
    如何是超时重传,新门限值=4
    1
    如果是快速重传,新门限值=4
    4或7
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    代码题

    用你熟悉的语言实现?(最好是C语言)

    基础代码题

    请用链表实现一个栈
    提示:实现入栈、出栈、取栈顶元素

    #include
    #include 
    #define FALSE 0
    #define TRUE 1
    
    //链栈的C语言定义如下。
    typedef int DataType;
    typedef struct Stacknode{
    	DataType data;
    	struct Stacknode * next;
    }slStacktype;
    
    // 初始化
    slStacktype* Init(){
    	slStacktype *p;
    	if((p=(slStacktype * )malloc(sizeof( slStacktype)))==NULL) 
    	 	return NULL;
    	return p;
    } 
    
    //(1)入栈操作
    //将元素x压入链栈top中
    //要求:成功返回TRUE 失败返回FALSE 
    int PushLstack(slStacktype * top, DataType x){	
    
    }
    
    //(2)出栈操作
    //从链栈top中删除栈顶元素
    //要求:成功返回元素 失败返回-1 
    DataType PopLstack(slStacktype * top){
    
    }
    
    //取栈顶元素
    //要求:成功返回元素 失败返回-1 
    DataType GetLsPop(slStacktype * top){
    
    }
    
    //测试 
    int main(){
    	slStacktype *sl=Init();
    	
    	int x=1;
    	PushLstack(sl,x);
    	
    	int y=GetLsPop(sl);
    	printf("%d\n",y);//1 
    	
    	int z=PopLstack(sl);
    	printf("%d\n",z);//1
    	
    }
    
    • 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

    请用数组实现一个循环队列
    提示:实现入队、出队、判断队空

    补充:
    解决假溢出问题的三个方法
    来自数据结构算法习题三的综合体中的7 8

    1. 少用一个元素空间:(rear+1)%MAXSIZE==front
    2. 设置队尾元素位置rear和队中元素的个数quelen
    3. 设置标志位tag,区别队满队空
    #include
    #include 
    #define FALSE 0
    #define TRUE 1
    
    
    #define MAXSIZE 10
    //下面的循环以列及操作依据少用个元素空间来实现
    //循环队列的类型定义及基本运算如下。
    typedef int ElemType;
    typedef struct{	
    	ElemType elem [MAXSIZE];//队列的存储区
    	//队头队尾指针
    	int front, rear;	
    }CSeQueue;//循环队列
    
    
    //(1)置空队
    CSeQueue * IniseQueue(){
    	CSeQueue * q=(CSeQueue *)malloc(sizeof(CSeQueue));
    	q->front=q->rear=MAXSIZE-1;
    	return q;
    }
    
    //(2)入队
    //要求:入队失败返回 FALSE 成功返回 TRUE
    int InSeQueue( CSeQueue * q,ElemType x){
    
    }
    
    //(3)出队
    //要求:出队失败返回 FALSE 成功返回 TRUE
    int OutSeQueue( CSeQueue *q , ElemType *x){
    
    }
    
    //(4) 判断队空
    //要求:队非空返回 FALSE 空返回 TRUE
    int EmptySeQueue(CSeQueue *q){
    
    } 
    
    int main(){
    	CSeQueue *cs=IniseQueue();
    	
    	int x=1;
    	InSeQueue(cs,x);
    	printf("%d\n",EmptySeQueue(cs));//0
    	
    	int x0;
    	OutSeQueue(cs,&x0);
    	printf("%d\n",x0);//1
    	
    	printf("%d\n",EmptySeQueue(cs));//1
    	
    }
    
    • 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

    实现一个Hash表
    提示:除留余数法+开放地址法
    可问:平均查找长度

    提示:编写[算法8-12]哈希表的查找HashSearch

    #include
    #include
    #define HASHSIZE 11
    #define INIT -1
    typedef int otherdata; 
    typedef struct{
    	int key;
    	otherdata other;
    }Datatype;
    
    
    // [算法8-10]采用除留余数法构造哈希函数
    int HashFunc(int key){
    	return key%HASHSIZE;
    }
    //[算法8-11]采用线性探测再散列处理冲突
    int Collision(int di){
    	return(di+1)%HASHSIZE;
    }
    //[算法8-12]哈希表的查找
    int HashSearch(Hashtable ht, Datatype x){
    
    }
    
    //[算法8-13]  哈希表的插入
    int HashInsert( Hashtable ht,Datatype x)  {
    	
    	int address;
    	address=HashSearch(ht,x);  
    	if(address>=0)return 0;
    	int times=1;
    	address= HashFunc(x.key);//计算散列地址
    	while(ht[address].data.key!=INIT){
    		address=Collision(address);//没找到,处理冲突
    		times++;
    	}
    		
    	ht[address].data=x;
    	ht[address].times=times;
    	return 1;
    }
    //[算法8-14]哈希表的创建
    
    void Createht(Hashtable ht, Datatype L[],int n){
    	int i;
    	for(i=0;i<HASHSIZE;i++){
    		ht[i].data.key=INIT;
    		ht[i].times=0;
    	}
    	
    	for(i=0;i<n;i++)
    		HashInsert(ht,L[i]);
    
    }
    
    
    
    //输出
    void output(Hashtable ht){
    	printf("输出散列表\n") ; 
    	int i;
    	printf("散列地址 关键字值 比较次数\n"); 
    	for(i=0;i<HASHSIZE;i++){
    		printf("%8d %8d %8d\n",i,ht[i].data.key,ht[i].times);
    	}
    } 
    void CreateData(Datatype L[],int data[],int n){
    	int i;
    	for(i=0;i<n;i++){
    		L[i].key=data[i];
    	}
    } 
    void printData(Datatype L[],int n){
    	int i;
    	for(i=0;i<n;i++){
    		printf("%d ",L[i].key);
    	}
    }
    // 19,01,23,14,55,68,11,82,36
    int main(){
    	Hashtable ht;
    	Datatype L[9]={0};
    	int data[9]={19,1,23,14,55,68,11,82,36};
    	CreateData(L,data,9);
    	printData(L,9); 
    
    	Createht(ht,L,9);
    	output(ht);
    	return 0;
    }
    
    
    • 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
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91

    其他代码题

    回文或括号匹配
    实现计算器:操作数栈和操作符栈
    实现KMP算法

    二叉树的遍历算法(非递归)
    实现哈夫曼树

    实现图的遍历
    实现拓扑排序
    实现最短路径

    实现折半查找
    实现快速排序

    后面的问题

    你认为你最有荣誉感或成就感的一件事?
    你对自己的评价

    对你最有帮助的人
    你最敬佩的老师或同学,敬佩的点是什么
    如果是班干部,可问:你认为怎么做好一个班干部?

    你为什么选择我们学校?
    你对研究生生活的期待是什么样的?

    补充题目

    你有什么反问我的吗?

    你有什么其他问题想问后面的同学吗?
    给我的题库做补充。

    基础代码题答案

    链栈

    D:\大学学习\3-大二上学习\数据C\学习\3\3-链栈.c

    #include
    #include 
    #define FALSE 0
    #define TRUE 1
    
    //链栈的C语言定义如下。
    typedef int DataType;
    typedef struct Stacknode{
    	DataType data;
    	struct Stacknode * next;
    }slStacktype;
    
    // 初始化
    slStacktype* Init(){
    	slStacktype *p;
    	if((p=(slStacktype * )malloc(sizeof( slStacktype)))==NULL) 
    	 	return NULL;
    	return p;
    } 
    
    //(1)入栈操作
    //将元素x压入链栈top中
    int PushLstack(slStacktype * top, DataType x){	
    	slStacktype *p;
    	//申请一个结点
    	if((p=(slStacktype * )malloc(sizeof( slStacktype)))==NULL) 
    	 	return FALSE;
    	p->data=x;
    	p->next=top->next;
    	top->next=p;
    	return TRUE;
    }
    
    //(2)出栈操作
    //从链栈top中删除栈顶元素
    DataType PopLstack(slStacktype * top){
    	slStacktype * p;
    	DataType x;
    	if(top->next==NULL){//空栈  
    		printf("此栈为空!");  
    		return -1;
    	}
    	p=top->next;
    	top->next=p->next;  
    	x=p->data;
    	free(p);
    	return x;
    }
    
    //取栈顶元素
    DataType GetLsPop(slStacktype * top){
    	if(top->next==NULL){//空栈  
    		printf("此栈为空!");  
    		return -1;
    	}
    	DataType x=top->next->data;
    	return x;
    }
    
    int main(){
    	slStacktype *sl=Init();
    	
    	int x=1;
    	PushLstack(sl,x);
    	
    	int y=GetLsPop(sl);
    	printf("%d\n",y);//1 
    	
    	int z=PopLstack(sl);
    	printf("%d\n",z);//1
    	
    }
    
    • 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

    循环队列1

    D:\大学学习\3-大二上学习\数据C\学习\3\6-循环队列.c

    #include
    #include 
    #define FALSE 0
    #define TRUE 1
    
    
    #define MAXSIZE 10
    //下面的循环以列及操作依据少用个元素空间来实现
    //循环队列的类型定义及基本运算如下。
    typedef int ElemType;
    typedef struct{	
    	ElemType elem [MAXSIZE];//队列的存储区
    	//队头队尾指针
    	int front, rear;	
    }CSeQueue;//循环队列
    
    
    //(1)置空队
    CSeQueue * IniseQueue(){
    	CSeQueue * q=(CSeQueue *)malloc(sizeof(CSeQueue));
    	q->front=q->rear=MAXSIZE-1;
    	return q;
    }
    
    //(2)入队
    int InSeQueue( CSeQueue * q,ElemType x){
    	if((q->rear+1)%MAXSIZE==q->front){//队满不能人队
    		printf("队满");
    		return FALSE;
    	}else{
    		q->rear=(q->rear+1)%MAXSIZE;
    		q->elem[q->rear]=x;
    		return TRUE;//人队完成
    	}
    }
    
    //(3)出队
    int OutSeQueue( CSeQueue *q , ElemType *x){
    	if(q->front==q->rear){
    		printf("队空");
    		return FALSE;
    	}else{
    		q->front=(q->front+1)%MAXSIZE;
    		*x=q->elem[q->front];//读出队头元素
    		return TRUE;//出队完成
    	}
    }
    
    //(4) 判断队空
    int EmptySeQueue(CSeQueue *q){
    	if(q->front==q->rear) return TRUE;
    	else return FALSE;
    } 
    
    int main(){
    	CSeQueue *cs=IniseQueue();
    	
    	int x=1;
    	InSeQueue(cs,x);
    	printf("%d\n",EmptySeQueue(cs));//0
    	
    	int x0;
    	OutSeQueue(cs,&x0);
    	printf("%d\n",x0);//1
    	
    	printf("%d\n",EmptySeQueue(cs));//1
    	
    }
    
    • 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

    循环队列2

    D:\大学学习\3-大二上学习\数据C\习题\3\8.c

    #include
    #include 
    #define FALSE 0
    #define TRUE 1
    
    
    #define MAXSIZE 2
    //下面的循环以列及操作依据少用个元素空间来实现
    //循环队列的类型定义及基本运算如下。
    typedef int ElemType;
    typedef struct{	
    	ElemType elem [MAXSIZE];//队列的存储区
    	//队头队尾指针
    	int front, rear;
    	int flag;	
    }CSeQueue;//循环队列
    
    
    //(1)置空队
    CSeQueue * IniseQueue(){
    	CSeQueue * q=(CSeQueue *)malloc(sizeof(CSeQueue));
    	q->front=q->rear=MAXSIZE-1;
    	q->flag=0;
    	return q;
    }
    
    //(2)入队
    int InSeQueue( CSeQueue * q,ElemType x){
    	if(ManSeQueue(q)){//队满不能人队
    		printf("队满");
    		return FALSE;
    	}else{
    		q->rear=(q->rear+1)%MAXSIZE;
    		q->elem[q->rear]=x;
    		q->flag=1;
    		return TRUE;//人队完成
    	}
    
    }
    
    //(3)出队
    int OutSeQueue( CSeQueue *q , ElemType *x){
    	if(EmptySeQueue(q)){
    		printf("队空");
    		return FALSE;
    	}else{
    		q->front=(q->front+1)%MAXSIZE;
    		*x=q->elem[q->front];//读出队头元素
    		q->flag=0;
    		return TRUE;//出队完成
    	}
    }
    
    //(4) 判断队空
    int EmptySeQueue(CSeQueue *q){
    	if(q->front==q->rear&&q->flag==0) return TRUE;
    	else return FALSE;
    } 
    
    //判断队满
    int ManSeQueue(CSeQueue *q){
    	if(q->front==q->rear&&q->flag==1) return TRUE;
    	else return FALSE;
    } 
    int main(){
    	CSeQueue *cs=IniseQueue();
    	printf("%d",EmptySeQueue(cs));//1
    	
    	int x1=1;
    	InSeQueue(cs,x1);
    	
    	int x2=2;
    	InSeQueue(cs,x2);
    	
    	printf("%d",ManSeQueue(cs));//1
    	
    	int y1;
    	OutSeQueue(cs,&y1);
    	printf("%d",y1);//1
    	
    	printf("%d",EmptySeQueue(cs));//0
    	
    	int y2;
    	OutSeQueue(cs,&y2);
    	printf("%d",y2);//2
    	
    	printf("%d",EmptySeQueue(cs));//1
    	
    }
    
    • 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
    • 86
    • 87
    • 88
    • 89

    哈希表

    D:\大学学习\3-大二上学习\数据C\学习\8\4-哈希表查找.c

    #include
    #include
    #define HASHSIZE 11
    #define INIT -1
    typedef int otherdata; 
    typedef struct{
    	int key;
    	otherdata other;
    }Datatype;
    typedef struct{
    	Datatype data;
    	int times;//比较次数
    }Hashtable[HASHSIZE];
    
    // [算法8-10]采用除留余数法构造哈希函数
    int HashFunc(int key){
    	return key%HASHSIZE;
    }
    //[算法8-11]采用线性探测再散列处理冲突
    int Collision(int di){
    	return(di+1)%HASHSIZE;
    }
    //[算法8-12]哈希表的查找
    int HashSearch(Hashtable ht, Datatype x){
    	int address;
    	address= HashFunc(x.key);//计算散列地址
    	while(ht[address].data.key!=INIT&&ht[address].data.key!=x.key)
    		address=Collision(address);//没找到,处理冲突
    	if(ht[address].data.key==x.key) return address;//查找成功
    	else return -1;//查找失败
    }
    
    //[算法8-13]  哈希表的插入
    int HashInsert( Hashtable ht,Datatype x)  {
    	
    	int address;
    	address=HashSearch(ht,x);  
    	if(address>=0)return 0;
    	int times=1;
    	address= HashFunc(x.key);//计算散列地址
    	while(ht[address].data.key!=INIT){
    		address=Collision(address);//没找到,处理冲突
    		times++;
    	}
    		
    	ht[address].data=x;
    	ht[address].times=times;
    	return 1;
    }
    //[算法8-14]哈希表的创建
    
    void Createht(Hashtable ht, Datatype L[],int n){
    	int i;
    	for(i=0;i<HASHSIZE;i++){
    		ht[i].data.key=INIT;
    		ht[i].times=0;
    	}
    	
    	for(i=0;i<n;i++)
    		HashInsert(ht,L[i]);
    
    }
    
    #define DEL -1
    //[算法8-15]  哈希表的删除
    
    int HashDel(Hashtable ht, Datatype x){
    	int address;
    	address = HashFunc(x.key);  
    	if(address>=0){	//找到			
    		ht[address].data.key=DEL;//置删除标志
    		return 1;
    	}
    
    	return 0;
    }
    
    //输出
    void output(Hashtable ht){
    	printf("输出散列表\n") ; 
    	int i;
    	printf("散列地址 关键字值 比较次数\n"); 
    	for(i=0;i<HASHSIZE;i++){
    		printf("%8d %8d %8d\n",i,ht[i].data.key,ht[i].times);
    	}
    } 
    void CreateData(Datatype L[],int data[],int n){
    	int i;
    	for(i=0;i<n;i++){
    		L[i].key=data[i];
    	}
    } 
    void printData(Datatype L[],int n){
    	int i;
    	for(i=0;i<n;i++){
    		printf("%d ",L[i].key);
    	}
    }
    // 19,01,23,14,55,68,11,82,36
    int main(){
    	Hashtable ht;
    	Datatype L[9]={0};
    	int data[9]={19,1,23,14,55,68,11,82,36};
    	CreateData(L,data,9);
    	printData(L,9); 
    	printf("\n"); 
    	
    	Createht(ht,L,9);
    	output(ht);
    	return 0;
    }
    
    
    • 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
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112

    最后

    2023-10-19 13:39:45

    我们都有光明的未来

    祝大家考研上岸
    祝大家工作顺利
    祝大家得偿所愿
    祝大家如愿以偿
    点赞收藏关注哦

  • 相关阅读:
    数据结构 二叉树
    算法-链表-简单-相交、反转、回文、环形、合并
    神经网络 深度神经网络,双隐层神经网络结构
    Altium Designer学习笔记6
    【C】栈和队列
    VLANIF配置
    背包问题学习笔记-01背包
    选择最佳路线(单源最短路扩展应用)
    Java从控制台接收用户输入的一行英文句子,把句子的最前面两个单词移到句子的最后面去,并整理句子的大小写及标点符号,将新的句子输出
    基于 Iterative 映射和单纯形法的改进灰狼优化算法-附代码
  • 原文地址:https://blog.csdn.net/qq_51625007/article/details/133923817