欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 链式栈、队列

链式栈、队列

2024/10/25 20:17:22 来源:https://blog.csdn.net/xgshxjhs_/article/details/141970080  浏览:    关键词:链式栈、队列

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;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com