fix pop from FIFOBuff and fix warnings for CLI

This commit is contained in:
2024-04-08 15:08:10 +02:00
parent e6d9bb1da6
commit 24738ebeea
2 changed files with 43 additions and 27 deletions

View File

@@ -1,9 +1,8 @@
#include "FIFOBuffChar.h"
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include "FIFOBuffChar.h"
bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function parameters to match the declaration
{
@@ -126,25 +125,39 @@ bool FIFOBuffChar_pop(FIFOBuffChar_t *fifo)
{
bool ok = true;
if (fifo->FirstEl_p == NULL)
{ // buffer is empty
ok = false;
}
else
{ // there is data in the buffer
// find the second last element
FIFOBuffChar_element_t* secondLastEl; = fifo->FirstEl_p;
while (secondLastEl->nextElement != fifo->LastEl_p)
{
secondLastEl = secondLastEl->nextElement;
}
switch (fifo->size){
case 0: // buffer is empty
ok = false;
break;
// remove last element
free(fifo->LastEl_p);
case 1: // singel item in buffer
// make buffer empty
free(fifo->FirstEl_p);
fifo->FirstEl_p = NULL;
fifo->LastEl_p = NULL;
fifo->size = 0;
ok = true;
break;
// update chain
secondLastEl->nextElement == NULL;
fifo->LastEl_p = secondLastEl;
default: // buffer is at least 2 element big
// find the second last element
FIFOBuffChar_element_t* secondLastEl = fifo->FirstEl_p;
while (
secondLastEl->nextElement != fifo->LastEl_p
&& ((FIFOBuffChar_element_t*)secondLastEl->nextElement)->nextElement != NULL
){
secondLastEl = secondLastEl->nextElement;
}
// remove last element
free(fifo->LastEl_p);
// update chain
secondLastEl->nextElement = NULL;
fifo->LastEl_p = secondLastEl;
fifo->size--;
ok = true;
break;
}
return ok;