说起队列和栈,链表+动态内存分配的方式,是比较常见的方式,最近项目下需要在dsp上面使用队列和栈两种数据结构,所有就使用链表+动态内存分配的方式实现了一下,但是调试的过程中发现运行的时候总是在动态内存分配的位置出bug,动态内存分配malloc总是分配失败,返回空指针,尝试修改cmd的配置后,也仍然没有解决问题。思来想去还是用数组的方式来实现队列和栈,结果性能出奇的稳定,所以本博客记录一下用数组实现队列和栈的代码。
队列
queue.h
#ifndef USERPROGRAM_QUEUE_QUEUE_H_
#define USERPROGRAM_QUEUE_QUEUE_H_#include <main.h>#define QUEUE_MAX_SIZE 500
/*--------------- 单个Uint16队列 ---------------*/
typedef struct _queue{int size;int front;int rear;Uint16 data[QUEUE_MAX_SIZE];
} queue;void queue_init(queue *q);
int enqueue(queue *q, Uint16 value);
int dequeue(queue *q, Uint16 *value);
int is_queue_empty(queue *q);extern queue scib_rx_queue;
extern queue scib_tx_queue;#endif /* USERPROGRAM_QUEUE_QUEUE_H_ */
queue.c
#include <queue.h>queue scib_rx_queue;
queue scib_tx_queue;void queue_init(queue *q){q->size = 0;q->front = 0;q->rear = -1;
}int enqueue(queue *q, Uint16 value){if(q->size == QUEUE_MAX_SIZE){return 0;}q->rear++;q->data[q->rear] = value;q->size++;return 1;}int dequeue(queue *q, Uint16 *value){if(q->size == 0){return 0;}*value = q->data[q->front];q->front++;q->size--;// 初始化 如果是实时系统里面一直用队列,初始化非常的关键if(q->size==0){q->front = 0;q->rear = -1;}return 1;
}int is_queue_empty(queue *q){return (q->size==0);
}
栈
stack.h
#ifndef USERPROGRAM_STACK_STACK_H_
#define USERPROGRAM_STACK_STACK_H_#include <main.h>#define STACK_MAX_SIZE 500typedef struct _stack
{float data[STACK_MAX_SIZE]; //数组建立顺序栈int top;//栈中元素个数
}stack;extern stack input_compute_stack;
extern stack output_compute_stack;void stack_init(stack* s);
int is_stack_empty(stack* s);
int enstack(stack* s, float value);
int destack(stack* s, float * value);
void clearstack(stack* s);float computeformula(stack *s, float* constant_value, float* ch_value, Uint16* compute_rule, int N, int * ret);#pragma CODE_SECTION(computeformula,"ramfuncs");#endif /* USERPROGRAM_STACK_STACK_H_ */
stack.c
#include <stack.h>stack input_compute_stack;
stack output_compute_stack;void stack_init(stack* s){s->top = 0;
}int is_stack_empty(stack* s){return (s->top==0);
}int enstack(stack* s, float value){if (s->top == STACK_MAX_SIZE){return 0;}else{s->data[s->top] = value;s->top++;return 1;}
}int destack(stack* s, float * value)
{if (s->top == 0){return 0;}else{s->top--;*value = s->data[s->top];return 1;}
}void clearstack(stack* s){s->top = 0;
}float computeformula(stack *s, float* constant_value, float* ch_value, Uint16* compute_rule, int N, int * ret){int i;float result = 0;float operator1 = 0;float operator2 = 0;int stack_pop_ret1 = 1;int stack_pop_ret2 = 1;int stack_push_ret = 1;for(i = 0; i < N; i++){switch (compute_rule[i]){case ADD:stack_pop_ret1 = destack(s, &operator1);stack_pop_ret2 = destack(s, &operator2);result = operator2 + operator1;stack_push_ret = enstack(s, result);if(stack_pop_ret1==0 || stack_pop_ret2==0 || stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case SUBSTRACT:stack_pop_ret1 = destack(s, &operator1);stack_pop_ret2 = destack(s, &operator2);result = operator2 - operator1;stack_push_ret = enstack(s, result);if(stack_pop_ret1==0 || stack_pop_ret2==0 || stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case MULTIPLY:stack_pop_ret1 = destack(s, &operator1);stack_pop_ret2 = destack(s, &operator2);result = operator2 * operator1;stack_push_ret = enstack(s, result);if(stack_pop_ret1==0 || stack_pop_ret2==0 || stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case DIVIDE:stack_pop_ret1 = destack(s, &operator1);stack_pop_ret2 = destack(s, &operator2);if(stack_pop_ret1==0 || stack_pop_ret2==0 || operator1 == 0){*ret = 0;clearstack(s);return 0;}else{result = operator2 / operator1;stack_push_ret = enstack(s, result);if(stack_push_ret==0){*ret = 0;clearstack(s);return 0;}}break;case CH1_P:stack_push_ret = enstack(s, ch_value[0]);if(stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case CH2_P:stack_push_ret = enstack(s, ch_value[1]);if(stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case CH3_P:stack_push_ret = enstack(s, ch_value[2]);if(stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case CH4_P:stack_push_ret = enstack(s, ch_value[3]);if(stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case CH5_P:stack_push_ret = enstack(s, ch_value[4]);if(stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case CH1_N:stack_push_ret = enstack(s, -ch_value[0]);if(stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case CH2_N:stack_push_ret = enstack(s, -ch_value[1]);if(stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case CH3_N:stack_push_ret = enstack(s, -ch_value[2]);if(stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case CH4_N:stack_push_ret = enstack(s, -ch_value[3]);if(stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case CH5_N:stack_push_ret = enstack(s, -ch_value[4]);if(stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case CONSTANT1:stack_push_ret = enstack(s, constant_value[0]);if(stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case CONSTANT2:stack_push_ret = enstack(s, constant_value[1]);if(stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case CONSTANT3:stack_push_ret = enstack(s, constant_value[2]);if(stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case CONSTANT4:stack_push_ret = enstack(s, constant_value[3]);if(stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;case CONSTANT5:stack_push_ret = enstack(s, constant_value[4]);if(stack_push_ret==0){*ret = 0;clearstack(s);return 0;}break;default:break;}}clearstack(s);*ret = 1;return result;}