Lab 0 - C Primer


Practice with C Programming: This lab serves to reinforce your proficiency in C programming, particularly with pointers, dynamic memory allocation (malloc), and manual memory management (free), which are critical for systems programming and performance-sensitive applications.

Lab Materials


Discussion Videos

  1. C Primer

Assignment


The sample program compiles but fails the test-cases:
$ make
	gcc -Wall -g -I. -ITests -o runTests main.c
	In file included from Tests/test1.h:4,
					 from main.c:1:
	./LinkedList.h:46:21: warning: ‘createNode’ defined but not used [-Wunused-function]
	   46 | static struct Node* createNode(int data)
		  |                     ^~~~~~~~~~
	Program has been compiled!

$./runTests
	Merge Test Failed: Function returned NULL.
	Cycle Detection Test Failed: Merged list is NULL.
	Merge Test Failed: Function returned NULL.
	Cycle Detection Test Failed: Merged list is NULL.
	Merge Test Failed: Function returned NULL.
	Cycle Detection Test Failed: Merged list is NULL.
	Merge Test Passed: Both lists were empty, and merge returned NULL as expected.
	Cycle Detection Test Passed: No cycle detected, as expected with empty lists.
	Merge Test Failed: Function returned NULL.
	Cycle Detection Test Failed: Merged list is NULL.
	Merge Test Failed: Function returned NULL.
	Cycle Detection Test Failed: Merged list is NULL.
	Merge Test Failed: Function returned NULL.
	Cycle Detection Test Failed: Merged list is NULL.
	Merge Test Failed: Function returned NULL.
	Cycle Detection Test Failed: Merged list is NULL.
	Merge Test Failed: Function returned NULL.
	Cycle Detection Test Failed: Merged list is NULL.
	Merge Test Failed: Function returned NULL.
	Cycle Detection Test Failed: Merged list is NULL.
	Total Score: 28/100
Your tasks for this lab are as follows:
  1. Learn Linked List Implementation: You practice implementing core operations on linked lists, such as node creation, insertion, traversal, and deletion. This helps build your understanding of memory management (dynamic memory allocation and deallocation), pointers, and struct usage in C.
  2. Solve Real-World Problems: By solving problems such as detecting cycles in linked lists and merging two sorted lists, you apply your understanding of algorithms and data structures to real-world scenarios. These problems introduce concepts like fast/slow pointer techniques (cycle detection) and algorithmic merging (sorting and combining data structures).
On successfully completing your task:
$ make runTests
$ ./runTests
	Merged List: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL
	Merge Test Passed.
	Cycle Detection Test Passed.
	Merged List: 1 -> 2 -> 3 -> 4 -> 5 -> 7 -> 9 -> NULL
	Merge Test Passed.
	Cycle Detection Test Passed.
	Merged List: 2 -> 4 -> 6 -> NULL
	Merge Test Passed.
	Cycle Detection Test Passed.
	Merge Test Passed: Both lists were empty, and merge returned NULL as expected.
	Cycle Detection Test Passed: No cycle detected, as expected with empty lists.
	Merged List: 1 -> 1 -> 3 -> 3 -> 5 -> 5 -> NULL
	Merge Test Passed.
	Cycle Detection Test Passed.
	Merged List: 1 -> 3 -> 5 -> 7 -> NULL
	Merge Test Passed.
	Cycle Detection Test Passed.
	Merged List: 2 -> 2 -> 4 -> 6 -> NULL
	Merge Test Passed.
	Cycle Detection Test Passed.
	Merged List: -10 -> -8 -> -3 -> 2 -> 5 -> 7 -> 20 -> NULL
	Merge Test Passed.
	Cycle Detection Test Passed.
	Merged List: 1 -> 2 -> 3 -> 4 -> 6 -> 7 -> 8 -> NULL
	Merge Test Passed.
	Cycle Detection Test Passed.
	Merged List: 1 -> 2 -> 3 -> 10 -> 20 -> 30 -> NULL
	Merge Test Passed.
	Cycle Detection Test Passed.
	Total Score: 100/100

Evaluation


Please compress your source code along with the original files and submit it through Canvas.



< Back to the Lab Home Page