链式栈、队列

news2024/9/20 8:06:36

1、链式栈:

声明:

#ifndef _STACK_H
#define _STACK_H
#include<stdlib.h>

typedef int DataType;

typedef struct snode    //节点
{
    DataType data;
    struct snode *pnext;
}SNode_t;

typedef struct stack    //链表 
{
    SNode_t *ptop;
    int clen;
}Stack_t;

extern Stack_t *create_stack();
extern int push_stack(Stack_t *stack,DataType data);
extern int pop_stack(Stack_t *stack , DataType data);
extern int get_stack_top(Stack_t *stack,DataType *data);
extern void clear_stack(Stack_t *stack);
extern void destroy_stack(Stack_t *stack);
extern int is_empty_stack(Stack_t *stack);
extern void print_stack(Stack_t *stack);



#endif

链表:

Stack_t *create_stack()
{
    Stack_t *stack = malloc(sizeof(Stack_t));
    if(stack == NULL)
    {
        perror("create fail\n");
        return NULL;
    }
    stack->ptop = NULL;
    stack->clen =  0;
    return stack;
}

int push_stack(Stack_t *stack,DataType data)
{
    SNode_t *snode = malloc(sizeof(SNode_t));
    if(NULL == snode)
    {
        perror("fail malloc\n");
        return 0;
    }
    
    snode->data = data;
    snode->pnext = NULL;
    
    snode->pnext = stack->ptop;
    stack->ptop = snode;
    stack->clen++;
    return 1;
}

int get_stack_top(Stack_t *stack,DataType *data)
{
   if(stack->ptop == NULL) 
   {
    return 0;
   }
   *data = stack->ptop->data;
   printf("%d\n",*data);
   return 1;
}

int is_empty_stack(Stack_t *stack)
{
   return stack->ptop == NULL; 
}
int pop_stack(Stack_t *stack,DataType *data)
{
    if(is_empty_stack(stack))
    {
        return 0;
    }
    SNode_t *snode = stack->ptop;
    *data = snode->data;
    if(snode->pnext != NULL)
    {
    stack->ptop = snode->pnext;
    stack->clen--;
    }
    else
    {
        stack->ptop = NULL;
    }
    free(snode);
    return 1;
}

void destroy_stack(Stack_t *stack)
{
    DataType data;
    while(!(is_empty_stack(stack)))
    {
        pop_stack(stack,&data);
    }
    free(stack);
    return;
}

void clear_stack(Stack_t *stack)
{
    if(is_empty_stack(stack))
    {
        return;
    }
    SNode_t *snode = stack->ptop;
    while(stack->ptop != NULL)
    {
        snode = stack->ptop;
        stack->ptop = snode->pnext;
        free(snode);
    }
    stack->clen = 0;
}

void print_stack(Stack_t *stack)
{
    if(is_empty_stack(stack))
    {
        printf("stack empty");
        return;
    }
    SNode_t *snode = stack->ptop;
    while(snode != NULL)
    {
        printf("%d\n",snode->data);
        snode = snode->pnext;
    }
}

主函数调用:

#include<stdio.h>
#include"stack.h"

int main()
{
    DataType data;
    Stack_t *stack = create_stack();
    
    push_stack(stack,1);
    push_stack(stack,2);
    push_stack(stack,3);
    push_stack(stack,4);
    print_stack(stack);
    pop_stack(stack,&data);
   
    print_stack(stack);
    get_stack_top(stack,&data);
    //clear_stack(stack);
    //print_stack(stack);
    printf("----------\n");
    destroy_stack(stack);
    return 0;
}

2、队列

声明:

#ifndef _QUEUE_H
#define _QUEUE_H
#include <pthread.h>
#include<stdlib.h>

typedef int QDataType;

typedef struct qnode           //节点
{
    QDataType data;
    struct qnode *pnext;
}Qnode_t;

typedef struct queue   //队列
{
    Qnode_t *pfront;
    Qnode_t *prear;
    int clen;
    pthread_mutex_t mutex;
}Queue_t;

Queue_t *create_queue();
int is_empty_queue(Queue_t*queue);
int push_queue(Queue_t* queue,QDataType data);
int pop_queue(Queue_t *queue,QDataType *data);
int get_queue_front(Queue_t *queue,QDataType *data);
void clear_queue(Queue_t *queue);
void destroy_queuey(Queue_t *queue);
extern void print_queue(Queue_t *queue);

#endif

队列:

#include<stdio.h>
#include"queue.h"

Queue_t *create_queue()
{
    Queue_t *queue = malloc(sizeof(Queue_t));
    if(queue == NULL)
    {
        perror("fail create_queue ");
        return NULL;
    }
    queue->pfront = NULL;
    queue->prear = NULL;
    queue->clen = 0;
    return queue;
}

int is_empty_queue(Queue_t *queue)
{
    return queue->pfront == NULL;
}

