C语言中常用的数据结构

  1. 数组(Array):一组相同类型的数据元素按一定顺序排列而成的数据结构。

  2. 链表(Linked List):由一系列结点组成的数据结构,每个结点包含数据和指向下一个结点的指针。

  3. 栈(Stack):一种特殊的线性数据结构,只能在栈顶进行插入和删除操作。

  4. 队列(Queue):一种先进先出的线性数据结构,可以在队尾插入元素,在队头删除元素。

  5. 树(Tree):由一组结点和一组连接这些结点的边组成的数据结构,每个结点有一个父结点和零个或多个子结点。

  6. 图(Graph):由一组结点和一组连接这些结点的边组成的数据结构,每个结点可以有零个或多个相邻结点。

  7. 哈希表(Hash Table):一种根据关键字直接访问内存位置以提高查找效率的数据结构,通常用于实现字典、集合等数据类型。                                                                            

 

  1. 数组(Array)

数组是一种线性数据结构,它由一组相同类型的元素按照一定顺序排列而成。C语言中的数组是静态的,即数组的大小在编译时就确定了,不能动态地改变。

示例代码:

int arr[5] = {1, 2, 3, 4, 5}; // 定义一个包含5个元素的整型数组
for (int i = 0; i < 5; i++) {
    printf("%d ", arr[i]); // 输出数组中的元素
}
  1. 链表(Linked List)

链表是一种动态的数据结构,它由一系列结点组成,每个结点包含数据和指向下一个结点的指针。链表中的结点可以在运行时动态地分配和释放,因此它的大小可以根据需要动态地改变。

示例代码:

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

struct Node* head = NULL; // 定义链表头结点

void insert(int data) {
    struct Node* node = (struct Node*) malloc(sizeof(struct Node)); // 动态分配结点内存
    node->data = data;
    node->next = head; // 将新结点插入链表头部
    head = node;
}

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

int main() {
    insert(5);
    insert(4);
    insert(3);
    insert(2);
    insert(1);
    print(); // 输出链表元素
    return 0;
}
  1. 栈(Stack)

栈是一种特殊的线性数据结构,它只能在栈顶进行插入和删除操作。栈的插入操作称为入栈,删除操作称为出栈。栈的特点是后进先出(Last In First Out,LIFO)。

示例代码:

#define MAX_SIZE 100

int stack[MAX_SIZE];
int top = -1; // 栈顶指针

void push(int data) {
    if (top == MAX_SIZE - 1) {
        printf("Stack overflow");
        return;
    }
    stack[++top] = data; // 将元素入栈
}

int pop() {
    if (top == -1) {
        printf("Stack underflow");
        return -1;
    }
    return stack[top--]; // 将元素出栈
}

int main() {
    push(1);
    push(2);
    push(3);
    printf("%d ", pop()); // 输出3
    printf("%d ", pop()); // 输出2
    printf("%d ", pop()); // 输出1
    return 0;
}
  1. 队列(Queue)

队列是一种先进先出(First In First Out,FIFO)的线性数据结构,它可以在队尾插入元素,在队头删除元素。队列的插入操作称为入队,删除操作称为出队。

示例代码:

#define MAX_SIZE 100

int queue[MAX_SIZE];
int front = -1, rear = -1; // 队头和队尾指针

void enqueue(int data) {
    if (rear == MAX_SIZE - 1) {
        printf("Queue overflow");
        return;
    }
    if (front == -1) {
        front = 0;
    }
    queue[++rear] = data; // 将元素入队
}

int dequeue() {
    if (front == -1 || front > rear) {
        printf("Queue underflow");
        return -1;
    }
    return queue[front++]; // 将元素出队
}

int main() {
    enqueue(1);
    enqueue(2);
    enqueue(3);
    printf("%d ", dequeue()); // 输出1
    printf("%d ", dequeue()); // 输出2
    printf("%d ", dequeue()); // 输出3
    return 0;
}
  1. 树(Tree)

树是一种由一组结点和一组连接这些结点的边组成的数据结构,每个结点有一个父结点和零个或多个子结点。树的根结点没有父结点,每个叶子结点没有子结点。

示例代码:

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

struct Node* createNode(int data) {
    struct Node* node = (struct Node*) malloc(sizeof(struct Node)); // 动态分配结点内存
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    return node;
}

int main() {
    struct Node* root = createNode(1);
    root->left = createNode(2);
    root->right = createNode(3);
    root->left->left = createNode(4);
    root->left->right = createNode(5);
    return 0;
}
  1. 图(Graph)

图是一种由一组结点和一组连接这些结点的边组成的数据结构,每个结点可以有零个或多个相邻结点。图可以是有向的或无向的,有权的或无权的。

示例代码:

#define MAX_SIZE 100

int graph[MAX_SIZE][MAX_SIZE];
int numVertices = 0;

void addEdge(int u, int v) {
    graph[u][v] = 1; // 在u和v之间添加一条边
    graph[v][u] = 1; // 如果是无向图,还需添加一条v到u的边
}

int main() {
    numVertices = 5; // 图中的结点数
    addEdge(0, 1);
    addEdge(0, 4);
    addEdge(1, 2);
    addEdge(1, 3);
    addEdge(1, 4);
    addEdge(2, 3);
    addEdge(3, 4);
    return 0;
}
  1. 哈希表(Hash Table)

哈希表是一种根据关键字直接访问内存位置以提高查找效率的数据结构,通常用于实现字典、集合等数据类型。哈希表的核心是哈希函数,它将关键字映射到一个固定大小的数组索引上。

示例代码:

#define MAX_SIZE 100

struct Node {
    int key;
    int value;
};

struct Node* hashTable[MAX_SIZE];

int hashFunction(int key) {
    return key % MAX_SIZE; // 哈希函数
}

void insert(int key, int value) {
    int index = hashFunction(key);
    struct Node* node = (struct Node*) malloc(sizeof(struct Node)); // 动态分配结点内存
    node->key = key;
    node->value = value;
    hashTable[index] = node; // 在哈希表中插入键值对
}

struct Node* search(int key) {
    int index = hashFunction(key);
    return hashTable[index]; // 在哈希表中查找键值对
}

int main() {
    insert(1, 10);
    insert(2, 20);
    insert(3, 30);
    struct Node* node = search(2);
    printf("%d", node->value); // 输出20
    return 0;
}

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://www.net2asp.com/d4cac317ab.html