西工大数据结构实验期末复习

我也不知道题库,但是听说挺简单的,所以就自己想到一些题型可能会考到~

1_合并有序数组

直接用数组实现,他也管不了我用不用链表。

#include <stdio.h>

int seq1[20];
int seq2[20];
int seq3[40];
int m, n;

void readdata();
void merge();
void printseq3();

int main(){
	readdata();
	merge();
	printseq3();
	return 0;
}

void readdata(){
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%d", &seq1[i]);
	}
	scanf("%d", &m);
	for (int i = 0; i < m; i++){
		scanf("%d", &seq2[i]);
	}
}

void merge(){
	int i = 0, j = 0, k = 0;
	while(i < n && j < m){
		if (seq1[i] < seq2[j]){
			seq3[k] = seq1[i];
			i++;
			k++;
		}
		else{
			seq3[k] = seq2[j];
			j++;
			k++;
		}
	}
	while (i < n){
		seq3[k] = seq1[i];
		i++;
		k++;
	}
	while (j < m){
		seq3[k] = seq2[j];
		k++;
		j++;
	}
	return;
}

void printseq3(){
	for (int i = 0; i < m+n-1; i++){
		printf("%d\n", seq3[i]);
	}
	printf("%d", seq3[m+n-1]);
}

2_排序类

排序都用快排实现。

#include <stdio.h>

#define MAXSIZE 10000

void readdata();
int sort(int seq[], int left, int right);
void quicksort(int seq[], int left, int right);
void print(int seq[]);

int seq[MAXSIZE];
int length;

int main(){
	readdata();
	quicksort(seq, 0, length-1);
	print(seq);
}
void readdata(){
	scanf("%d", &length);
	for (int i = 0; i < length; i++){
		scanf("%d", &seq[i]);
	}
}

int sort(int seq[], int left, int right){
	int i = left; 
	int j = right; 
	int tmp = seq[i];
	while (i != j){
		while (i < j && tmp <= seq[j]){
			j--;
		}
		seq[i] = seq[j];
		while (i < j && tmp >= seq[i]){
			i++;
		}
		seq[j] = seq[i];
	}
	seq[i] = tmp;
	return i;
}

void quicksort(int seq[], int left, int right){
	if (left < right){
		int i = sort(seq, left, right);
		quicksort(seq, left, i-1);
		quicksort(seq, i+1, right);
	}
}

void print(int seq[]){
	for (int i = 0; i < length; i++){
		printf("%d", seq[i]);
	}
}

3_三元组实现稀疏矩阵转置

按理来说需要限制一下行和列的数组溢出,但是我不管,他肯定不会给超出的测试用例(我们小镇做题家是这样的)

#include <stdio.h>
#define MAXSIZE 20

int matrix[20][3];
int n, m;

void readdata(){
	scanf("%d %d", &n, &m);
	int i = 0;
	while (1){
		scanf("%d %d %d", &matrix[i][0], &matrix[i][1], &matrix[i][2]);
		if (matrix[i][0] == 0 && matrix[i][1] == 0 && matrix[i][2] == 0){
			break;
		}
		i++;
	}
}

void transpose(int matrix[20][3]){
	int i = 0;
	while (1){
		int tmp = matrix[i][1];
		matrix[i][1] = matrix[i][2];
		matrix[i][2] = tmp;
		if (matrix[i][0] == 0 && matrix[i][1] == 0 && matrix[i][2] == 0){
			break;
		}
		i++;
	}
}

void printmatrix(int mat[20][3]){
	int i = 0;
	while(1){
		printf("%d %d %d\n", mat[i][0], mat[i][1], mat[i][2]);
		if (mat[i][0] == 0 && mat[i][1] == 0 && mat[i][2] == 0){
			break;
		}
		i++;
	}
}

int main(){
	readdata();
	transpose(matrix);
	printmatrix(matrix);
	return 0;
}

4_二分查找有序数组元素

要记住中间一点细节:

1.不用像快排那样用函数递归调用来解,只用写一个while循环语句,再实时更新left & right的值就可以了。再有一个while的条件一定要是left<=right,否则会出问题。

#include <stdio.h>
#define MAXSIZE 10000

int seq[MAXSIZE];
int length;
int des_num;

void readdata(){
	scanf("%d", &length);
	for (int i = 0; i < length; i++){
		scanf("%d", &seq[i]);
	}
	scanf("%d", &des_num);
} 

int bisearch(int seq[], int des, int length){
	int left = 0;
	int right = length-1;
	while(left <= right){//left <= right 是必须的,否则导致输出错误 
		int mid = (left + right)/2;
		if (seq[mid] == des){
			return mid;
		}
		else if (des > seq[mid]){
			left = mid+1;
		}
		else if (des < seq[mid]){
			right = mid-1;
		}
	}
	return -1;
}

int main(){
	readdata();
	int i = bisearch(seq, des_num, length);
	printf("%d", i+1);
}

5_Floyd算法求任意两点的最短路径

哎不是哥们,数据结构实验考试应该不会对时间卡的太死吧~我就只学这个啦!哈哈啊哈!floyd真是比迪基简单好多呢!

