自学嵌入式 day22 -数据结构 栈 队列

一、栈

1.定义:栈是限定仅在表尾进行插入和删除的线性表

2.特点:先进后出

3.栈顶:允许操作的一方;栈底:不允许操作的一方

4基础操作:

(1)创建

LinkStack* CreateLinkStack()

{

LinkStack* ls = ( LinkStack* )malloc(sizeof(LinkStack));

if(NULL == ls)

{

fprintf(stderr,"CreateLinkStack malloc\n");

return NULL;

}

ls->top =NULL;

ls->clen = 0 ;

return ls;

}

(2)入栈

int PushLinkStack(LinkStack*ls,DATATYPE* data)

{

LinkStackNode* newnode = malloc(sizeof(LinkStackNode));

if(NULL == newnode)

{

fprintf(stderr,"PushLinkStack malloc\n");

return 1;

}

memcpy(&newnode->data,data,sizeof(DATATYPE));

newnode->next = NULL;

newnode->next = ls->top;

ls->top = newnode;

ls->clen++;

}

(3)出栈

int PopLinkStack(LinkStack*ls)

{

if(IsEmptyLinkStack(ls))

{

return 1;

}

LinkStackNode* tmp = ls->top;

ls->top = ls->top->next;

free(tmp);

ls->clen--;

return 0;

}

int IsEmptyLinkStack(LinkStack*ls)

{

return 0 == ls->clen;

}

(4)取栈顶元素

DATATYPE* GetTopLinkStack(LinkStack*ls)

{

if(IsEmptyLinkStack(ls))

{

return NULL;

}

return &ls->top->data;

}

(5)销毁

int DestroyLinkStack(LinkStack*ls)

{

int len = GetSizeLinkStack(ls);

for(int i = 0 ;i < len ;++i)

{

PopLinkStack(ls);

}

free(ls);

return 0;

}

6.判断"()""\[\]""{}"是否对齐

#include <stdio.h>

#include <string.h>

#include "linkstack.h"

int readfile(char *filename, char *filecontext)//文本读取函数

{

FILE *fp = fopen(filename, "r");

if (NULL == fp)

{

return 1;

}

fread(filecontext, 1024, 1, fp);//从文件读取1024字节到缓冲区,返回实际读取块数

fclose(fp);

return 0;

}

int main(int argc, char **argv)

{

char buf1024 = {0};//初始化1024字节缓冲区

int ret = readfile("/home/linux/2.c", buf);//读取文件内容到buf

if (1 == ret)//判断读取是否成功

{

return 1;

}

LinkStack *ls = CreateLinkStack();//创建链表来存储符号

char *tmp = buf;//定义一个指针,指向缓冲区起始位置

DATATYPE data = {0};//定义一个结构体存储符号和其行列数

int row = 1;//初始化行号

int col = 1;//初始化列号

DATATYPE *top;//存储栈顶元素

while (*tmp) // 遍历缓冲区

{

memset(&data, 0, sizeof(DATATYPE));//初始化结构体数据

switch (*tmp)//判断是否是"({ ["符号

{

case '(':

case '[':

case '{':

data.c = *tmp;//存储符号

data.col = col;//存储列号

data.row = row;//存储行号

PushLinkStack(ls, &data);//入栈

break;

case ')'://判断符号是否是")"并且与栈顶元素比较,看是否匹配

top = GetTopLinkStack(ls);//获取栈顶元素

if ('(' == top->c && NULL != top)

{

PopLinkStack(ls);

}

else

{

if (NULL == top)

{

printf("sym:%c row:%d col%d\n", ')', row, col);

}

else

{

printf( "top sym:%c row:%d col%d , or file sym:%c row:%d col%d\n",top->c, top->row, top->col, *tmp, row, col);

}

return 1;

}

break;

case ']'://判断符号是否是"]"并且与栈顶元素比较,看是否匹配

top = GetTopLinkStack(ls);

if ('[' == top->c && NULL != top)

{

PopLinkStack(ls);

}

else

{

if (NULL == top)

{

printf("sym:%c row:%d col%d\n", ')', row, col);

}

else

{

printf(

"top sym:%c row:%d col%d , or file sym:%c row:%d col%d\n",top->c, top->row, top->col, *tmp, row, col);

}

return 1;

}

break;

case '}'://判断符号是否是"}"并且与栈顶元素比较,看是否匹配

top = GetTopLinkStack(ls);

if ('{' == top->c && NULL != top)

{

PopLinkStack(ls);

}

else

{

if (NULL == top)

{

printf("sym:%c row:%d col%d\n", ')', row, col);

}

else

{

printf(

"top sym:%c row:%d col%d , or file sym:%c row:%d col%d\n",top->c, top->row, top->col, *tmp, row, col);

}

return 1;

}

break;

}

col++;//列数递增

if ('\n' == *tmp)//行数递增

{

col = 1;

row++;

}

tmp++;//移动到下一个字符

}

if ('\0' == *tmp && IsEmptyLinkStack(ls))//判断是否成功遍历缓冲区

{

printf("ok");

}

else

{

top = GetTopLinkStack(ls);

printf("top sym:%c row:%d col%d\n", top->c, top->row, top->col);

}

DestroyLinkStack(ls);

return 0;

}

