/Practice_LeetCode_C

【On-Going】Practice for C programming language leetcode questions in local environment

Primary LanguageCMIT LicenseMIT

Practice_LeetCode_C

Topics Badges / URLs
License License: MIT
Code Style Google’s C++ style guide (Clang-Format Style Options)
Common Functions CommonFunctionLints CommonFunctionTests
Programming Topics ProgrammingTopicLints ProgrammingTopicTests
CodeQL CodeQL

Workspace Structure

C language local leetcode practice overall folder structure:

Common Functions

To ensure code quality, all shared common code based on different programming topics will be placed in the Common_Functions folder following these workflows:

Workflows Functionality Source Code
CommonFunctionLints Code Style Check commonFuncLint.yml
CommonFunctionTests Unit Test (Google Test) commonFuncTest.yml

Code Structure of Basic Building Unit

To start getting hands on the project, the fundamental code structure of each question is as follows:

.
├── cfg
│   ├── compile_cfg.h                     /* common function configuration file */
│   └── method_cfg.h                      /* solution method configuration file */
├── doc
│   ├── sketch.drawio                     /* drawing sketch file */
│   └── sketch.excalidraw                 /* drawing sketch file */
├── inc
│   └── solution_name.h                   /* solution header file */
├── src
│   ├── solution_name__method_name_01.c   /* solution method 01 source file */
│   ├── solution_name__method_name_02.c   /* solution method 02 source file */
│   ├── ...
│   └── solution_name__method_name_xx.c   /* solution method XX source file */
└── main.c                                /* entry point */

The call hierarchy between files is as follow:

Call Hierarchy

The main.c file is the entry point of the program, which calls the solution method in the solution_name__method_name_xx.c file. Meanwhile, the main.c file also contains the test cases for the selected solution method.

The cfg folder contains the configuration files for the common functions and solution methods.

  • The compile_cfg.h file is the configuration file for common functions. By toggling the #define macro, the corresponding common function can be enabled or disabled during compilation.

  • The method_cfg.h file is the configuration file for solution methods. By toggling the #define macro, the corresponding solution method can be enabled or disabled during compilation.

The doc folder contains the documentation files and drawing sketches for the question.

The inc folder contains the header files for the solution methods.

The src folder contains the source files for the solution methods.

  • The solution_name__method_name_xx.c file is the source file for a specific solution method. By toggling the #define macro in the method_cfg.h file, the corresponding solution method can be enabled or disabled during compilation.

For a complete example, please refer to the following file: F02_Programming_Topics\F01_Data_Structure\01_Array\01_binarySearch\main.c

Flowchart source file: compilation_structure.drawio (hediet.vscode-drawio extension required)

Build & Run Code

Operating Systems

  • Windows
  • Ubuntu

Build Tool Versions

  • Python 3.12.1
  • gcc.exe (Rev7, Built by MSYS2 project) 13.1.0
  • g++.exe (Rev7, Built by MSYS2 project) 13.1.0
  • GNU gdb (GDB) 13.2

VSCode Native - Build & Run Current File

To execute the source code in VSCode, choose the desired main.c file and navigate to the following tab and options in the VSCode interface:

Terminal -> Run Task... -> Build & Run Current File

or simply press

Ctrl + Shift + B

Source code: tasks.json and python build.py.

VSCode Extension - Code Runner

The Code Runner vscode extension provides a convenient way to execute the main.c file directly.

Source code: settings.json.

Build Process

The gcc compilation process is as follows:

  1. Search for the header files in /01_Common_Functions/inc/commonDef/ and /01_Common_Functions/inc/ folders.

  2. Search for the header files in ${fileDirname}/cfg/ and ${fileDirname}/inc/ folder.

  3. Search and compile all the source files in /01_Common_Functions/src/*.c and ${fileDirname}/src/*.c with ${fileDirname}/main.c as the entry point.

  4. Generate the executable file in ${fileDirname}/ folder with name main.exe.

Source code: python build.py and settings.json.

Debug Code

To facilitate source code debugging in VSCode, select the main.c file that you wish to run. First, build and run the source code as described in the previous section. Then, click the following button in VSCode:

Run and Debug (Ctil+Shift+D) -> (gdb) Debug Main

or simply press

F5

Source code: launch.json.

Clean all .exe files

To ensure a clean development environment, it is important to remove unnecessary executable files. The following steps can be used to clean all .exe files:

First, open the terminal by pressing

ctrl + ~

then run

.\F00_Prerequisite\01_Clean_Exe\killExes.bat         /* windows system */
./F00_Prerequisite/01_Clean_Exe/killexe.sh           /* linux system */

Source code: killExes.bat and killExes.sh.

Questions

Now, please feel free to start the leetcode practice journey by selecting the desired programming topic in the Programming_Topics folder.

If there are any questions, please feel free to create an issue in the Issues or start a discussion in the Discussions.