1. C语言基础
1.1 数据类型
C语言中的数据类型包括基本数据类型(如int、float、char)和构造数据类型(如数组、结构体、联合体)。
代码示例:
#include <stdio.h>
int main() {
int a = 10;
float b = 3.14;
char c = 'A';
return 0;
}
1.2 运算符
C语言中的运算符包括算术运算符、关系运算符、逻辑运算符等。
代码示例:
#include <stdio.h>
int main() {
int a = 5, b = 3;
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a % b = %d\n", a % b);
return 0;
}
2. 控制结构
2.1 顺序结构
顺序结构是C语言中最简单的结构,按照代码的编写顺序执行。
2.2 选择结构
选择结构包括if语句和switch语句。
代码示例:
#include <stdio.h>
int main() {
int a = 10;
if (a > 5) {
printf("a > 5\n");
}
return 0;
}
2.3 循环结构
循环结构包括for循环、while循环和do-while循环。
代码示例:
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 5; i++) {
printf("i = %d\n", i);
}
return 0;
}
3. 函数
3.1 函数定义
函数是C语言中的基本模块,用于实现代码的复用。
代码示例:
#include <stdio.h>
void printHello() {
printf("Hello, World!\n");
}
int main() {
printHello();
return 0;
}
3.2 函数参数
函数参数用于在函数调用时传递数据。
代码示例:
#include <stdio.h>
void printNumber(int num) {
printf("Number: %d\n", num);
}
int main() {
int a = 10;
printNumber(a);
return 0;
}
4. 指针
4.1 指针定义
指针是C语言中的一个重要概念,用于存储变量的地址。
代码示例:
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a;
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", (void *)ptr);
return 0;
}
4.2 指针运算
指针运算包括指针的加减、自增自减等。
代码示例:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("arr[0]: %d\n", *(ptr + 0));
printf("arr[1]: %d\n", *(ptr + 1));
return 0;
}
5. 数组
5.1 一维数组
一维数组是C语言中最基本的数组类型。
代码示例:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("arr[%d]: %d\n", i, arr[i]);
}
return 0;
}
5.2 二维数组
二维数组是C语言中的一种常见数组类型。
代码示例:
#include <stdio.h>
int main() {
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("arr[%d][%d]: %d\n", i, j, arr[i][j]);
}
}
return 0;
}
6. 字符串
6.1 字符串定义
字符串是C语言中的一种特殊数组,用于存储字符序列。
代码示例:
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
printf("str: %s\n", str);
return 0;
}
6.2 字符串操作
C语言中提供了丰富的字符串操作函数,如strlen、strcpy、strcmp等。
代码示例:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
printf("Length of str1: %d\n", strlen(str1));
strcpy(str2, str1);
printf("str2: %s\n", str2);
return 0;
}
7. 结构体
7.1 结构体定义
结构体是C语言中的一种自定义数据类型,用于将多个不同类型的数据组合在一起。
代码示例:
#include <stdio.h>
typedef struct {
int x;
int y;
} Point;
int main() {
Point p = {1, 2};
printf("p.x: %d\n", p.x);
printf("p.y: %d\n", p.y);
return 0;
}
7.2 结构体指针
结构体指针用于访问结构体成员。
代码示例:
#include <stdio.h>
typedef struct {
int x;
int y;
} Point;
int main() {
Point p = {1, 2};
Point *ptr = &p;
printf("ptr->x: %d\n", ptr->x);
printf("ptr->y: %d\n", ptr->y);
return 0;
}
8. 联合体
8.1 联合体定义
联合体是C语言中的一种自定义数据类型,用于存储多个不同类型的数据,但同一时间只能存储其中一个类型的数据。
代码示例:
#include <stdio.h>
typedef union {
int x;
float y;
} UnionType;
int main() {
UnionType ut;
ut.x = 10;
printf("ut.x: %d\n", ut.x);
ut.y = 3.14;
printf("ut.y: %f\n", ut.y);
return 0;
}
9. 位字段
9.1 位字段定义
位字段是C语言中的一种特殊数据类型,用于存储多个布尔值。
代码示例:
#include <stdio.h>
typedef struct {
unsigned int a : 1;
unsigned int b : 1;
unsigned int c : 1;
unsigned int d : 1;
} BitField;
int main() {
BitField bf;
bf.a = 1;
bf.b = 1;
printf("bf.a: %d\n", bf.a);
printf("bf.b: %d\n", bf.b);
return 0;
}
10. 文件操作
10.1 文件打开
文件打开是C语言中文件操作的第一步。
代码示例:
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "r");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
return 0;
}
10.2 文件读取
文件读取是C语言中文件操作的核心。
代码示例:
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "r");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
char ch;
while ((ch = fgetc(fp)) != EOF) {
printf("%c", ch);
}
fclose(fp);
return 0;
}
11. 动态内存分配
11.1 动态内存分配
动态内存分配是C语言中一种重要的内存管理技术。
代码示例:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = (int *)malloc(5 * sizeof(int));
if (arr == NULL) {
printf("Error allocating memory\n");
return 1;
}
for (int i = 0; i < 5; i++) {
arr[i] = i;
}
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
free(arr);
return 0;
}
12. 链表
12.1 链表定义
链表是C语言中一种重要的数据结构,用于动态存储数据。
代码示例:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node *createNode(int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Error allocating memory\n");
return NULL;
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertNode(Node **head, int data) {
Node *newNode = createNode(data);
if (newNode == NULL) {
return;
}
if (*head == NULL) {
*head = newNode;
} else {
Node *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void printList(Node *head) {
Node *current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
Node *head = NULL;
insertNode(&head, 1);
insertNode(&head, 2);
insertNode(&head, 3);
printList(head);
return 0;
}
13. 栈
13.1 栈定义
栈是一种后进先出(LIFO)的数据结构。
代码示例:
#include <stdio.h>
#include <stdlib.h>
typedef struct Stack {
int *array;
int top;
int capacity;
} Stack;
Stack *createStack(int capacity) {
Stack *stack = (Stack *)malloc(sizeof(Stack));
if (stack == NULL) {
printf("Error allocating memory\n");
return NULL;
}
stack->array = (int *)malloc(capacity * sizeof(int));
if (stack->array == NULL) {
printf("Error allocating memory\n");
free(stack);
return NULL;
}
stack->top = -1;
stack->capacity = capacity;
return stack;
}
int isFull(Stack *stack) {
return stack->top == stack->capacity - 1;
}
int isEmpty(Stack *stack) {
return stack->top == -1;
}
void push(Stack *stack, int data) {
if (isFull(stack)) {
printf("Stack is full\n");
return;
}
stack->array[++stack->top] = data;
}
int pop(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1;
}
return stack->array[stack->top--];
}
int main() {
Stack *stack = createStack(5);
push(stack, 1);
push(stack, 2);
push(stack, 3);
printf("Popped element: %d\n", pop(stack));
printf("Popped element: %d\n", pop(stack));
printf("Popped element: %d\n", pop(stack));
return 0;
}
14. 队列
14.1 队列定义
队列是一种先进先出(FIFO)的数据结构。
代码示例:
#include <stdio.h>
#include <stdlib.h>
typedef struct Queue {
int *array;
int front;
int rear;
int capacity;
} Queue;
Queue *createQueue(int capacity) {
Queue *queue = (Queue *)malloc(sizeof(Queue));
if (queue == NULL) {
printf("Error allocating memory\n");
return NULL;
}
queue->array = (int *)malloc(capacity * sizeof(int));
if (queue->array == NULL) {
printf("Error allocating memory\n");
free(queue);
return NULL;
}
queue->front = 0;
queue->rear = -1;
queue->capacity = capacity;
return queue;
}
int isFull(Queue *queue) {
return (queue->rear + 1) % queue->capacity == queue->front;
}
int isEmpty(Queue *queue) {
return queue->front == queue->rear;
}
void enqueue(Queue *queue, int data) {
if (isFull(queue)) {
printf("Queue is full\n");
return;
}
queue->rear = (queue->rear + 1) % queue->capacity;
queue->array[queue->rear] = data;
}
int dequeue(Queue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty\n");
return -1;
}
int data = queue->array[queue->front];
queue->front = (queue->front + 1) % queue->capacity;
return data;
}
int main() {
Queue *queue = createQueue(5);
enqueue(queue, 1);
enqueue(queue, 2);
enqueue(queue, 3);
printf("Dequeued element: %d\n", dequeue(queue));
printf("Dequeued element: %d\n", dequeue(queue));
printf("Dequeued element: %d\n", dequeue(queue));
return 0;
}
15. 树
15.1 树定义
树是一种非线性数据结构,由节点组成,每个节点有零个或多个子节点。
代码示例:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *left;
struct Node *right;
} Node;
Node *createNode(int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Error allocating memory\n");
return NULL;
}
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void insertNode(Node **root, int data) {
if (*root == NULL) {
*root = createNode(data);
} else {
Node *current = *root;
while (current != NULL) {
if (data < current->data) {
if (current->left == NULL) {
current->left = createNode(data);
break;
} else {
current = current->left;
}
} else {
if (current->right == NULL) {
current->right = createNode(data);
break;
} else {
current = current->right;
}
}
}
}
}
void inorderTraversal(Node *root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
int main() {
Node *root = NULL;
insertNode(&root, 5);
insertNode(&root, 3);
insertNode(&root, 7);
insertNode(&root, 2);
insertNode(&root, 4);
insertNode(&root, 6);
insertNode(&root, 8);
inorderTraversal(root);
return 0;
}
16. 图
16.1 图定义
图是一种非线性数据结构,由节点和边组成。
代码示例:
“`c
#include
typedef struct Node {
int data;
struct Node *next;
} Node;
typedef struct Graph {
int numVertices;
Node **adjLists;
int *visited;
} Graph;
Graph *createGraph(int numVertices) {
Graph *graph = (Graph *)malloc(sizeof(Graph));
if (graph == NULL) {
printf("Error allocating memory\n");
return NULL;
}
graph->numVertices = numVertices;
graph->adjLists = (Node **)malloc(numVertices * sizeof(Node *));
if (graph->adjLists == NULL) {
printf("Error allocating memory\n");
free(graph);
return NULL;
}
for (int i = 0; i < numVertices; i++) {
graph->adjLists[i] = NULL;
}
graph->visited = (int *)malloc(numVertices * sizeof(int));
if (graph->visited == NULL) {
printf("Error allocating memory\n");
free(graph->adjLists);
free(graph);
return NULL;
}
for (int i = 0; i < numVertices; i++) {
graph->visited[i] = 0;
}
return graph;
}
void addEdge(Graph *graph, int src, int dest) {
Node *newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
}
void DFS(Graph *graph, int vertex) {
Node *current = graph->adjLists[vertex];
graph->visited[vertex] = 1;
printf("%d ", vertex);
while (current != NULL) {
int adjVertex = current->data;
if (graph->visited[adjVertex] == 0) {
DFS(graph, adjVertex);
}
current = current->next;
}
}
