LecLabs 2.5 Heaps and Paging


Intro to Programming Languages.

[ ← Module List ]

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


Challenges

Lecture video on the heap

Lecture video on malloc an int

Lecture video on create a heap based array

Lecture video on free the heap

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

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

Lecture video on relloc

Lecture video on getline

Lecture video on strdup

Lecture video on struct alloc

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

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. You must first complete 2.4.2.6 Filter Songs before you can start on this lab.

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 an 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

30-Day Scoreboard:

This scoreboard reflects solves for challenges in this module after the module launched in this dojo.

Rank Hacker Badges Score