【无标题】

数据结构

链表

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

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

// 在链表开头插入节点
void push(Node** head_ref, int new_data) {
    Node* new_node = (Node*) malloc(sizeof(Node));
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}

// 在链表末尾追加节点
void append(Node** head_ref, int new_data) {
    Node* new_node = (Node*) malloc(sizeof(Node));
    Node* last = *head_ref;
    new_node->data = new_data;
    new_node->next = NULL;
    if (*head_ref == NULL) {
        *head_ref = new_node;
        return;
    }
    while (last->next != NULL)
        last = last->next;
    last->next = new_node;
}

// 打印链表
void printList(Node* node) {
    while (node != NULL) {
        printf(" %d ", node->data);
        node = node->next;
    }
}

int main() {
    Node* head = NULL;

    append(&head, 6);
    push(&head, 7);
    push(&head, 1);
    append(&head, 4);

    printf("Created Linked list is: ");
    printList(head);

    return 0;
}


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

// 定义栈节点结构
struct StackNode {
    int data;
    struct StackNode *next;
};

// 创建新节点
struct StackNode* createNode(int data) {
    struct StackNode* newNode = (struct StackNode*)malloc(sizeof(struct StackNode));
    if (!newNode) {
        perror("malloc");
        exit(1);
    }
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// 判断栈是否为空
int isEmpty(struct StackNode* root) {
    return !root;
}

// 入栈
void push(struct StackNode** root, int data) {
    struct StackNode* newNode = createNode(data);
    newNode->next = *root;
    *root = newNode;
    printf("%d pushed to stack\n", data);
}

// 出栈
int pop(struct StackNode** root) {
    if (isEmpty(*root)) {
        printf("Stack underflow\n");
        exit(1);
    }
    struct StackNode* temp = *root;
    *root = (*root)->next;
    int popped = temp->data;
    free(temp);
    return popped;
}

// 查看栈顶元素
int peek(struct StackNode* root) {
    if (isEmpty(root)) {
        printf("Stack is empty\n");
        exit(1);
    }
    return root->data;
}

int main() {
    struct StackNode* root = NULL;

    push(&root, 10);
    push(&root, 20);
    push(&root, 30);

    printf("%d popped from stack\n", pop(&root));
    printf("Top element is %d\n", peek(root));

    return 0;
}

队列

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

#define MAX 10

typedef struct {
    int front, rear, size;
    unsigned capacity;
    int* array;
} Queue;

// 创建队列
Queue* createQueue(unsigned capacity) {
    Queue* queue = (Queue*) malloc(sizeof(Queue));
    queue->capacity = capacity;
    queue->front = queue->size = 0;
    queue->rear = capacity - 1;
    queue->array = (int*) malloc(queue->capacity * sizeof(int));
    return queue;
}

// 检查队列是否已满
int isFull(Queue* queue) {
    return (queue->size == queue->capacity);
}

// 检查队列是否为空
int isEmpty(Queue* queue) {
    return (queue->size == 0);
}

// 入队操作
void enqueue(Queue* queue, int item) {
    if (isFull(queue))
        return;
    queue->rear = (queue->rear + 1) % queue->capacity;
    queue->array[queue->rear] = item;
    queue->size = queue->size + 1;
    printf("%d enqueued to queue\n", item);
}

// 出队操作
int dequeue(Queue* queue) {
    if (isEmpty(queue))
        return -1;
    int item = queue->array[queue->front];
    queue->front = (queue->front + 1) % queue->capacity;
    queue->size = queue->size - 1;
    return item;
}

// 获取队首元素
int front(Queue* queue) {
    if (isEmpty(queue))
        return -1;
    return queue->array[queue->front];
}

// 获取队尾元素
int rear(Queue* queue) {
    if (isEmpty(queue))
        return -1;
    return queue->array[queue->rear];
}

int main() {
    Queue* queue = createQueue(MAX);

    enqueue(queue, 10);
    enqueue(queue, 20);
    enqueue(queue, 30);
    enqueue(queue, 40);

    printf("%d dequeued from queue\n", dequeue(queue));
    printf("Front item is %d\n", front(queue));
    printf("Rear item is %d\n", rear(queue));

    return 0;
}

二叉树

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

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

// 创建新节点
Node* newNode(int data) {
    Node* node = (Node*) malloc(sizeof(Node));
    node->data = data;
    node->left = node->right = NULL;
    return node;
}

// 中序遍历
void inorder(struct Node* node) {
    if (node == NULL)
        return;
    inorder(node->left);
    printf("%d ", node->data);
    inorder(node->right);
}

int main() {
    Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);

    printf("Inorder traversal of binary tree is: ");
    inorder(root);

    return 0;
}

哈希表

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

#define SIZE 20

typedef struct Item {
    int key;
    int value;
    struct Item* next;
} Item;

Item* hashArray[SIZE];

// 计算哈希值
int hashCode(int key) {
    return key % SIZE;
}

// 插入键值对
void insert(int key, int value) {
    Item* item = (Item*) malloc(sizeof(Item));
    item->key = key;
    item->value = value;
    item->next = NULL;

    int hashIndex = hashCode(key);
    if (hashArray[hashIndex] == NULL) {
        hashArray[hashIndex] = item;
    } else {
        Item* current = hashArray[hashIndex];
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = item;
    }
}

// 查找键对应的值
int search(int key) {
    int hashIndex = hashCode(key);

    Item* current = hashArray[hashIndex];
    while (current != NULL) {
        if (current->key == key) {
            return current->value;
        }
        current = current->next;
    }
    return -1;
}

// 显示哈希表
void display() {
    for (int i = 0; i < SIZE; i++) {
        Item* current = hashArray[i];
        if (current != NULL) {
            printf("(%d, %d)", current->key, current->value);
            while (current->next != NULL) {
                current = current->next;
                printf(" -> (%d, %d)", current->key, current->value);
            }
            printf("\n");
        }
    }
}

int main() {
    insert(1, 20);
    insert(2, 70);
    insert(42, 80);
    insert(4, 25);
    insert(12, 44);
    insert(14, 32);
    insert(17, 11);
    insert(13, 78);
    insert(37, 97);

    display();

    printf("Value for key 12 is %d\n", search(12));

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值