int push_queue(Queue_t *queue,QDataType data)
{
    Qnode_t *qnode = malloc(sizeof(Qnode_t));
	if (NULL == qnode)
	{
		perror("fail malloc");
		return -1;
	}
	qnode->data = data;
    qnode->pnext = NULL;
    if(is_empty_queue(queue))
    {
        queue->pfront = qnode;
        queue->prear = qnode;
    }
    else
    {
        queue->prear->pnext = qnode;
        queue->prear = qnode;
    }
    queue->clen++;
    return 0;
}
void print_queue(Queue_t *queue)
{
    Qnode_t *qnode = queue->pfront;
    while(qnode != NULL)
    {
        printf("%d",qnode->data);
        qnode = qnode->pnext;
    }
    printf("\n");
}
int pop_queue(Queue_t *queue,QDataType *data)
{
    if(is_empty_queue(queue))
    return 0;
    Qnode_t *qnode = queue->pfront;
    queue->pfront = qnode->pnext;
    if(data != NULL)
    {
        *data = qnode->data;
    }
    free(qnode);
    if(NULL == queue->pfront)
    {
        queue->prear = NULL;
    }
    queue->clen--;
    return 1;
}

int get_queue_front(Queue_t *queue,QDataType *data)
{
    if(is_empty_queue(queue))
        return 0;
    if(data == NULL)
        return 0;
    *data = queue->pfront->data;
    
    printf("*data = %d\n", *data);

    return 1;
}

void clear_queue(Queue_t *queue)
{
    while(!is_empty_queue(queue))
    {
        pop_queue(queue,NULL);
    }
}

void destroy_queuey(Queue_t *queue)
{
    clear_queue(queue);
    free(queue);
}

主函数调用

#include<stdio.h>
#include"queue.h"

