链式栈、队列

2024-09-07 03:12
文章标签 队列 链式

本文主要是介绍链式栈、队列,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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); //234pop_queue(queue,&data);  //34print_queue(queue);ret = get_queue_front(queue,&data);if(ret != 0){printf("get head = %d\n",ret);}return 0;
}

这篇关于链式栈、队列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


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

相关文章

C++ RabbitMq消息队列组件详解

《C++RabbitMq消息队列组件详解》:本文主要介绍C++RabbitMq消息队列组件的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. RabbitMq介绍2. 安装RabbitMQ3. 安装 RabbitMQ 的 C++客户端库4. A

golang实现延迟队列(delay queue)的两种实现

《golang实现延迟队列(delayqueue)的两种实现》本文主要介绍了golang实现延迟队列(delayqueue)的两种实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录1 延迟队列:邮件提醒、订单自动取消2 实现2.1 simplChina编程e简单版:go自带的time

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

Redis消息队列实现异步秒杀功能

《Redis消息队列实现异步秒杀功能》在高并发场景下,为了提高秒杀业务的性能,可将部分工作交给Redis处理,并通过异步方式执行,Redis提供了多种数据结构来实现消息队列,总结三种,本文详细介绍Re... 目录1 Redis消息队列1.1 List 结构1.2 Pub/Sub 模式1.3 Stream 结

SpringKafka错误处理(重试机制与死信队列)

《SpringKafka错误处理(重试机制与死信队列)》SpringKafka提供了全面的错误处理机制,通过灵活的重试策略和死信队列处理,下面就来介绍一下,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、Spring Kafka错误处理基础二、配置重试机制三、死信队列实现四、特定异常的处理策略五

Spring Boot整合消息队列RabbitMQ的实现示例

《SpringBoot整合消息队列RabbitMQ的实现示例》本文主要介绍了SpringBoot整合消息队列RabbitMQ的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录RabbitMQ 简介与安装1. RabbitMQ 简介2. RabbitMQ 安装Spring

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

解读Redis秒杀优化方案(阻塞队列+基于Stream流的消息队列)

《解读Redis秒杀优化方案(阻塞队列+基于Stream流的消息队列)》该文章介绍了使用Redis的阻塞队列和Stream流的消息队列来优化秒杀系统的方案,通过将秒杀流程拆分为两条流水线,使用Redi... 目录Redis秒杀优化方案(阻塞队列+Stream流的消息队列)什么是消息队列?消费者组的工作方式每

Redis延迟队列的实现示例

《Redis延迟队列的实现示例》Redis延迟队列是一种使用Redis实现的消息队列,本文主要介绍了Redis延迟队列的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录一、什么是 Redis 延迟队列二、实现原理三、Java 代码示例四、注意事项五、使用 Redi

hdu1180(广搜+优先队列)

此题要求最少到达目标点T的最短时间,所以我选择了广度优先搜索,并且要用到优先队列。 另外此题注意点较多,比如说可以在某个点停留,我wa了好多两次,就是因为忽略了这一点,然后参考了大神的思想,然后经过反复修改才AC的 这是我的代码 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<