数据结构(C语言)

链表

链表的基本能操作

c 复制代码
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

//链表的接口
typedef struct node_s{
	int val;
	struct node_s*next;
} Node;
typedef struct linkedlist_s{
	Node* head;
	Node* tail;
	int size;
}LinkedList;

//创建空链表
LinkedList* create_list() {
	return (LinkedList*)calloc(1,sizeof(LinkedList));
}

//头插法
void add_before_head(LinkedList* list,int val) {
	//创建新节点
	Node* newNode = (Node*)malloc(sizeof(Node));
	if(newNode == NULL) {
		printf("Error:malloc failed in add_before_head.\n");
		exit(1);
	}
	//初始化节点
	newNode->val = val;
	newNode->next = list->head;
	list->head = newNode;
	//判断链表为空
	if(list->tail == NULL) {
		list->tail = newNode;
	}
	//更新size
	list->size++;
	
}

//尾插法
void add_behind_tail(L:inkedList* list,int val) {
	//创建新节点
	Node* newNode = (Node*)malloc(sizeof(Node));
	if(newNode == NULL) {
		printf("Error:malloc failed in add_behind_tail.\n");
		exit(1);
	}
	//初始化节点
	newNode->val = val;
	newNode->next = NULL:
	//判断链表是否为空
	if(list->size == 0) {
		list->head = newNode;
		list->tail = newNode;
	}else{
		//连接新节点
		list->tail->next = newNode:
		//更新list->tail
		list->tail = newNode;
	}
	list->size++;
}

//在指定位置插入节点
void add_node(LinkedList* list,int index,int val) {
	if(index < 0 || index >= list->size) {
		printf("Error:Illegal index.\n");
		exit(1);
	}
	//创建新节点
	Node* newNode = (Node*)malloc(sizeof(Node));
	if(newNode == NULL) {
		printf("Error:malloc failed in add_behind_tail.\n");
		exit(1);
	}
	newNode->val = val;
	if(index == 0) {
		//头插法的逻辑
		newNode->next = list->head;
		list->head = newNode;
		lit
	}else{
		// 找到索引未 index-1 的节点
		Node* p = list->head;
		for(int i = 0;i < index-1;i++) {
			p = p->next;
		}
		newNode->next = p->next;
		p->next = newNode;
		
	}
	//更新尾节点
	if(index == list->size) {
		list->tail = newNode;		
	}
	list->size++;
}

//删除第一个与val相等的结点,如果没有这样的节点返回false
bool remove_node(LinkedList* list,int val) {
	Node* prev;
	Node* curr = list->head;
	//寻找前驱节点
	while(curr != NULL && curr->val != val ) {//逻辑与的短路原则,因此先判断curr是否尾NULL
		prev = curr;
		curr = curr->next;
	}
	//没有找到元素
	if(curr == NULL) return false;
	//删除第一个元素
	if(prev == NULL) {
		if(list->size == 1) {
			list->head = list->tail = NULL: 
		}else {
			list->head = list->head->next;
		}
		free(curr);
	}else {
		prev->>next = curr->next;
		if(prev->next == NULL) {
			list->tail = prev;
		}
		free(curr)
;	}
	list->size--;
	return true;
}

//找出第一个与val相等的节点的索引,如果没有这样的节点,返回-1
int indexOf(LinkedList* list,int val) {
	Node* curr = list->head;
	for(int i = 0;i < list->size;i++,curr = curr->next) {
		if(curr->val == val) {
			return i;
		}
	}
	//没有找到
	return -1;
}

void destroy_list(LinkedList* list) {
	//释放节点空间
	Node* curr = list->head;
	while(curr != NULL) {
		//保存后继节点
		Node* next = curr->next;
		free(curr);
		curr = next;
	}
	//释放LinkedList结构体
	free(list);
}

栈是一种操作首先的线性表,只能在栈顶添加和删除元素,LIFO(Last In First Out)

