数据结构(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;
}
相关推荐
青出于兰2 分钟前
C语言| 指针变量的定义
c语言·开发语言
玉笥寻珍15 分钟前
筑牢信息安全防线:涉密计算机与互联网隔离的理论实践与风险防控
开发语言·计算机网络·安全·计算机外设·php·安全架构·安全性测试
蓝莓味柯基19 分钟前
Lodash isEqual 方法源码实现分析
开发语言
秋野酱29 分钟前
python项目参考文献
开发语言·python
士别三日&&当刮目相看30 分钟前
数据结构*优先级队列(堆)
java·数据结构
QQ_4376643141 小时前
单向循环链表C语言实现实现(全)
数据结构·windows·链表
ptu小鹏1 小时前
list重点接口及模拟实现
数据结构·c++·list
lsswear1 小时前
php fiber 应用
开发语言·php
(・Д・)ノ1 小时前
python打卡day28
开发语言·python
保利九里1 小时前
java中的方法详解
java·开发语言·python