1.list
#include <stdio.h>
#include <stdlib.h>
typedef int E;
struct List {
E* array;
int capacity; //表示底层数组的容量
int size; //表中的元素数量
};
typedef struct List* ArrayList;
_Bool initList(ArrayList list) { //初始化
list->array = malloc(sizeof(E) * 10);
if (list->array == NULL) return 0;
list->capacity = 10;
list->size = 0;
return 1;
}
_Bool insertList(ArrayList list , E element, int index) { //插入数组
if(index < 1 || index > list->size + 1) return 0;
if(list->size == list->capacity){
int newCapacity = list->capacity + (list->capacity >>1);
E * newArray = realloc(list->array,newCapacity * sizeof(E));
if(newArray == NULL) return 0;
list->array = newArray;
list->capacity = newCapacity;
}
for (int i = list->size; i > index; i--) {
list->array[i] = list->array[i-1];
}
list->array[index-1] = element;
list->size++;
return 1;
}
_Bool deleteList(ArrayList list,int index) { //删除数组
//list是待操作表,index是要删除位序
if(index < 1 || index > list->size) return 0;
for (int i = index; i < list->size; ++i) {
list->array[i-1] = list->array[i];
}
list->size--;
return 1;
}
int sizeList(ArrayList list) { //返回size
return list->size;
}
E * getList(ArrayList list, int index) { //获取值
if(index < 1 || index >list->size) return NULL;
return &list->array[index - 1];
}
int findList(ArrayList list, E element) { //寻找表中元素位置
for (int i = 0; i < list->size; ++i) {
if(list->array[i] == element) return i + 1;
}
return -1;
}
void printList(ArrayList list) {
for (int i = 0; i <list->size ; i++) {
printf("%d ",list->array[i]);
}
printf("\n");
}
int main() {
struct List list;
if (initList(&list)) {
// 初始化成功后的操作
// insertList(&list,111,1);
// printList(&list);
// insertList(&list,222,2);
// printList(&list);
for (int i = 0; i < 30; ++i) {
insertList(&list,i*10,i+1);
}
printList(&list);
printf("%d\n",list.capacity);
printf("%d\n", *getList(&list,3));
printf("%d\n", findList(&list,10));
}
else {
printf("程序初始化失败,无法启动程序");
}
return 0;
}
2.LinkedList
#include <stdio.h>
#include<stdlib.h>
typedef int E;
struct ListNode {
E element; //保存当前元素
struct ListNode * next; //指向下一个结点的指针
};
typedef struct ListNode * Node;
void initList(Node head) {
head->next = NULL;
}
_Bool insertList(Node head, E element, int index) {
if(index < 1) return 0;
while(--index) {
head = head->next;
if(head == 0) return 0;
}
Node node = malloc(sizeof(struct ListNode));
if(node == NULL) return 0;
node->element = element;
node->next = head->next;
head->next = node;
return 1;
}
_Bool deleteList(Node head, int index) {
if(index < 1) return 0;
while(--index) {
head = head->next;
if(head == NULL) return 0;
}
if(head->next == NULL) return 0;
Node tmp = head->next;
head->next = head->next->next;
return 1;
}
E * getList(Node head , int index) {
if(index - 1) return 0;
do{
head = head->next;
if(head == NULL) return 0;
}while(--index);
return &head->element;
}
int findList(Node head, E element) {
head = head->next;
int i = 1;
while(head) {
if(head->element == element) return i;
head = head->next;
i++;
}
return 0;
}
int sizeList(Node head) {
int i = 0;
while(head->next) { //如果为null就停
head = head->next;
i++;
}
return i;
}
void printList(Node head) {
while(head->next){
head = head->next;
printf("%d ",head->element);
}
printf("\n");
}
int main() {
struct ListNode head;
initList(&head);
for(int i = 1 ; i<=3; i++) {
insertList(&head , i*10, i);
}
//deleteList(&head,1);
printList(&head);
printf("%d\n", *getList(&head,1));
printf("%d\n", findList(&head,10));
printf("%d\n", sizeList(&head));
return 0;
}
3.Double Linked List
#include <stdio.h>
#include <stdlib.h>
typedef int E;
struct ListNode{
E element;
struct ListNode * next; //指向下一结点的指针
struct ListNode * prev; //指向上一结点的指针
};
typedef struct ListNode * Node;
void initList(Node head){ //初始化
head->next = head->prev = NULL;
}
_Bool insertList(Node head , E element , int index) { //插入双向链表
if(index < 1) return 0;
while(--index) {
head = head->next;
if(head == NULL) return 0;
}
Node node =malloc(sizeof(struct ListNode));
node->element = element;
if(node == NULL) return 0;
if(head->next != NULL) {
node->next = head->next;
head->next->prev = node;
} else{
node->next = NULL;
}
head->next = node;
node->prev = head;
return 1;
}
_Bool deleteList(Node head, int index) { //删除
if(index < 1) return 0;
while(--index) {
head = head->next;
if(head == NULL) return 0;
}
if(head->next == NULL) return 0;
Node temp = head->next;
if(head->next->next) {
head->next->next->prev = head;
head->next = head->next->next;
} else {
head->next = NULL;
}
free(temp);
return 1;
}
int main() {
struct ListNode head;
initList(&head);
for(int i = 0; i < 5; i++) {
insertList(&head, i * 10, i);
}
Node node = &head;
do {
node = node->next;
printf("%d-> ", node->element);
} while (node->next != NULL);
printf("\n");
do{
printf("%d-> ",node->element);
node = node->prev;
} while (node->prev != NULL);
return 0;
}
4.Stack
#include <stdio.h>
#include <stdlib.h>
typedef int E;
struct Stack {
E * array;
int capacity;
int top; //这里使用top来表示当前栈顶位置,存的是栈顶元素的下标
};
typedef struct Stack * ArrayStack;
_Bool initStack(ArrayStack stack) {
stack->array = malloc(sizeof(E) * 10 );
if(stack->array == NULL) return 0;
stack->capacity = 10;
stack->top = -1;
return 1;
}
_Bool pushStack(ArrayStack stack, E element) {
//空间不足
if(stack->top + 1 == stack->capacity) {
int newCapacity = stack->capacity + (stack->capacity >> 1);
E * newArray = realloc(stack->array,newCapacity * sizeof(E));
if(newArray == NULL) return 0;
stack->array = newArray;
stack->capacity = newCapacity;
}
stack->array[++stack->top] = element;
return 1;
}
_Bool isEmpty(ArrayStack stack) {
return stack->top == -1;
}
E popStack(ArrayStack stack) {
return stack->array[stack->top--];
}
void printStack(ArrayStack stack) {
printf(" | \n");
for (int i = 0; i < stack->top + 1; i++) {
printf("%d " ,stack->array[i]);
}
printf("\n");
}
int main() {
struct Stack stack;
initStack(&stack);
for(int i = 0; i< 3; i++) {
pushStack(&stack , i * 10);
}
printStack(&stack);
while(!isEmpty(&stack)) {
printf("%d ", popStack(&stack));
}
return 0;
}
5.Linked Stack
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int E;
struct ListNode {
E element;
struct ListNode * next;
};
typedef struct ListNode * Node;
void initStack(Node head) {
head->next = NULL;
}
_Bool pushStack(Node head , E element) {
Node node =(struct ListNode*) malloc(sizeof(struct ListNode));
if(node->next == NULL) return 0;
node->element = element;
node->next = head->next;
head->next = node;
return 1;
}
_Bool isEmpty(Node head) {
return head->next ==NULL;
}
E popStack(Node head) {
Node tmp = head->next;
E e = tmp->element;
head->next = head->next->next;
free(tmp);
return e;
}
void printStack(Node head) {
printf(" | \n");
head = head->next;
while(head) {
printf("%d ",head->element);
head = head->next;
}
printf("\n");
}
int main() {
struct ListNode head;
initStack(&head);
for(int i =0; i< 5; i++) {
pushStack(&head,i*10);
}
printStack(&head);
while(!isEmpty(&head)) {
printf("%d ", popStack(&head));
}
return 0;
}
6.Queue
#include <stdio.h>
#include <stdlib.h>
typedef int E;
struct Queue {
E * array;
int capacity;
int rear, front; //队尾,队首指针
};
typedef struct Queue * ArrayQueue;
_Bool initQueue(ArrayQueue queue) {
queue->array = malloc(sizeof(E) * 10);
if(queue->array == NULL) return 0;
queue->capacity = 10;
queue->rear = 0;
queue->front = 0;
return 1;
}
_Bool offerQueue(ArrayQueue queue , E element) {
int pos = (queue->rear + 1) % queue->capacity;
if(pos == queue->front) return 0;
queue->rear = pos;
queue->array[queue->rear] = element;
return 1;
}
void printQueue(ArrayQueue queue) {
printf("<<< \n");
int i = queue->front;
do{
i = (i + 1) % queue->capacity;
printf("%d " ,queue->array[i]);
}while(i != queue->rear);
printf("\n<<< \n");
}
_Bool isEmpty(ArrayQueue queue) {
return queue->rear == queue->front;
}
E pollQueue(ArrayQueue queue) {
queue->front = (queue->front + 1) % queue->capacity;
return queue->array[queue->front];
}
int main() {
struct Queue queue;
initQueue(&queue);
for(int i = 0; i< 5; i++) {
offerQueue(&queue,i * 10);
}
printQueue(&queue);
while(!isEmpty(&queue)){
printf("%d " , pollQueue(&queue));
}
return 0;
}
7.Linked Queue
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int E;
struct LNode {
E element;
struct LNode* next;
};
typedef struct LNode* Node;
struct Queue {
Node front, rear;
};
typedef struct Queue* LinkedQueue;
bool initQueue(LinkedQueue queue) {
Node head = (struct LNode*)malloc(sizeof(struct LNode));
if (head == NULL) return false;
head->next = NULL; //更改
queue->rear = queue->front = head;
return true;
}
bool offerQueue(LinkedQueue queue, E element) {
Node node = (struct LNode*)malloc(sizeof(struct LNode));
if (node == NULL) return false;
node->element = element;
node->next = NULL; //更改
queue->rear->next = node;
queue->rear = node;
return true;
}
bool isEmpty(LinkedQueue queue) {
return queue->rear == queue->front;
}
E pollQueue(LinkedQueue queue) {
if (isEmpty(queue)) return -1; // 返回一个特殊值表示出错
Node tmp = queue->front->next;
E e = tmp->element;
queue->front->next = queue->front->next->next;
if (queue->rear == tmp) queue->rear = queue->front;
free(tmp);
return e;
}
void printQueue(LinkedQueue queue) {
printf("<<< \n");
Node node = queue->front->next;
while (node) {
printf("%d ", node->element);
node = node->next;
}
printf("\n<<<\n");
}
int main() {
struct Queue queue;
initQueue(&queue);
for (int i = 0; i < 5; i++) {
offerQueue(&queue, i * 10);
}
printQueue(&queue);
while (!isEmpty(&queue)) {
printf("%d ", pollQueue(&queue));
}
return 0;
}
8.
#include <stdio.h>
#include <stdlib.h>
// 二叉树结点的定义
typedef struct TreeNode {
char data;
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;
// 创建一个新的二叉树结点
TreeNode* createNode(char value) {
TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// 构建二叉树
TreeNode* buildTree(char* preorder, int* index) {
char value = preorder[(*index)++];
if (value == '\0' || value == '#') {
return NULL;
}
TreeNode* newNode = createNode(value);
newNode->left = buildTree(preorder, index);
newNode->right = buildTree(preorder, index);
return newNode;
}
// 先序遍历
void preorderTraversal(TreeNode* root) {
if (root == NULL) {
return;
}
printf("%c ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// 中序遍历
void inorderTraversal(TreeNode* root) {
if (root == NULL) {
return;
}
inorderTraversal(root->left);
printf("%c ", root->data);
inorderTraversal(root->right);
}
// 后序遍历
void postorderTraversal(TreeNode* root) {
if (root == NULL) {
return;
}
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%c ", root->data);
}
// 计算树的深度
int treeDepth(TreeNode* root) {
if (root == NULL) {
return 0;
}
int leftDepth = treeDepth(root->left);
int rightDepth = treeDepth(root->right);
return (leftDepth > rightDepth) ? (leftDepth + 1) : (rightDepth + 1);
}
// 计算叶子节点个数
int countLeaves(TreeNode* root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right == NULL) {
return 1;
}
return countLeaves(root->left) + countLeaves(root->right);
}
int main() {
char* preorder = "abc##d##ef###"; // 示例输入的完整先序遍历序列
int index = 0;
TreeNode* root = buildTree(preorder, &index);
int choice;
while (1) {
printf("\nMenu:\n");
printf("1. 先序遍历\n");
printf("2. 中序遍历\n");
printf("3. 后序遍历\n");
printf("4. 计算深度\n");
printf("5. 计算叶子节点个数\n");
printf("6. 退出\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("先序遍历结果: ");
preorderTraversal(root);
printf("\n");
break;
case 2:
printf("中序遍历结果: ");
inorderTraversal(root);
printf("\n");
break;
case 3:
printf("后序遍历结果: ");
postorderTraversal(root);
printf("\n");
break;
case 4:
printf("树的深度: %d\n", treeDepth(root));
break;
case 5:
printf("叶子节点个数: %d\n", countLeaves(root));
break;
case 6:
printf("退出程序.\n");
exit(0);
default:
printf("无效选项,请重新选择.\n");
}
}
return 0;
}