int main(void)
{
    QDataType data;
    int ret;
    Queue_t *queue = create_queue();
    if(queue == NULL)
    {
        perror("fail create");
        return 0;
    }
    push_queue(queue,1);
    push_queue(queue,2);
    push_queue(queue,3);
    push_queue(queue,4);
    
    print_queue(queue); // 1234 
    pop_queue(queue,&data); //234
    pop_queue(queue,&data);  //34
    print_queue(queue);
    ret = get_queue_front(queue,&data);
    if(ret != 0)
    {
        printf("get head = %d\n",ret);
    }
    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2112067.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Patlibc———更快捷的更换libc

起初是为了简化做pwn题目时&#xff0c;来回更换libc的麻烦&#xff0c;为了简化命令&#xff0c;弄了一个小脚本&#xff0c;可以加入到/usr/local/bin中&#xff0c;当作一个快捷指令&#x1f522; 这个写在了tools库&#xff08;git clone https://github.com/CH13hh/tools…

C++利用jsoncpp库实现写入和读取json文件(含中文处理)

C利用jsoncpp库实现写入和读取json文件 1 jsoncpp常用类1.1 Json::Value1.2 Json::Reader1.3 Json::Writer 2 json文件3 写json文件3.1 linux存储结果3.2 windows存储结果 3 读json文件4 读json字符串参考文章 在C中使用跨平台的开源库JsonCpp&#xff0c;实现json的序列化和反…

【有啥问啥】大模型应用中的哈希链推理任务

大模型应用中的哈希链推理任务 随着人工智能技术的快速发展&#xff0c;尤其是大模型&#xff08;如GPT、BERT、Vision Transformer等&#xff09;的广泛应用&#xff0c;确保数据处理和模型推理的透明性与安全性变得愈发重要。哈希链推理任务作为一种技术手段&#xff0c;能够…

会员营销如何利用JSON发送短信

在当今这个数字化时代&#xff0c;企业间的竞争日益激烈&#xff0c;如何高效地触达并维护用户群体&#xff0c;提升用户粘性和忠诚度&#xff0c;成为了每个企业都必须面对的重要课题。在众多营销手段中&#xff0c;会员营销因其精准性和个性化而备受青睐。而在会员营销的策略…

人工智能导论(上)

一、人工智能概述 人工智能这个基本概念的起源&#xff08;人工智能作为计算机科学的一个分支&#xff09; 很多应用研究领域都在人工智能的范畴里&#xff0c;比如机器人、语言识别、图像识别、自然语言处理和专家系统等等。更加通俗的说&#xff0c;人工智能是要让一部机器能…

传输层协议UDP

本篇将主要介绍 UDP 协议&#xff0c;介绍了有关 UDP 协议的报头、协议特点、UDP 协议在操作系统中的缓冲区、UDP 协议使用的注意事项&#xff0c;以及有关 UDP 的 Socket 编程程序&#xff0c;同时重点介绍了操作系统对于 UDP 协议报文的管理。 接着介绍了有关端口号的映射。 …

s3c2440---中断控制器

一、概述 S3C2440A 中的中断控制器接受来自 60 个中断源的请求。提供这些中断源的是内部外设&#xff0c;如 DMA 控制器、 UART、IIC 等等。 在这些中断源中&#xff0c;UARTn、AC97 和 EINTn 中断对于中断控制器而言是“或”关系。 当从内部外设和外部中断请求引脚收到多个中…

哲学概述2(马克)

三、哲学的基本问题 思维是主观的&#xff08;对应意识&#xff09; 存在是客观的&#xff0c;不以人的意志为转移&#xff08;对应物质&#xff09; 恩格斯说&#xff1a;“全部哲学&#xff0c;特别是近代哲学的重大的基本问题&#xff0c;是思维和存在的关系问题” 哲学的基…

HTML生日蛋糕

目录 写在前面 完整代码 代码分析 系列文章 写在最后 写在前面 HTML实现的生日蛋糕来喽&#xff0c;小编亲测&#xff0c;发给好友可以直接打开哦。在代码的第183行可以写下对朋友的祝福&#xff0c;快拿去送给你的好朋友吧&#xff01; 完整代码 <!DOCTYPE html>…

Python中的位运算-从入门到精通

你是否曾经好奇过计算机是如何在底层处理数据的?或者,你是否想知道为什么有些程序员总是津津乐道于位运算的强大?如果是,那么你来对地方了!今天,我们将深入探讨Python中的位运算,揭示它们的神奇之处,以及如何利用它们来优化你的代码。 目录 位运算:计算机的秘密语言为什么位…

JavaScript Web API入门day6

目录 1.正则表达式 1.1 什么是正则表达式 1.2 语法 1.3 元字符 1.3.1 边界符 1.3.2 量词 1.3.3 字符类 1.4 修饰符 1.5 案例 2.综合案例 2.1 小兔鲜页面注册 2.2 小兔鲜登录页面 2.3 小兔鲜首页页面 1.正则表达式 1.1 什么是正则表达式 正则表达式&#xff08;Re…

Chapter 12 Vue CLI脚手架组件化开发

欢迎大家订阅【Vue2Vue3】入门到实践 专栏&#xff0c;开启你的 Vue 学习之旅&#xff01; 文章目录 前言一、项目目录结构二、组件化开发1. 组件化2. Vue 组件的基本结构3. 依赖包less & less-loader 前言 组件化开发是Vue.js的核心理念之一&#xff0c;Vue CLI为开发者提…

会声会影哪个版本最新

会声会影2023永久免费版能通过多种不同的特效的添加和项目的编辑和处理&#xff0c;能迅速的帮助用户能实现多种不同格式下的结果的提升&#xff0c;让用户能直接的完成相应的帮助和完善提升。 会声会影2023永久免费版简介&#xff1a; 会声会影2023永久免费版是一款简单的视频…

多模态在京东内容算法上的应用

多模态在京东内容算法上的应用 作者&#xff1a;京东零售技术 2024-09-04 北京 本文字数&#xff1a;5226 字 阅读完需&#xff1a;约 17 分钟 本文作者唐烨参与 DataFunsummit2024&#xff1a;推荐系统架构峰会&#xff0c;在专题【多模态推荐论坛】中分享了多模态算法在京…

如何在Word中插入复选框

如何在Word中插入复选框&#xff1a;详细教程与技巧 在Word中插入复选框是一项非常实用的技巧&#xff0c;尤其是在制作问卷调查、待办事项清单、交互式表单或文档中需要用户进行选择时&#xff0c;复选框不仅能提高文档的功能性&#xff0c;还能显得更加专业。本文将详细讲解…

ICLR2024: 大视觉语言模型中对象幻觉的分析和缓解

https://arxiv.org/pdf/2310.00754 https://github.com/YiyangZhou/LURE 背景 对象幻觉&#xff1a;生成包含图像中实际不存在的对象的描述 早期的工作试图通过跨不同模式执行细粒度对齐&#xff08;Biten et al.&#xff0c;2022&#xff09;或通过数据增强减少对象共现模…

各类AI工具编程能力测试对比

各类AI工具编程能力对比 现在各类AI工具火爆&#xff0c;擅长各类问题解决&#xff0c;闲来无事&#xff0c;验证下各类AI工具的编程能力如何。问题&#xff1a;c 实现杨辉三角&#xff0c;并main函数测试 kimi 对话窗口输入问题&#xff0c;得到了c的完整程序&#xff1a; …

JS面试真题 part2

JS面试真题 part2 6、typeof 与 instanceof 区别7、JavaScript原型&#xff0c;原型链&#xff1f;有什么特点8、说说你对作用域链的理解9、谈谈this对象的理解10、说说new操作符具体干了什么 6、typeof 与 instanceof 区别 自己回答&#xff1a; typeof&#xff1a;用来判断数…

SLM561A​​系列 60V 10mA到50mA线性恒流LED驱动芯片 为智能家居照明注入新活力

SLM561A系列选型参考&#xff1a; SLM561A10ae-7G SOD123 SLM561A15ae-7G SOD123 SLM561A20ae-7G SOD123 SLM561A25ae-7G SOD123 SLM561A30ae-7G SOD123 SLM561A35ae-7G SOD123 SLM561A40ae-7G SOD123 SLM561A45ae-7G SOD123 SLM561A50ae-7G SOD123 …

数字证书与公钥基础设施

关注这个证书的其他相关笔记&#xff1a;NISP 一级 —— 考证笔记合集-CSDN博客 0x01&#xff1a;数字证书 数字证书是由第三方可信机构&#xff08;一般是证书服务器&#xff09;颁发的数字证书&#xff0c;可以证明身份的可信度。 数字证书具有以下特点以及性质&#xff1a…