return 0;

}

二、队列

1.定义:只允许在一端进行插入,而在另一端进行删除操作的线性表

2.特点:先进先出

3.对尾:只允许插入的一端;对头:允许删除的一端

4.基础操作(顺序链表):

(1)创建

SeqQueue* CreateSeqQue(int len)

{

SeqQueue *sq = (SeqQueue *)malloc(sizeof(SeqQueue));

if(NULL == sq)

{

fprintf(stderr,"CreateSeqQue malloc\n");

return NULL;

}

sq -> array = (DATATYPE *)malloc(sizeof(DATATYPE) * len);

if(NULL == sq -> array)

{

fprintf(stderr,"DATATYPE malloc");

return NULL;

}

sq -> head = 0;

sq -> tail = 0;

sq -> tlen = len;

return sq;

}

(2)插入

int IsFullSeqQue(SeqQueue*sq)

{

return (sq->tail +1 )%sq->tlen == sq->head;

}

int EnterSeqQue(SeqQueue*sq,DATATYPE*data)

{

if(IsFullSeqQue(sq))

{

fprintf(stderr,"EnterSeqQue,SeqQueue full\n");

return 1;

}

memcpy(&sq->arraysq-\>tail,data,sizeof(DATATYPE));

sq->tail = (sq->tail+1)%sq->tlen;

return 0;

}

(3)删除

int IsEmptySeqQue(SeqQueue*sq)

{

return sq->head == sq->tail;

}

int QuitSeqQue(SeqQueue*sq)

{

if(IsEmptySeqQue(sq))

{

fprintf(stderr,"QuitSeqQue SeqQueue empty\n");

return 1;

}

sq->head = (sq->head+1)%sq->tlen;

return 0;

}

相关推荐
灵枢时代7 小时前
小程序在生鲜配送行业,如何设计夜间预约和次日达的订单处理流程?
数据结构·人工智能·小程序
一条大祥脚7 小时前
ABC468 扫描线|贡献法|二阶差分|线段树优化DP
数据结构·算法
mifengxing1 天前
LeetCode 189 轮转数组|3种解法拆解,从暴力到O(1)原地最优解
数据结构·算法·leetcode
MIngYaaa5201 天前
2026暑期牛客多校3 2026-7-24
数据结构·算法·000
2501_937860941 天前
Java 集合底层深度剖析:Map 与 Set、二叉搜索树、哈希表全解
java·数据结构·散列表
Hommy881 天前
【剪映小助手】字符串列表转对象接口(Str List To Obds)
数据结构·list·剪映小助手·视频剪辑自动化·剪映api·开源剪映小助手
noipp1 天前
推荐题目:洛谷 P3726 [AHOI2017/HNOI2017] 抛硬币
c语言·数据结构·c++·算法·编程·洛谷·luogu
wabs6661 天前
关于链表【力扣19.删除链表的倒数第N个节点的思考】
数据结构·leetcode·链表
Rabitebla1 天前
C++ STL 之 set 详解:从底层红黑树到实际应用
开发语言·数据结构·c++·算法·leetcode
520拼好饭被践踏1 天前
JAVA+Agent学习day26
java·开发语言·数据结构·学习·agent