Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Linked-List Queue — Pharmacy Line Simulator

A small C exercise for practicing queues implemented with a singly linked list (FIFO — First In, First Out).

Scenario

A neighborhood pharmacy serves customers in the order they arrive. Every customer who walks in gets a ticket number and joins the back of the line. The pharmacist always calls the person who has been waiting the longest — the one at the front of the line.

This models a classic queue: new items join at the rear, items leave from the front.

Task

Implement a queue backed by a singly linked list, using two pointers (front and rear) so that both operations run in O(1):

Function Description
enqueue(Queue *q, int ticketNumber) Adds a new ticket at the rear of the line.
int dequeue(Queue *q) Removes and returns the ticket at the front of the line, or -1 if the queue is empty.
printQueue(const Queue *q) Prints the queue from front to rear, e.g. Queue: [1] [2] [3].
freeQueue(Queue *q) Frees every remaining node in the queue.

main() already contains a full simulation that exercises all four functions — it should not be modified. Once the functions above are implemented correctly, running the program produces the expected output below.

Rules

  1. Use a singly linked list (Node -> Node -> ... -> NULL).
  2. Keep both front and rear pointers up to date so you never need to walk the whole list to find the last node.
  3. enqueue always inserts at the rear; dequeue always removes from the front.
  4. Free every node you allocate — no memory leaks.
  5. Handle the empty-queue case everywhere (dequeue on an empty queue must not crash, and must return -1).

Build & Run

gcc -Wall -Wextra -std=c11 -o queue main.c
./queue

Expected Output

== Pharmacy opens ==
Ticket #1 joins the line.
Ticket #2 joins the line.
Ticket #3 joins the line.
Queue: [1] [2] [3]
Now serving Ticket #1. Come to the counter!
Ticket #4 joins the line.
Queue: [2] [3] [4]
Now serving Ticket #2. Come to the counter!
Now serving Ticket #3. Come to the counter!
Now serving Ticket #4. Come to the counter!
Error: queue is empty, cannot dequeue.
Queue is empty, nobody is waiting.
Error: queue is empty, cannot dequeue.
Trying to serve from an empty queue... nothing happens.
== Pharmacy closes ==

Checking for memory leaks (optional)

valgrind ./queue

About

C exercise: FIFO queue via singly linked list, pharmacy ticket simulator

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages