Professional Documents
Culture Documents
FIFO, FILO, LIFO and LILO Integer Stacks in C
FIFO, FILO, LIFO and LILO Integer Stacks in C
FIFO, FILO, LIFO and LILO Integer Stacks in C
One of my current side projects includes a stack-based interpreter written in C for a programming
language I'm designing, SSBL. Since the entire language is stack-based, I needed a way to create
and store stacks. Currently there are three separate stack types:
ssbl_stack.h
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
typedef enum
{
FIFO,
FILO,
LIFO,
LILO
} SSBL_StackIO;
typedef struct
{
SSBL_StackIO m_IO;
unsigned short m_Count;
unsigned char m_Elements[STACK_SIZE];
} SSBL_StackUint8;
typedef struct
{
SSBL_StackIO m_IO;
unsigned short m_Count;
unsigned short m_Elements[STACK_SIZE];
} SSBL_StackUint16;
typedef struct
{
SSBL_StackIO m_IO;
unsigned short m_Count;
unsigned long m_Elements[STACK_SIZE];
} SSBL_StackUint32;
SSBL_StackUint8* SSBL_StackUint8Create(SSBL_StackIO io);
void SSBL_StackUint8Push(SSBL_StackUint8* stack, unsigned char element);
unsigned char SSBL_StackUint8Pop(SSBL_StackUint8* stack);
unsigned char SSBL_StackUint8Get(SSBL_StackUint8* stack, unsigned short index);
return 0;
}
return 0;
}
return 0;
}
return 0;
}
return 0;
}
unsigned long SSBL_StackUint32Get(SSBL_StackUint32* stack, unsigned short index)
{
SSBL_StackUint32 tempStack = *stack;
if(index >= 0 && index < tempStack.m_Count)
{
return tempStack.m_Elements[index];
}
return 0;
}
test.c
#include <stdio.h>
#include <conio.h>
#include "util/ssbl_stack.h"
return 0;
}
What can be improved? Can this be genericized? Are there any bad practices? Any critical
issues?
c stack
Ethan Bierlein
15.6k33 gold badges4949 silver badges138138 bronze badges
3
I'm curious, why are you calling a FIFO structure a stack, shouldn't it be a queue? – pacmaninbw May
22 '17 at 15:44
Right now your code is broken, specifically the SSBL_StackUint32 struct is broken because it can't
handle unsigned ints, only unsigned shorts. You also might want to look at calloc() versus malloc(). –
pacmaninbw May 22 '17 at 15:52
@pacmaninbw Yep. I actually noticed and fixed that last night, right after I posted the question. Just
forgot to upload it. Should be fixed now. – Ethan Bierlein May 22 '17 at 15:57
4
Is there a difference between FIFO and LILO (or FILO and LIFO)? – vnp May 22 '17 at 16:40
add a comment
2 Answers
activeoldest votes
6
A few observations to help out:
Bug in Pop
In a LIFO structure the value to be returned is tempStack.m_Elements[tempStack.m_Count -
1] not tempStack.m_Elements[0]; .
LIFO structures are definitely different from FIFO structures.
Is there really a difference between these two, as far as I can tell the both equate to FIFO?
The same question applys to FILO and LIFO, in each case it evaluates to First In Last Out?
Typical computer science terminology refers to stacks as LIFO and queues as FIFO, the two
alternate forms could confuse people using the structures as a library.
pacmaninbw
9,93633 gold badges2222 silver badges4747 bronze badges
1
In my implementation, there is a difference between FIFO and LILO, partially due to my (incorrect)
interpretation of their definitions. In a FIFO stack, when ...Push is called, the passed element is
added to the beginning of the stack and when ...Pop is called the element at the beginning of the
stack is removed. Vice versa for LILO. The same concept applies to FILO and LIFO as well. – Ethan
Bierlein May 22 '17 at 18:05
Why do you say, "calloc() by default zeros out the memory it allocates"? I don't think there's anything
default about it; that's the only documented correct behaviour of the function. – Toby Speight May 23 '17
at 9:01
Typo in the memset() call: that should be sizeof *newStack, not sizeof newStack. You might
suggest malloc(sizeof *newStack) too, to match (and that's a standard idiom I'd recommend). –
Toby Speight May 23 '17 at 9:03
add a comment
4
Use integer types with well-defined widths
In the stdint.h header, you are provided with fixed width types. Currently,
your SSBL_StackUint32 structure assumes that long is 32 bits, however, on some
implementations it will be 64 bits.
Simply replace unsigned long with uint32_t. The latter type is guaranteed to always be 32 bits
wide.
typedef struct
{
SSBL_StackIO m_IO;
unsigned short m_Count;
uint32_t m_Elements[STACK_SIZE]; // replaced here
} SSBL_StackUint32; // now the name is accurate
Your other structures work out with their current types, but I'd still suggest modifying them so
that they have the explicit width you expect.
SSBL_StackUint32 tempStack;
stack32_copy(&tempStack, stack);
The remaining copy behaviour can be adapted from this point.