#include <stdio.h>
#define MAXSIZE 100
#define INF 10000

int Node_num;
int dist[MAXSIZE][MAXSIZE];

void readdata(){
	scanf("%d", &Node_num);
	for (int i = 0; i < Node_num; i++){
		for (int j = 0; j < Node_num; j++){
			scanf("%d", &dist[i][j]);
		}
	}
}

void floyd(int dist[MAXSIZE][MAXSIZE]){
	for (int k = 0; k < Node_num; k++){
		for (int j = 0; j < Node_num; j++){
			for (int i = 0; i < Node_num; i++){
				if (dist[j][i] > dist[j][k]+dist[k][i]){
					dist[j][i] = dist[j][k]+dist[k][i];
				}
			}
		}
	}
}

void print(int dist[MAXSIZE][MAXSIZE]){
	for (int i = 0; i < Node_num; i++){
		for (int j = 0; j < Node_num; j++){
			printf("%d ", dist[i][j]);
		}
		printf("\n"); 
	}
}

int main(){
	readdata();
	floyd(dist);
	print(dist);
}

6_栈的基本操作(可以用来混分嘻嘻^ ^)

#include<stdio.h>
#define MAXSIZE 10000

typedef struct Stack{
	int num[];
	int top;
}Stack;

void init(Stack *s){
	s->top = -1;
}

int isEmpty(Stack *s){
	return s->top == -1;
}

int isFull(Stack *s){
	return s->top == MAXSIZE-1;
}

void push(Stack *s, char x){
	if (isFull(s)){
		return;
	}
	else{
		s->num[++s->top] = x;
	}
}

char pop(Stack *s){
	if (isEmpty(s)){
		return;
	}
	else{
		return s->num[--s->top];
	}
}

char getTop(Stack *s){
	if (isEmpty(s)){
		return;
	}
	else{
		return s->num[s->top];
	}
}

7_括号匹配问题

#include <stdio.h>
#include <stdlib.h> // 用于 malloc
#include <stdbool.h> // 用于 bool 类型

#define MAXSIZE 10000

typedef struct Stack {
    char num[MAXSIZE]; // 固定大小数组
    int top;
} Stack;

void init(Stack *s) {
    s->top = -1;
}

int isEmpty(Stack *s) {
    return s->top == -1;
}

int isFull(Stack *s) {
    return s->top == MAXSIZE - 1;
}

void push(Stack *s, char x) {
    if (isFull(s)) {
        return;
    }
    else {
        s->num[++s->top] = x;  //++,--的前后顺序不能错 
    }
}

char pop(Stack *s) {
    if (isEmpty(s)) {
        return -1;
    }
    else {
        return s->num[s->top--];
    }
}

char getTop(Stack *s) {
    if (isEmpty(s)) {
        return -1;
    }
    else {
        return s->num[s->top];
    }
}

void readdata(char expression[MAXSIZE]) {
    int i = 0;
    char ch;
    while ((ch = getchar()) != '\n' && ch != EOF) {
        expression[i++] = ch;
    }
    expression[i] = '\0';
}

bool isMatched(char left, char right) {
    return (left == '(' && right == ')') || 
           (left == '[' && right == ']') || 
           (left == '{' && right == '}');
}

bool legal(char expression[MAXSIZE]) {
    Stack s;
    init(&s);
    for (int i = 0; expression[i] != '\0'; i++) {
        if (expression[i] == '(' || expression[i] == '[' || expression[i] == '{') {
            push(&s, expression[i]);
        }
        else if (expression[i] == ')' || expression[i] == ']' || expression[i] == '}') {
            if (isEmpty(&s) || !isMatched(pop(&s), expression[i])) {
                return false;
            }
        }
    }
    return isEmpty(&s);
}

int main() {
    char expression[MAXSIZE];
    readdata(expression);
    if (legal(expression)) {
        printf("yes\n");
    }
    else {
        printf("no\n");
    }
    return 0;
}

8_dfs

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10000

int start_Node, end_Node;
typedef struct Graph{
	int Node_num;
	int Edge_num;
	int mat[MAXSIZE][MAXSIZE];
}Graph;

void readdata(Graph *p){
	scanf("%d %d", &p->Node_num, &p->Edge_num);
	int vi, vj;
	for (int i = 0; i < p->Edge_num; i++){
		scanf("%d %d", &vi, &vj);
		p->mat[vi][vj] = 1;
	}
	scanf("%d %d", &start_Node, &end_Node);
}

int dfs(Graph *p, int beginning){
	if (beginning == end_Node){ //不用分成两种情况,如果不是就直接将判断交给后面的递归 
		return 1;
	}
	for (int i = 0; i < p->Node_num; i++){
		if (p->mat[beginning][i] == 1){
//				dfs(p,i);
				if (dfs(p,i)){
					return 1; //需要将返回值递归到调用栈的最顶层 
				}
			}
		}
	return 0;
}

int main(){
	Graph *p = (Graph *)malloc(sizeof(Graph));
	readdata(p);
	int isConnected = dfs(p, start_Node);
	if (isConnected){
		printf("yes");
	}
	else{
		printf("no");
	}
	free(p);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值