链表
链表的基本能操作
#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(看栈顶元素)
#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(查看队列是否为空)
#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;
}