---
# System prepended metadata

title: Week 4 - Stacks and Queues

---

# Week 4 - Stacks and Queues

## Team

>Members:
>
>- Andreas Andreas 507675
>-
>
> Date: *day* *month* 2023

## Provided code

The `zip` file on Blackboard contains the C code for this week.
The project contains three *targets*: `stacks`, `queues` and `circular-buffer`.

Also, the project uses a static library that provides an array and a linked list.
If you're on Windows, then run your code on WSL.

## Activities

Make sure to have the activities signed off regularly to ensure progress is tracked.

### Activity 1: LIFO vs FIFO

LIFO stands for "Last-In, First-Out" and FIFO stands for "First-In, First-Out."

Lifo refers to a system where the last item is the first item to be removed
Fifo refers to a system where the first item that was added in the list is the first to be removed
Lifo memory buffer stack : A stack is called that way when it oparates the same as a lifo system in a stck the most recently added item is the first to be removed.When data is stored in a stack new data are pushed on top of the stack and when you remove them the data are popped from the stack.This is usefull for  undo and redo applications

### Activity 2: Stacks and LIFO
SYEUQTSAONIE

### Activity 3: Pushes and pops
B
Because as we can see the b exapmle starts with 4 that means that number 0-3 have been poped out  in oposite order.Then 
 that means that number 5 has been poped off the stack and 6 is printed.Thats inposible because inorder for this to happen  5 should have been pushed on top of 6 and that conteredicts the fact that 0-9 where pushed in oder onto the stack
### Activity 4: Communication through a linked list



### Activity 5: FIFO and queues

E
A
S
Y
Q
U
E
S
T
I
O
N

### Activity 6: Arrays, linked lists, and queues

| Operation                          |      Array      |   Linked List  |
| ---------------------------------- | --------------- | -------------- |
| Insert / remove **first** element  |    O(N)     |     O(1)    |
| Insert / remove **last** element   |     O(N)      |      O(1)    |
| Peek first / last element          |     O(N)      |      O(1)  |



### Activity 7: Time complexities, once again

| Operation                     | Time complexity |
| ----------------------------- | --------------- |
| Insert / remove first element |    O(1)      |
| Insert / remove last element  |    O(1 )     |
| Peek first / last element     |    O(1)      |

Record your answer here

### Activity 8: Clock arithmetic

void circular_buffer_append(buffer_t *buffer, float value) {
    size_t po = buffer_get_rear_idx(buffer);
    buffer->data[po] = value;
    buffer->count++;
}

### Activity 9: Writing into a circular buffer

bool buffer_try_write(buffer_t *buffer, char value) {
    if (buffer->count == buffer->size) {
        return false; //full buffer
    }
    size_t po = buffer_get_rear_po(buffer);
    buffer->data[po] = value;
    buffer->count++;
    return true;
}


### Activity 10: Reading from a circular buffer

bool buffer_try_read(buffer_t *buffer, char *character) {
    if (buffer->count == 0) {
        return false; // buffer is empty
    }
    *character = buffer->data[buffer->head];
    buffer->head = (buffer->head + 1) % buffer->size;
    buffer->count--;
    return true;
}
int main() {
    buffer_t buffer;
    buffer_init(&buffer, 10); 
    char c;
    while (buffer_try_read(&buffer, &c)) {
        putchar c; 
    }
    buffer_free(&buffer);
    return 0;
}


### Activity 11: Testing the queue
The program works by copying the input file into the output file, using the buffer.
## Looking back

### What we've learnt

Formulate at least one lesson learned.

### What were the surprises

Fill in...

### What problems we've encountered

Fill in...

### What was or still is unclear

Fill in...

### How did the group perform?

How was the collaboration? What were the reasons for hick-ups? What worked well? What can be improved next time?
