海印网
海印网

C语言算法问答集:掌握基础数据结构和算法

admin数码00

在 c 语言中实现链表:创建一个typedef struct node的结构体,其中包含data和next成员,并使用指向链表头部的指针head。在 c 语言中实现栈:使用整形数组stack和顶部指针top,push()函数在栈顶添加元素,pop()函数从栈顶移除并返回元素。在 c 语言中实现队列:使用整形数组queue和front、rear指针,enqueue()函数在队尾添加元素,dequeue()函数从队头移除并返回元素。在 c 语言中实现二叉树:创建一个typedef struct node的结构体,其中包含data、left和right成员,并使用指向二叉树根节点的指针root。5.

C语言算法问答集:掌握基础数据结构和算法-第1张图片-海印网

C语言算法问答集:掌握基础数据结构和算法

问:如何在C语言中实现一个链表?

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

Node* head = NULL;  // 指向链表头的指针

登录后复制

问:如何用C语言实现一个栈?

立即学习“C语言免费学习笔记(深入)”;

#define MAX_SIZE 100

int stack[MAX_SIZE];
int top = -1;

void push(int value) {
    if (top == MAX_SIZE - 1) {
        printf("Stack overflow!");
    } else {
        stack[++top] = value;
    }
}

int pop() {
    if (top == -1) {
        printf("Stack underflow!");
        return -1;
    } else {
        return stack[top--];
    }
}

登录后复制

问:如何用C语言实现一个队列?

#define MAX_SIZE 100

int queue[MAX_SIZE];
int front = -1;
int rear = -1;

void enqueue(int value) {
    if ((front == 0 && rear == MAX_SIZE - 1) || (rear == (front - 1) % (MAX_SIZE - 1))) {
        printf("Queue overflow!");
    } else {
        if (front == -1) {
            front = rear = 0;
        } else if (rear == MAX_SIZE - 1 && front != 0) {
            rear = 0;
        } else {
            rear++;
        }
        queue[rear] = value;
    }
}

int dequeue() {
    if (front == -1) {
        printf("Queue underflow!");
        return -1;
    } else {
        int value = queue[front];
        if (front == rear) {
            front = rear = -1;
        } else if (front == MAX_SIZE - 1) {
            front = 0;
        } else {
            front++;
        }
        return value;
    }
}

登录后复制

问:如何用C语言实现一个二叉树?

typedef struct Node {
    int data;
    struct Node* left;
    struct Node* right;
} Node;

Node* root = NULL;  // 指向二叉树根节点的指针

登录后复制

问:如何用C语言实现一个图?

typedef struct Graph {
    int num_vertices;
    int** adj_matrix;
} Graph;

Graph* create_graph(int num_vertices) {
    Graph* graph = (Graph*)malloc(sizeof(Graph));
    graph->num_vertices = num_vertices;
    graph->adj_matrix = (int**)malloc(num_vertices * sizeof(int*));
    for (int i = 0; i < num_vertices; i++) {
        graph->adj_matrix[i] = (int*)malloc(num_vertices * sizeof(int));
    }
    for (int i = 0; i < num_vertices; i++) {
        for (int j = 0; j < num_vertices; j++) {
            graph->adj_matrix[i][j] = 0;
        }
    }
    return graph;
}

void add_edge(Graph* graph, int src, int dest) {
    graph->adj_matrix[src][dest] = 1;
}

登录后复制

实战案例:

使用链表存储一组学生成绩,并根据成绩从小到大排序:

typedef struct Student {
    int id;
    int score;
} Student;

Node* head = NULL;

// 插入一个学生成绩
void insert(int id, int score) {
    Node* new_node = (Node*)malloc(sizeof(Node));
    new_node->data.id = id;
    new_node->data.score = score;

    if (head == NULL) {
        head = new_node;
        return;
    }

    Node* curr = head;
    while (curr->next != NULL && curr->next->data.score < score) {
        curr = curr->next;
    }

    new_node->next = curr->next;
    curr->next = new_node;
}

// 按成绩从小到大排序
void sort() {
    Node* curr = head;
    Node* next;

    while (curr != NULL) {
        next = curr->next;

        while (next != NULL) {
            if (curr->data.score > next->data.score) {
                int tmp_id = curr->data.id;
                int tmp_score = curr->data.score;

                curr->data.id = next->data.id;
                curr->data.score = next->data.score;

                next->data.id = tmp_id;
                next->data.score = tmp_score;
            }
            next = next->next;
        }
        curr = curr->next;
    }
}

登录后复制

以上就是C语言算法问答集:掌握基础数据结构和算法的详细内容,更多请关注其它相关文章!

Tags: 语言指针

Sorry, comments are temporarily closed!