栈的作用

(1)函数调用

(2)实现深度优先遍历

(3)浏览器的前进后退

(4)括号匹配问题

(5)后缀表达式求值问题

栈的基本操作

push(入栈)

pop(出栈)

isEmpty(是否为空)

peek/top(看栈顶元素)

c 复制代码
#inlcude <stdio.h>

typedef struct node_s {
	int val;
	struct node_s* next;
} Node;

void push(Node** ptr_top,int val) {
	//创建新节点
	Node* newNode = (Node*)maclloc(sizeof(Node));
	if(newNode == NULL) {
		printf("Error:malloc failed in push.\n");
		exit(1);
	}
	//初始化结点
	newNode->val = val;
	//头插法
	newNode->next = *ptr_top;
	*ptr_top = newNode;
}

int pop(Node** ptr_top) {
	if(isEmpty(*ptr_top)) {
		printf("Error:stack is Emptgy.\n");
		exit(1);
	}
	//保存原来头节点的值
	Node* old_top = *ptr_top;
	int result = old_top->val;
	//更新头节点
	*ptr_top = (*ptr_top)->next;
	free(old_top);
	return result;
}

bool isEmpty(Node* top) {
	return top == NULL;	
}
//查看栈顶元素
int peek(Node* top) {
	if(isEmpty(top)) {
		printf("Error:st ack is Emptgy.\n");
		exit(1);
	}
	return top->val;
}

队列

也是一种操作受限的线性表,一端插入一端删除;插入一端尾队尾,删除一端为队头,FIFO(First In First Out)

队列作用

(1)广度优先遍历

(2)缓存(消息中间件)

队列的基本操作

enqueue(入队)

dequeue(出队)

peek(查看队头元素)

isEmpty(查看队列是否为空)

c 复制代码
#include <stdlib.h>
#include <stdio.h>
#define N 25
typedef struct {
	int elements[N];
	int front;
	int rear;
} Queue;

//创建空队列
Queue* queue_create() {
	return (Queue*)calloc(1,sizeof(Queue));	
}

void Queue_destroy() {

}
//入队
void enqueue(Queue* q,int val) {
	if(isFull(q)) {
		printf("Error:queue is Full.\n");
		exit(1);
	}
	q->element[q->rear] = val;
	//更新rear的值
	q->rear = (q->rear+1)%N;
}
//出队
int dequeue(Queue* q) {
	if(isEmpty(q)) {
		printf("Error:queue is Full.\n");
		exit(1);
	}
	int removeValue = q->elements[q->front];
	q->front = (q->front + 1) % N;
	return removeValue;
}
int peek(Queue* q) {
	if(isEmpty(q)) {
		printf("Error:queue is Full.\n");
		exit(1);
	}
	return q->elements[q->front];
}
bool isEmpty(Queue* q) {
	return q->front == q->rear;
}
bool idFull(Queue* q) {
	return q->front == (q->rear + 1) % N;
}
相关推荐
小字节,大梦想24 分钟前
【C++】二叉搜索树
数据结构·c++
XKSYA(小巢校长)41 分钟前
NatGo我的世界联机篇
开发语言·php
Cons.W44 分钟前
Codeforces Round 975 (Div. 1) C. Tree Pruning
c语言·开发语言·剪枝
我是哈哈hh1 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
憧憬成为原神糕手1 小时前
c++_ 多态
开发语言·c++
VBA63371 小时前
VBA信息获取与处理第三个专题第三节:工作薄在空闲后自动关闭
开发语言
挥剑决浮云 -1 小时前
Linux 之 安装软件、GCC编译器、Linux 操作系统基础
linux·服务器·c语言·c++·经验分享·笔记
丶Darling.1 小时前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
labuladuo5202 小时前
Codeforces Round 977 (Div. 2) C2 Adjust The Presentation (Hard Version)(思维,set)
数据结构·c++·算法