Intro to Programming Languages

LecLabs 2.5 Heaps and Paging

[ ← Module List ]

This lab will show you an example of how to paging results when the list is filtered by the user.

12 challenges

Challenges

CHALLENGE 01

Lecture 2.5.1.3. the heap

— solves

Lecture video on the heap

CHALLENGE 02

Lecture 2.5.1.4. malloc an int

— solves

Lecture video on malloc an int

CHALLENGE 03

Lecture 2.5.1.5. create a heap based array

— solves

Lecture video on create a heap based array

CHALLENGE 04

Lecture 2.5.1.6. free the heap

— solves

Lecture video on free the heap

CHALLENGE 05

EzLab 2.5.1.6 Heap

— solves

Follow the CODE instructions to define a pointer and allocate memory for an array of 4 integers.

CHALLENGE 06

Lab 2.5.1.6 - Stack on Heap

— solves

Objective

In this lab, we will create a stack data structure.

CTRL-Click Here for Follow Along Video

Steps to Complete

  1. Modify functions in stack.c to complete the CODE: requests
  2. Modify main in main.c to complete the CODE: requests
  3. Modify the Makefile to enable compiling
  4. Compile and test the program
  5. Run /challenge/tester
  6. Get flag
CHALLENGE 07

Lecture 2.5.2.1. realloc

— solves

Lecture video on realloc

CHALLENGE 08

Lecture 2.5.2.2. getline

— solves

Lecture video on getline

CHALLENGE 09

Lecture 2.5.2.3. strdup

— solves

Lecture video on strdup

CHALLENGE 10

Lecture 2.5.2.4. struct malloc

— solves

Lecture video on struct malloc

CHALLENGE 11

EzLab 2.5.2.4 Heap Based Array

— solves

Follow the CODE instructions to create a heap based array of structs.

CHALLENGE 12

Lab 2.5.2.4 - Paging Songs

— solves
SPECIAL NOTE: This lab is a part of a series 2.4.1.5 Songs List -> 2.4.2.6 Make Songs -> 2.4.2.6 filter songs. The lab also uses the same stack as created in 2.5.1.6. You must first complete 2.4.2.6 Filter Songs and should have completed 2.5.1.6 Stack on the Heap before starting on this lab.

At the start of the lab you should have a main.c, songs.h, songs.c, utils.h, utils.c, Makefile, stack.c, and stack.h from the previous labs. If you do not have these files and have completed the prior labs, please contact the course staff.


Objective

In this lab, we will create a new version of the song list program where the results are filtered and paginated.

CTRL-Click Here for Follow Along Video

Requirements

  1. Create paging for full list
    • Declare a starting index variable
    • Wrap the printing for loop in a while that terminates when the user inputs a 'q'
    • Change the for loop that prints songs so that it only display 10 songs
    • Below the for loop, add a user input prompt printf("(n for next, p for prev, f for filter, q for quit): ");
    • Get the user's choice
    • if next then increase starting index by 10 as long as it will not exceed the last song's index
    • if prev then decrease starting index by 10 as long as it will not make it go below 0
  2. Add filtered pagination
    • Once the list is filtered, it breaks the next and previous functionality.
    • moving to next, requires the code to know when a song is being displayed and keeping track of the number of songs displayed
    • To solve this problem, the program will implement a stack that stores the last starting index
      • Pushes the current starting index onto the stack when the user chooses next and then increments it
      • Pops the top most (last pushed) index when the user choose previous and uses it as the starting index
    • The function should not repeat entries (e.g., at the end of the list it shold not keep adding the last starting index)
    • If the stack is empty it should set the starting index to 0

Steps to Complete

  • Implement the requirements
  • Compile and test the program
  • Run /challenge/tester
  • Get flag