溫馨提示×

C語言int數(shù)組與其他數(shù)據(jù)結(jié)構(gòu)的轉(zhuǎn)換

小樊
83
2024-08-27 03:16:32
欄目: 編程語言

在C語言中,int數(shù)組是一種常見的數(shù)據(jù)結(jié)構(gòu),用于存儲整數(shù)值。要將int數(shù)組與其他數(shù)據(jù)結(jié)構(gòu)進行轉(zhuǎn)換,你需要了解這些數(shù)據(jù)結(jié)構(gòu)的特點以及如何在它們之間進行轉(zhuǎn)換。以下是一些常見的數(shù)據(jù)結(jié)構(gòu)及其與int數(shù)組之間的轉(zhuǎn)換方法:

  1. 字符串(char數(shù)組)

int數(shù)組轉(zhuǎn)換為字符串:

#include<stdio.h>
#include<string.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    char str[n * 4 + 1]; // 為每個整數(shù)分配4個字符(包括負號和空格),再加上一個終止符'\0'

    for (int i = 0; i < n; i++) {
        sprintf(str + i * 4, "%d ", arr[i]);
    }
    str[n * 4] = '\0'; // 添加終止符

    printf("String: %s\n", str);
    return 0;
}
  1. 鏈表(struct定義的鏈表)

int數(shù)組轉(zhuǎn)換為鏈表:

#include<stdio.h>
#include <stdlib.h>

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

Node* create_node(int data) {
    Node* new_node = (Node*)malloc(sizeof(Node));
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}

Node* array_to_linked_list(int arr[], int n) {
    Node* head = create_node(arr[0]);
    Node* current = head;

    for (int i = 1; i < n; i++) {
        current->next = create_node(arr[i]);
        current = current->next;
    }

    return head;
}

void print_linked_list(Node* head) {
    Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    Node* linked_list = array_to_linked_list(arr, n);
    print_linked_list(linked_list);

    return 0;
}
  1. 二叉樹(struct定義的二叉樹)

int數(shù)組轉(zhuǎn)換為二叉搜索樹(BST):

#include<stdio.h>
#include <stdlib.h>

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

TreeNode* create_tree_node(int data) {
    TreeNode* new_node = (TreeNode*)malloc(sizeof(TreeNode));
    new_node->data = data;
    new_node->left = NULL;
    new_node->right = NULL;
    return new_node;
}

TreeNode* insert_into_bst(TreeNode* root, int data) {
    if (root == NULL) {
        return create_tree_node(data);
    }

    if (data< root->data) {
        root->left = insert_into_bst(root->left, data);
    } else {
        root->right = insert_into_bst(root->right, data);
    }

    return root;
}

TreeNode* array_to_bst(int arr[], int n) {
    TreeNode* root = NULL;

    for (int i = 0; i < n; i++) {
        root = insert_into_bst(root, arr[i]);
    }

    return root;
}

void inorder_traversal(TreeNode* root) {
    if (root == NULL) {
        return;
    }

    inorder_traversal(root->left);
    printf("%d -> ", root->data);
    inorder_traversal(root->right);
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    TreeNode* bst = array_to_bst(arr, n);
    inorder_traversal(bst);
    printf("NULL\n");

    return 0;
}

這些示例展示了如何將int數(shù)組轉(zhuǎn)換為其他數(shù)據(jù)結(jié)構(gòu)。請注意,這些示例僅用于演示目的,實際應(yīng)用中可能需要根據(jù)具體需求進行調(diào)整。

0