数据结构C
发布人:shili8
发布时间:2025-01-20 07:18
阅读次数:0
**数据结构C**
###1. 数组数组是一种线性数据结构,元素都存储在连续的内存空间中。每个元素都有一个下标或索引,可以通过下标来访问。
####1.1 数组定义和初始化
cint arr[5]; // 定义一个长度为5的整型数组arr[0] =10; // 初始化第一个元素
####1.2 数组操作
c#include <stdio.h> void printArray(int arr[], int size) { for (int i =0; i < size; i++) { printf("%d ", arr[i]); } printf(" "); } int main() { int arr[5] = {10,20,30,40,50}; printArray(arr,5); return0; }
###2. 链表链表是一种非线性数据结构,元素存储在不连续的内存空间中,每个元素都有一个指向下一个元素的指针。
####2.1 链表定义和初始化
ctypedef struct Node { int data; struct Node* next; } Node; Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); if (!newNode) { printf("Memory error "); return NULL; } newNode->data = data; newNode->next = NULL; return newNode; }
####2.2 链表操作
c#include <stdio.h> void printList(Node* head) { Node* temp = head; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf(" "); } int main() { Node* head = createNode(10); Node* second = createNode(20); Node* third = createNode(30); head->next = second; second->next = third; printList(head); return0; }
###3. 栈栈是一种线性数据结构,元素都存储在连续的内存空间中。每个元素都有一个下标或索引,可以通过下标来访问。
####3.1 栈定义和初始化
ctypedef struct Stack { int* elements; int top; int capacity; } Stack; Stack* createStack(int capacity) { Stack* stack = (Stack*)malloc(sizeof(Stack)); if (!stack) { printf("Memory error "); return NULL; } stack->elements = (int*)malloc(capacity * sizeof(int)); stack->top = -1; stack->capacity = capacity; return stack; }
####3.2 栈操作
c#include <stdio.h> void push(Stack* stack, int element) { if (stack->top == stack->capacity -1) { printf("Stack is full "); return; } stack->elements[++stack->top] = element; } int pop(Stack* stack) { if (stack->top == -1) { printf("Stack is empty "); return -1; } return stack->elements[stack->top--]; } void printStack(Stack* stack) { for (int i =0; i <= stack->top; i++) { printf("%d ", stack->elements[i]); } printf(" "); }
###4. 队列队列是一种线性数据结构,元素都存储在连续的内存空间中。每个元素都有一个下标或索引,可以通过下标来访问。
####4.1 队列定义和初始化
ctypedef struct Queue { int* elements; int front; int rear; int size; } Queue; Queue* createQueue(int capacity) { Queue* queue = (Queue*)malloc(sizeof(Queue)); if (!queue) { printf("Memory error "); return NULL; } queue->elements = (int*)malloc(capacity * sizeof(int)); queue->front =0; queue->rear = -1; queue->size =0; return queue; }
####4.2 队列操作
c#include <stdio.h> void enqueue(Queue* queue, int element) { if (queue->rear == queue->capacity -1) { printf("Queue is full "); return; } queue->elements[++queue->rear] = element; queue->size++; } int dequeue(Queue* queue) { if (queue->front > queue->rear) { printf("Queue is empty "); return -1; } int temp = queue->elements[queue->front++]; queue->size--; return temp; } void printQueue(Queue* queue) { for (int i =0; i < queue->size; i++) { printf("%d ", queue->elements[i]); } printf(" "); }
###5.树树是一种非线性数据结构,元素存储在不连续的内存空间中,每个元素都有一个指向子元素的指针。
####5.1 树定义和初始化
ctypedef struct Node { int data; struct Node* left; struct Node* right; } Node; Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); if (!newNode) { printf("Memory error "); return NULL; } newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; }
####5.2 树操作
c#include <stdio.h> void printTree(Node* root) { if (root == NULL) { return; } printf("%d ", root->data); printTree(root->left); printTree(root->right); } int main() { Node* root = createNode(10); Node* left = createNode(20); Node* right = createNode(30); root->left = left; root->right = right; printTree(root); return0; }
###6.图图是一种非线性数据结构,元素存储在不连续的内存空间中,每个元素都有一个指向相邻元素的指针。
####6.1 图定义和初始化
ctypedef struct Graph { int numVertices; int** adjMatrix; } Graph; Graph* createGraph(int numVertices) { Graph* graph = (Graph*)malloc(sizeof(Graph)); if (!graph) { printf("Memory error "); return NULL; } graph->numVertices = numVertices; graph->adjMatrix = (int**)malloc(numVertices * sizeof(int*)); for (int i =0; i < numVertices; i++) { graph->adjMatrix[i] = (int*)malloc(numVertices * sizeof(int)); } return graph; }
####6.2 图操作
c#include <stdio.h> void addEdge(Graph* graph, int v1, int v2) { if (v1 >= graph->numVertices || v2 >= graph->numVertices) { printf("Invalid vertex "); return; } graph->adjMatrix[v1][v2] =1; } int main() { Graph* graph = createGraph(5); addEdge(graph,0,1); addEdge(graph,1,2); addEdge(graph,2,3); for (int i =0; i < graph->numVertices; i++) { for (int j =0; j < graph->numVertices; j++) { printf("%d ", graph->adjMatrix[i][j]); } printf(" "); } return0; }
以上是数据结构C的基本内容,包括数组、链表、栈、队列、树和图。每种数据结构都有其特点和应用场景。